使用余弦定律来获得角度,但它不起作用



我正在为一个要求我用 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)

acos的参数不能小于 -1 或大于 1,因为这是 cos 的域。所以你不应该使用Math.toDegrees.

请参阅此处的示例:http://www.tutorialspoint.com/java/number_acos.htm

编辑实际上似乎您只是颠倒了acostoDegrees的使用。

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

最新更新