/* Name: Richard Fox Class: CSC 260.002 Date: 8/17/16 */ /* Description: This program determines if 3 input points form a right triangle or not. After inputting the 3 points as x,y cartesian coordinates (6 variables), the program computes the length of each side using the Euclidean distance formula and then uses the Pythagorean theorem on the 3 sides to determine if the triangle is a right triangle or not. The Euclidean distance formula is: length = square root((point1 x - point2 x)^2 + (point1 y - point2 y)^2). The Pythagoream theorem says if the three sides are a, b and c with a being the longest (hypotenuse) then if a^2 = b^2 + c^2 then the triangle is a right triangle. Given 3 sides, s1, s2, s3, if s1 is the hypotenuse, then we determine if s1 = square root(s2^2 + s3^2) to determine if the triangle is right or not. */ import java.util.*; // for Scanner public class RightTriangle3 { public static void main(String[] args) { int p1x, p1y, p2x, p2y, p3x, p3y; // the 3 points as cartesian (x,y) coordinates double s1, s2, s3; // given the 3 points, we compute the 3 sides and store them in these doubles boolean right; // used to store whether the triangle is a right triangle or not Scanner in=new Scanner(System.in); // For keyboard input System.out.print("Input p1x: "); // prompt the user and input point 1 p1x=in.nextInt(); System.out.print("Input p1y: "); p1y=in.nextInt(); System.out.print("Input p2x: "); // prompt the user and input point 2 p2x=in.nextInt(); System.out.print("Input p2y: "); p2y=in.nextInt(); System.out.print("Input p3x: "); // prompt the user and input point 3 p3x=in.nextInt(); System.out.print("Input p3y: "); p3y=in.nextInt(); // compute the length of each side s1=computeSide(p1x,p1y,p2x,p2y); // s1 is the side connecting points 1 & 2 s2=computeSide(p2x,p2y,p3x,p3y); // s2 is the side connecting points 2 & 3 s3=computeSide(p3x,p3y,p1x,p1y); // s3 is the side connecting points 1 & 3 right=computePythagorean(s1,s2,s3); // determine if triangle is right triangle or not and output result if(right) System.out.println("the triangle formed out of these points is a right triangle"); else System.out.println("the triangle formed out of these points is a not right triangle"); System.exit(0); // exit program } // This method uses the Euclidean distance formula to compute the distance between two cartesian points. // The return value is a double, automatically cast because of the Math.sqrt method. public static double computeSide(int x1, int y1, int x2, int y2) { return Math.sqrt(Math.pow((x1-x2),2)+Math.pow((y1-y2),2)); } // This method determines if the 3 lengths passed to it form a right triangle using // the Pythagorean theorem. In this case, assuming s1 is the longest, it is determining // if s1^2 = s2^2 + s3^2. It is doing it by taking the square root of both sides. // This method returns a boolean based on the application of the theorem. public static boolean computePythagorean(double s1, double s2, double s3) { // determine which side is longest first if(s1>s2&&s1>s3) return (s1==Math.sqrt(s2*s2+s3*s3)); // s1 is longest else if (s2>s1&&s2>s3) return (s2==Math.sqrt(s1*s1+s3*s3)); // s2 is longest else return (s3==Math.sqrt(s1*s1+s2*s2)); // s3 is longest } }