import java.awt.*; public class Triangle implements Shape { private Point p1; private Point p2; private Point p3; public Triangle(Point p1, Point p2, Point p3) { if (p1 == null || p2 == null || p3 == null || p1.equals(p2) || p1.equals(p3) || p2.equals(p3)) { // colinear points would break this throw new IllegalArgumentException(); } this.p1 = p1; this.p2 = p2; this.p3 = p3; } public void draw(Graphics g) { this.p1.draw(g); this.p2.draw(g); this.p3.draw(g); g.drawLine(this.p1.getX(), this.p1.getY(), this.p2.getX(), this.p2.getY()); g.drawLine(this.p2.getX(), this.p2.getY(), this.p3.getX(), this.p3.getY()); g.drawLine(this.p3.getX(), this.p3.getY(), this.p1.getX(), this.p1.getY()); } public double getPerimeter() { return p1.distance(p2) + p2.distance(p3) + p3.distance(p1); } public double getArea() { // this actually doesn't work for all triangles... int maxX = Math.max(this.p1.getX(), Math.max(this.p2.getX(), this.p3.getX())); int minX = Math.min(this.p1.getX(), Math.min(this.p2.getX(), this.p3.getX())); int maxY = Math.max(this.p1.getY(), Math.max(this.p2.getY(), this.p3.getY())); int minY = Math.min(this.p1.getY(), Math.min(this.p2.getY(), this.p3.getY())); int base = maxX - minX; int height = maxY - minY; return 0.5 * base * height; } public int getMinX() { return Math.min(this.p1.getX(), Math.min(this.p2.getX(), this.p3.getX())); } public int getMaxX() { return Math.max(this.p1.getX(), Math.max(this.p2.getX(), this.p3.getX())); } public int getMinY() { return Math.min(this.p1.getY(), Math.min(this.p2.getY(), this.p3.getY())); } public int getMaxY() { return Math.max(this.p1.getY(), Math.max(this.p2.getY(), this.p3.getY())); } public void translate(int dx, int dy) { this.p1.translate(dx, dy); this.p2.translate(dx, dy); this.p3.translate(dx, dy); } public String getType() { double d12 = this.p1.distance(this.p2); double d13 = this.p1.distance(this.p3); double d23 = this.p2.distance(this.p3); if (d12 == d13 && d13 == d23) { return "Equilateral"; } else if (d12 == d13 || d13 == d23 || d12 == d23) { return "Isosceles"; } else { return "Scalene"; } } }