Calico
Calico

Reputation: 57

Triangle Sides Input Output Angles for Non-Right Triangles

I'm trying to write a method which will take an input of the three side lengths and give back the angles in degrees. It uses the law of cosines solved for the angle opposite c, and I switch the sides, so it can find the second angle. However, the program doesn't output any of the correct angles. For instance, I input the side lengths 6, 7, and 8, and it gave me 55.51, 44.28, and 80.21. However none of the angles in a 6 - 7 - 8 triangle match those. Here is the method I used:

double angleNo(double a, double b, double c)
{
   double calc = Math.toDegrees(Math.acos((a*a + b*b - c*c)/(2*a*b)));
   double s = calc*100;
   double r = Math.round(s);
   double e = r/100;
   return e;
}

Also, if it's of any help, here is where it's executed:

        angleOne = pull.angleNo(op1, op2, hypo);
        angleTwo = pull.angleNo(op2, hypo, op1);
        angleThree = 180 - angleTwo - angleOne;

    }

     System.out.println("The angles are " + angleOne + "°, " + angleTwo + "°, " + angleThree + "°.")        

Any help would be lovely, thanks.

Upvotes: 2

Views: 1324

Answers (1)

aioobe
aioobe

Reputation: 421180

The formula is

enter image description here

So change

Math.toDegrees(Math.acos((a*a + b*b - c*c)/2*a*b));

to

Math.toDegrees(Math.acos((a*a + b*b - c*c)/(2*a*b)));
                                           ^     ^

(Otherwise you're dividing by 2 and multiplying by a*b, while you want to divide by 2*a*b.)

I suggest you don't try to round the value inside the method. Let it be a double until you need to print the value, and then you print it using for instance

System.out.printf("Value: %.2f%n", yourDouble);

Upvotes: 2

Related Questions