gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 09:14:53 27 4
gpt4 key购买 nike

我正在为一门课做作业,要求我用 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 的使用。

关于java - 使用余弦定律获取角度但不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35171171/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com