Question

I'm trying to create a program in Java to calculate the inside angles of any triangle when the user inputs the side lengths. I've seen a few questions similar to this but I can`t get mine to work.

I want this to calculate the angle in degrees but it keeps giving me the wrong answer or not a number (NaN). I've tried putting it all in to one equation in case it was just rounding errors but it just gave the same answer. I've since put it back into this format to make it easier to read.

public class Triangles
{
    // variables already declared and user inputs double sideOne, sideTwo, sideThree  
    threeSq=sideThree*sideThree;
    twoSq=sideTwo*sideTwo;
    oneSq=sideOne*sideOne;

    public static double getAngleOne(double oneSq, double twoSq, double threeSq, double sideOne, double sideTwo, double sideThree)
    {
        double angOne;
        angOne = (oneSq + twoSq - threeSq) / (2 * sideOne * sideTwo);
        angOne = Math.toRadians(angOne);
        angOne = Math.acos(angOne);
        angOne = Math.toDegrees(angOne);
        return angOne;
    }

    public static double getAngleTwo(double oneSq, double twoSq, double threeSq, double   sideOne, double sideTwo, double sideThree)
    {
        double angTwo;
        angTwo = (twoSq + threeSq - oneSq) / (2 * sideTwo * sideThree);
        angTwo = Math.toRadians(angTwo);
        angTwo = Math.acos(angTwo);
        angTwo = Math.toDegrees(angTwo);
        return angTwo;
    }

    public static double getAngleThree(double oneSq, double twoSq, double threeSq, double   sideOne, double sideTwo, double sideThree)
    {
        double angThree;
        angThree = (oneSq + threeSq - twoSq) / (2 * sideOne * sideThree);
        angThree = Math.toRadians(angThree);
        angThree = Math.acos(angThree);
        angThree = Math.toDegrees(angThree);
        return angThree;
    }
} 

I`m using the cosine law, but it is not giving me the correct answer. For example, when I input the side lengths as 3, 3 and 3 it gives me 71.68993312052173; when I input 5, 6 and 7 (sides 1, 2 and 3 respectively), I get NaN.

edit: Thanks for the advice, I have changed all the ints to doubles and my math was the problem (forgot brackets around the oneSq + twoSq - threeSq)

I put up the full revised code but it is still giving the wrong answer, for a triangle with all sides the same, it should return 60 for all three but it`s returning 89.49999365358626.

Was it helpful?

Solution

After correcting the computation of the ratios there still remains one thing to do: Lose the lines

angOne = Math.toRadians(angOne);  

at this point, angOne does not contain any angle. If the sides obey the triangle inequality, angOne should at that point contain a number between -1 and 1 that does not need converting.


The ratio of the areas for an equilateral triangle is 0.5. The operations convert-to-radians, acos, convert-to-degrees can be combined as

M*acos(x/M) = M*(pi/2-asin(x/M)),

with the multiplier M=180/pi. Since x/M is small, the result is approximately

M*(pi/2-x/M)=90-x, 

resulting in a value close to 89.5, as obtained in your last trial.


Of course, the desired result is M*acos(0.5)=M*(pi/3)=60.

OTHER TIPS

Apart from not using double values, your calculations are probably not correct.

According to cosine law

cosγ = (a^2 + b^2 - c^2)/2ab

so change ang = oneSq + threeSq - twoSq / (2 * sideOne * sideThree); to

double ang = (oneSq + twoSq - threeSq)*1.0 / (2 * sideOne * sideTwo);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top