Last active
December 11, 2015 11:08
-
-
Save HannahMitt/4591509 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Triangle { | |
public static String identifyTriangle(Point w, Point r, Point t) { | |
double A, B, C; | |
double angA, angB, angC; | |
String sideClass, angleClass; | |
A = distance(w, r); | |
B = distance(w, t); | |
C = distance(r, t); | |
//Check valid triangle | |
if(area(w, r, t) == 0){ | |
return "Not a triangle"; | |
} | |
if(A == B && A == C){ | |
sideClass = "equilateral"; | |
} else if(A == B || A == C || B == C){ | |
sideClass = "isosceles"; | |
} else { | |
sideClass = "scalene"; | |
} | |
//cos rule | |
angC = Math.toDegrees(Math.acos(Math.toRadians((Math.pow(A, 2) + Math.pow(B, 2) - Math.pow(C, 2)) / (2 * A * B)))); | |
//sin rule | |
angA = Math.toDegrees(Math.asin(A * Math.sin(Math.toRadians(angC)) / C)); | |
//triangle is 180degrees | |
angB = 180 - angA - angC; | |
if(angA == 90 || angB == 90 || angC == 90){ | |
angleClass = "right"; | |
} else if(angA > 90 || angB > 90 || angC > 90){ | |
angleClass = "obtuse"; | |
} else { | |
angleClass = "acute"; | |
} | |
return sideClass + " " + angleClass + " triangle"; | |
} | |
public static double distance(Point w, Point r){ | |
return Math.sqrt(Math.pow(w.x - r.x, 2) + Math.pow(w.y - r.y, 2)); | |
} | |
public static double area(Point w , Point r, Point t){ | |
return (w.x*(r.y - t.y)) + (r.x*(t.y - w.y)) + (t.x*(w.y - r.y)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment