使用余弦定律获取角度但不起作用

Using the law of cosine to get angle but its not working

我正在为 class 做作业,要求我使用 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

Edit 其实你好像只是颠倒了 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