Reputation: 63
I'm doing an assignment for a class that requires me to make a triangle using 3 points. i have most of it done but i cant seem to get the angles right. I'm using the formula of law of cosines using 3 sides (SSS) but it doesnt give me an answer, it just gives me NaN or a very small angle. I dont know whats wrong.
//returns angle between side 1 and 2
public double getAngle1()
{
return Math.acos(Math.toDegrees(((getSide1()*getSide1())+(getSide2()*getSide2())-(getSide3()*getSide3()))/(2*getSide1()*getSide2())));
}
//returns angle between 2 and 3
public double getAngle2()
{
return Math.acos(Math.toDegrees(((getSide2()*getSide2())+(getSide3()*getSide3())-(getSide1()*getSide1()))/(2*getSide2()*getSide3())));
}
//returns angle between 3 and 1
public double getAngle3()
{
return Math.acos(Math.toDegrees(((getSide3()*getSide3())+(getSide1()*getSide1())-(getSide2()*getSide2()))/(2*getSide1()*getSide3())));
}
public void getTriangle()
{
System.out.println("The length of side 1 is: " + getSide1() + " units.");
System.out.println("The length of side 2 is: " + getSide2() + " units.");
System.out.println("The length of side 3 is: " + getSide3() + " units.");
System.out.println("Angle 1 is: " + getAngle1() + " degrees");
System.out.println("Angle 2 is: " + getAngle2() + " degrees");
System.out.println("Angle 3 is: " + getAngle3() + " degrees");
System.out.println("The perimeter of the triangle is: " + getPerimeter() + " units.");
System.out.println("The area of the triangle is: " + getArea() + " units.");
}
Example output:
Enter the coordinate x1: 0
Enter the coordinate y1: 0
Enter the coordinate x2: 0
Enter the coordinate y2: 1
Enter the coordinate x3: 1
Enter the coordinate y3: 0
The length of side 1 is: 1.0 units.
The length of side 2 is: 1.4142135623730951 units.
The length of side 3 is: 1.0 units.
Angle 1 is: 0 degrees
Angle 2 is: 0 degrees
Angle 3 is: 2 degrees
The perimeter of the triangle is: 3 units.
The area of the triangle is: 0 units.
BUILD SUCCESSFUL (total time: 19 seconds)
Upvotes: 2
Views: 618
Reputation: 6887
Itamar Katz is right, you get the cosine of the angle and need to reverse it with acos and then convert it to degrees. I removed unnecessary brackets and wrote two functions, now your code is much more readable.
public static double getAngleGamma()
{
// a²+b²-c² / 2ab
return arcCosineInDegree( (square(getSideA()) + square(getSideB()) - square(getSideC())) / (2 * getSideA() * getSideB()) );
}
public static double getAngleAlpha()
{
// b²+c²-a² / 2bc
return arcCosineInDegree( (square(getSideB()) + square(getSideC()) - square(getSideA())) / (2 * getSideB() * getSideC()) );
}
public static double getAngleBeta()
{
// a²+c²-b² / 2ac
return arcCosineInDegree( (square(getSideC()) + square(getSideA()) - square(getSideB())) / (2 * getSideA() * getSideC()) );
}
public static double square(double x)
{
return Math.pow(x, 2);
}
public static double arcCosineInDegree(double cosine)
{
return Math.toDegrees(Math.acos(cosine));
}
public static void getTriangle()
{
System.out.println("The length of side A is: " + getSideA() + " units.");
System.out.println("The length of side B is: " + getSideB() + " units.");
System.out.println("The length of side C is: " + getSideC() + " units.");
System.out.println("Angle Alpha is: " + getAngleAlpha() + " degrees");
System.out.println("Angle Gamma is: " + getAngleGamma() + " degrees");
System.out.println("Angle Beta is: " + getAngleBeta() + " degrees");
}
Input-Output-Sample
Intput:
A = 8
B = 6
C = 7
Output:
The length of side A is: 8.0 units.
The length of side B is: 6.0 units.
The length of side C is: 7.0 units.
Angle Alpha is: 75.52248781407008 degrees
Angle Gamma is: 57.91004874371969 degrees
Angle Beta is: 46.56746344221023 degrees
Upvotes: 3
Reputation: 9655
The argument for acos
cannot be smaller than -1 or larger than 1, since that's the domain of cos
. So you shouldn't use Math.toDegrees
.
See an example here: http://www.tutorialspoint.com/java/number_acos.htm
Edit Actually seems you just reversed the use of acos
and toDegrees
.
Upvotes: 4