Skip to content

Instantly share code, notes, and snippets.

@HannahMitt
Last active December 11, 2015 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HannahMitt/4591509 to your computer and use it in GitHub Desktop.
Save HannahMitt/4591509 to your computer and use it in GitHub Desktop.
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