Austin Berke
Austin Berke

Reputation: 87

Need Help Working with Law of Sines/Law of Cosines in Java

I'm doing a project where I have to find the angles of a triangle given the side lengths, and I figured writing a program would simplify things. However, my code isn't working.

int a, b, c; // distance lengths
double A, B, C; // angle measurements

public FootprintSet(int sideA, int sideB, int sideC) {
    a = sideA;
    b = sideB;
    c = sideC;  
    computeAngles();
}

private void computeAngles() {
   A = Math.acos((b * b + c * c - a * a)/(2.0 * b * c)); // law of cosines
   B = Math.asin((Math.sin(A) * b)/a); // law of sines
   C = 180 - (A + B); 
}

public int getPacing() {
    return (int)(A+0.5);
}

public int getStride() {
    return (int)((C + B)/2.0 + 0.5); // average of two stride angles
}

For whatever object I create, getStride() always returns a value that rounds to 89, and getPacing() always returns a value that rounds to 2 or 3. What am I doing wrong?

Upvotes: 1

Views: 6051

Answers (1)

Terry Li
Terry Li

Reputation: 17268

C = 180 - (A + B);

This tells me that you are using degrees.

However, Math.sin takes in radians.

Try using Math.sin(Math.toRadians(A)) instead.

Upvotes: 6

Related Questions