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 Triangle(Point2D _pA, Point2D _pB, Point2D _pC){ point1 = _pA; point2 = _pB; point3 = _pC; } public void draw(BufferedImage img){ if (!is_initialised){initialise();} int[] point1 = new int[]{0,0}; int[] point2 = new int[]{0, 0}; int lastX = 0; int lastXLong = 0; char currentLine = 'A'; LineLong.initialise(); LineA.initialise(); LineB.initialise(); LineLong.draw(img); LineA.draw(img); LineB.draw(img); for(int x = min.intX(); x <= max.intX(); x += 1) { while(lastXLong == point1[0]) { point1 = LineLong.nextPix(); } while(lastX == point2[0]) { if (currentLine == 'A') { point2 = LineA.nextPix(); if (point2 == null) { currentLine = 'B'; point2 = LineB.nextPix(); } } else { point2 = LineB.nextPix(); } } lastXLong = point1[0]; lastX = point2[0]; if(point1[1] < point2[1]) { for (int y = point1[1]; y <= point2[1]; y += 1) {img.setRGB(x, y, Color.HSBtoRGB(.5f, 1, 1)); }} else { for (int y = point2[1]; y <= point1[1]; y += 1) {img.setRGB(x, y, Color.HSBtoRGB(.5f, 1, 1)); }} } is_initialised = false; } public void initialise(){ 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. // if there are no vertical lines, then we need to find which points touch the edges. 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; } }