import java.awt.*; import java.awt.image.BufferedImage; public class Triangle{ public Point2D point1; public Point2D point2; public Point2D point3; private Line LineLong; private Line LineA; private Line LineB; // initialisation variables private boolean is_initialised = false; private Point2D min; private Point2D max; // progress variables public void invalidate() { is_initialised = false; } public Triangle(Point2D _pA, Point2D _pB, Point2D _pC){ point1 = _pA; point2 = _pB; point3 = _pC; } // returns int for debug public int draw(BufferedImage img){ long lastMillis; if (!is_initialised){initialise();} int[] point1; int[] point2; char currentLine = 'A'; lastMillis = System.currentTimeMillis(); point1 = LineLong.nextPix(); point2 = LineA.nextPix(); for(int x = min.x+1; x <= max.x; x += 1) { while(x-1 == point1[0]) { point1 = LineLong.nextPix(); } while(x-1 == point2[0]) { if (currentLine == 'A') { try{point2 = LineA.nextPix();} catch (RuntimeException e){ currentLine = 'B'; point2 = LineB.nextPix(); } } else { point2 = LineB.nextPix(); } } // cancel drawing if the x value of the triangle is out of bounds if (x >= img.getWidth()) {break;} if (x > 0) { // check which way to loop // TODO - work out a way of not needing to test for this every time if (point1[1] < point2[1]) { for (int y = Math.max(point1[1], 0); y <= Math.min(point2[1], img.getHeight() - 1); y += 1) { img.setRGB(x, y, Color.HSBtoRGB(.5f, 1, 1)); } } else { for (int y = Math.max(point2[1], 0); y <= Math.min(point1[1], img.getHeight() - 1); y += 1) { img.setRGB(x, y, Color.HSBtoRGB(.5f, 1, 1)); } } } } lastMillis = (System.currentTimeMillis() - lastMillis); return (int)lastMillis; } public void initialise(){ if (point1 == null || point2 == null || point3 == null){ throw new NullPointerException(); } min = new Point2D(Math.min(point1.x, Math.min(point2.x, point3.x)), Math.min(point1.y, Math.min(point2.y, point3.y))); max = new Point2D(Math.max(point1.x, Math.max(point2.x, point3.x)), Math.max(point1.y, Math.max(point2.y, point3.y))); // woo horrible IFs mess. // we need to figure out which points touch the edges in order to find which line is the 'full length' edge. Point2D realPointA; Point2D realPointB; Point2D realPointC; if (point1.x == min.x) { realPointA = point1; if (point2.x == max.x){ realPointB = point2; realPointC = point3; } else { realPointB = point3; realPointC = point2; } } else if (point2.x == min.x) { realPointA = point2; if (point1.x == max.x) { realPointB = point1; realPointC = point3; } else { realPointB = point3; realPointC = point1; } } else { realPointA = point3; if (point1.x == max.x) { realPointB = point1; realPointC = point2; } else { realPointB = point2; realPointC = point1; } } // assign points to lines LineLong = new Line(realPointA, realPointB); LineA = new Line(realPointA, realPointC); LineB = new Line(realPointC, realPointB); is_initialised = true; } }