/* This program draws a triangle, and reports whether it is an equilateral (all sides the same length), isosceles (2 sides the same length), or scalene (all sides different lengths) triangle type. Please enter the first point's x and y: 0 100 Please enter the second point's x and y: 100 0 Please enter the third point's x and y: 100 100 This triangle is type isosceles. */ import java.awt.*; public class Triangles { public static void main(String[] args) { // Let's write it! printWelcomeMessage(); Scanner console = new Scanner(System.in); System.out.print("Please enter the first point's x and y: "); double x1 = console.nextDouble(); double y1 = console.nextDouble(); System.out.print("Please enter the second point's x and y: "); double x2 = console.nextDouble(); double y2 = console.nextDouble(); System.out.print("Please enter the third point's x and y: "); double x3 = console.nextDouble(); double y3 = console.nextDouble(); drawTriangle(x1, y1, x2, y2, x3, y3); int same = sameLength(x1, y1, x2, y2, x3, y3); if (same == 3) { System.out.println("This triangle is type equilateral"); } else if (same == 2) { System.out.println("This triangle is type isosceles"); } else { // same == 1 System.out.println("This triangle is type scalene"); } } public static void printWelcomeMessage() { System.out.println("This program draws a triangle, and reports whether it is an"); System.out.println("equilateral (all sides the same length),"); System.out.println("isosceles (2 sides the same length), or"); System.out.println("scalene (all sides different lengths) triangle type."); System.out.println(); } public static void drawTriangle(double x1, double y1, double x2, double y2, double x3, double y3) { DrawingPanel panel = new DrawingPanel(400, 400); Graphics g = panel.getGraphics(); Polygon triangle = new Polygon(); triangle.addPoint((int)x1, (int)y1); triangle.addPoint((int)x2, (int)y2); triangle.addPoint((int)x3, (int)y3); g.fillPolygon(triangle); } // If (x1, y1), (x2, y2), and (x3, y3) represent 3 points, // Consider line segments line1, line2, and line3 that connect them. // This method returns 1 if all all three are different lengths, // 2 if two of the line segments are the same length, // and 3 if all three line segments have the same length. public static int sameLength(double x1, double y1, double x2, double y2, double x3, double y3) { double d12 = getDistance(x1, y1, x2, y2); double d13 = getDistance(x1, y1, x3, y3); double d23 = getDistance(x2, y2, x3, y3); // d12 == 10 // d13 == 20 // d23 == 20 int result; if (d12 == d13) { if (d12 == d23) { result = 3; } else { result = 2; } } else { // d12 != d13 if (d12 == d23) { result = 2; } else if (d13 == d23) { result = 2; } else { result = 1; } } return result; } public static double getDistance(double x1, double y1, double x2, double y2) { return Math.round(Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2))); } }