-1

So, right now I am trying to calculate the angles of a right triangle using the inverse of Cosine. However, I don't really know how to do it. I know the equation, just not how to convert into code. The equation would be:: Cos-1(A/C); However, that does not seem to work in Java. I also tried

angleX = (int) Math.acos(sideC / sideA);
B Whitehead
  • 49
  • 1
  • 8

1 Answers1

2

If sideC and sideA were integers, one would have integer division (2 / 3 == 0). If you do not expect a result in radians, but degrees, a conversion is needed. As double is an approximation, use round too.

if (sideA == 0) { ... }
angleX = (int) Math.round(
        Math.toDegrees(Math.acos(((double)sideC) / sideA)));
Joop Eggen
  • 96,344
  • 7
  • 73
  • 121