我正在为一个要求我使用3分制作三角形的课程做作业。我已经完成了大部分工作,但似乎无法让角度正确。我使用3边(SSS)的余弦定律公式,但它没有给我一个答案,它只给我NaN或一个非常小的角度。我不知道什么是错的。
//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.");
}
示例输出:
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)
答案 0 :(得分:4)
acos
的参数不能小于-1或大于1,因为它是cos
的域。所以你不应该使用Math.toDegrees
。
请在此处查看示例:http://www.tutorialspoint.com/java/number_acos.htm
修改实际上您似乎只是颠倒了acos
和toDegrees
的使用。
答案 1 :(得分:3)
Itamar Katz是对的,你得到了角度的余弦,需要用acos反转它,然后将其转换为度数。我删除了不必要的括号并编写了两个函数,现在你的代码更具可读性。
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");
}
输入输出样本
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