Question is from here:
Today’s exercise is Problem A from the Google Code Jam Beta 2008. The problem is to accept three points as input, determine if they form a triangle, and, if they do, classify it at equilateral (all three sides the same), isoceles (two sides the same, the other different), or scalene (all three sides different), and also classify it as acute (all three angles less than 90 degrees), obtuse (one angle greater than 90 degrees) or right (one angle equal 90 degrees).
Your task is to write the triangle classifier.
Solution:
Plot three points on the (x,y) plane and join them with lines. If the points are not in the same straight line, you will have created a triangle.
Internal angles ‘A’, ‘B’ and ‘C’, and sides of length ‘a’, ‘b’ and ‘c’:
Distance can be calculated using:

After calculating three distances, internal angels ‘A’,'B’ and ‘C’ can be calculated using these formula:
Step 1: Begin by using the cosine rule to find the largest angle.
Note: The largest angle is always opposite to the largest side (assumed to be distance 2 here):

Step 2: Use the sine rule to find one of the remaining angles.
Step 3: Use the ‘sum of internal angles’ rule to find the third angle.
public class Triangle_Trilemma {
public static void main(String[] args) {
System.out.print("CASE # 1: ");
isTri(new Pair(0, 0), new Pair(0, 4), new Pair(1, 2));
System.out.print("CASE # 2: ");
isTri(new Pair(1, 1), new Pair(1, 4), new Pair(3, 2));
System.out.print("CASE # 3: ");
isTri(new Pair(2, 2), new Pair(2, 4), new Pair(4, 3));
System.out.print("CASE # 4: ");
isTri(new Pair(3, 3), new Pair(3, 4), new Pair(5, 3));
System.out.print("CASE # 5: ");
isTri(new Pair(4, 4), new Pair(4, 5), new Pair(5, 6));
System.out.print("CASE # 6: ");
isTri(new Pair(5, 5), new Pair(5, 6), new Pair(6, 5));
System.out.print("CASE # 7: ");
isTri(new Pair(6, 6), new Pair(6, 7), new Pair(6, 8));
System.out.print("CASE # 8: ");
isTri(new Pair(7, 7), new Pair(7, 7), new Pair(7, 7));
}
private static void isTri(Pair p1, Pair p2, Pair p3){
double dis1, dis2, dis3;
//calculating distance
dis1 = Math.pow((p1.returnX() - p3.returnX()), 2) +
Math.pow((p1.returnY() - p3.returnY()), 2);
dis2 = Math.pow((p1.returnX() - p2.returnX()), 2) +
Math.pow((p1.returnY() - p2.returnY()), 2);
dis3 = Math.pow((p2.returnX() - p3.returnX()), 2) +
Math.pow((p2.returnY() - p3.returnY()), 2);
dis1 = Math.sqrt(dis1);
dis2 = Math.sqrt(dis2);
dis3 = Math.sqrt(dis3);
double ang1, ang2, ang3;
//dis2 must be the largest side, in order to
//use following formula, which calculates
//largest angel first (largest angle is opposite
//largest side which is considered to be dis2)
if( dis1 > dis2 || dis3 > dis2){
double temp = dis2;
if(dis1 > dis3){
dis2 = dis1;
dis1 = temp;
} else {
dis2 = dis3;
dis3 = temp;
}
}
//calculating angles
//cosB = a^2 + c^2 - b^2/2ac
ang1 = (dis1*dis1 + dis3*dis3 - dis2*dis2)/(2*dis1*dis3);
//B = cos^-1(cosB)
ang1 = Math.acos(ang1)*180/Math.PI;
//sinC = c*sinB/b
ang2 = (dis3 * Math.sin(ang1*Math.PI/180))/dis2;
//A = sin^-1(sinC)
ang2 = Math.asin(ang2)*180/Math.PI;
//summation of three angles must be 180
ang3 = 180 - ang2 - ang1;
//condition for not a triangle
if(ang1 == 0 || ang2 == 0 || ang3 == 0 || Double.isNaN(ang1)
|| Double.isNaN(ang2) || Double.isNaN(ang3)){
System.out.println("not a triangle");
return;
}
//rounding angles to make comparison easier
ang1 = Math.round(ang1);
ang2 = Math.round(ang2);
ang3 = Math.round(ang3);
String triType = "";
//deciding triangle type based on angels
if(ang1 == 90 || ang2 == 90 || ang3 == 90){
triType += "right ";
} else if(ang1 > 90 || ang2 > 90 || ang3 > 90) {
triType += "obtuse ";
}else {
triType += "acute ";
}
//deciding triangle type based on length of sides
if(dis1 != dis2 && dis2 !=dis3 && dis1 != dis3){
triType += "scalene ";
} else if (dis1 == dis2 || dis2 == dis3 || dis1 == dis3){
triType += "isosceles ";
} else if(dis1 == dis2 && dis2 == dis3){
triType += "equilateral ";
}
triType += "triangle";
System.out.println(triType);
}
//inner class
static class Pair{
private int x;
private int y;
public Pair(int x, int y){
this.x = x;
this.y = y;
}
public void setX(int x){
this.x = x;
}
public int returnX(){
return x;
}
public void setY(int y){
this.y = y;
}
public int returnY(){
return y;
}
}
}
Output:
CASE # 1: obtuse isosceles triangle CASE # 2: acute scalene triangle CASE # 3: acute isosceles triangle CASE # 4: right scalene triangle CASE # 5: obtuse scalene triangle CASE # 6: right isosceles triangle CASE # 7: not a triangle CASE # 8: not a triangle

