Java 圆上的三个随机点

Java 圆上的三个随机点,java,geometry,points,Java,Geometry,Points,我正在尝试创建一个程序,其中在一个圆中创建三个随机点,从而创建一个内接三角形。然而,我得到的角度都搞砸了 这是我的密码: public static void main(String[] args) { double r = 40.0; double angle1 = Math.random()* (2 * Math.PI); double angle2 = Math.random()* (2 * Math.PI); dou

我正在尝试创建一个程序,其中在一个圆中创建三个随机点,从而创建一个内接三角形。然而,我得到的角度都搞砸了

这是我的密码:

    public static void main(String[] args) {
        double r = 40.0;
        double angle1 = Math.random()* (2 * Math.PI);
        double angle2 = Math.random()* (2 * Math.PI);
        double angle3 = Math.random()* (2 * Math.PI);
        double x_1 = r * Math.cos(angle1); 
        double y_1 = r * Math.sin(angle1);
        double x_2 = r * Math.cos(angle2); 
        double y_2 = r * Math.sin(angle2);
        double x_3 = r * Math.cos(angle3); 
        double y_3 = r * Math.sin(angle3);
        System.out.println("The coordinates of the three points are: 
        (" + x_1 +                 ", " + y_1 + ") 
        (" + x_2 + ", " + y_2 + ") 
        (" + x_3 + ", " + y_3 + ")");
    //Get length of each side
    double distanceFrom1To2 = Math.sqrt(Math.pow(x_2 - x_1, 2) + 
    Math.pow(y_2 - y_1, 2));
    double distanceFrom2To3 = Math.sqrt(Math.pow(x_3 - x_2, 2) + 
    Math.pow(y_3 - y_2, 2));
    double distanceFrom3To1 = Math.sqrt(Math.pow(x_1 - x_3, 2) + 
    Math.pow(y_1 - y_3, 2));
    //Get angles ***
    double triangleAngle1 = Math.atan(distanceFrom1To2 / distanceFrom2To3);
    double triangleAngle2 = Math.atan(distanceFrom2To3 / distanceFrom3To1);
    double triangleAngle3 = Math.atan(distanceFrom3To1 / distanceFrom1To2);
    System.out.println("The three angles are " + triangleAngle1 + " " + 
    triangleAngle2 + " " + triangleAngle3);
    System.out.println(triangleAngle1 + triangleAngle2 + triangleAngle3);
}
我肯定知道获取角度的方法是错误的。以下是我的程序运行示例:

    The coordinates of the three points are: (5.224534224725408,  
    -39.65733528787168) (-29.696946087404676, 26.79722733944279) 
    (32.70889681040468, -23.02451018906371)

    The three angles are 0.7545364726122026 1.18830825410364 
    0.40435068059871415
    Total angle sum: 2.347195407314557
所有角度加起来都远大于π/2弧度。我考虑过正弦定律,但你必须至少知道一个角度…

找到了它

以下是固定代码:

    //Get length of each side
    double a = Math.sqrt(Math.pow(x_2 - x_1, 2) + Math.pow(y_2 - y_1, 2)); // distance from 1 to 2
    double b = Math.sqrt(Math.pow(x_3 - x_2, 2) + Math.pow(y_3 - y_2, 2)); // distance from 2 to 3
    double c = Math.sqrt(Math.pow(x_1 - x_3, 2) + Math.pow(y_1 - y_3, 2)); // distance from 3 to 1
    //Get angles ***
    double triangleAngle1 = Math.acos((Math.pow(a, 2) + Math.pow(b, 2) - Math.pow(c, 2)) / (2 * a * b));
    double triangleAngle2 = Math.acos((Math.pow(b, 2) + Math.pow(c, 2) - Math.pow(a, 2)) / (2 * c * b));
    double triangleAngle3 = Math.acos((Math.pow(c, 2) + Math.pow(a, 2) - Math.pow(b, 2)) / (2 * a * c));
我把它改成使用余弦定律。

算出了

以下是固定代码:

    //Get length of each side
    double a = Math.sqrt(Math.pow(x_2 - x_1, 2) + Math.pow(y_2 - y_1, 2)); // distance from 1 to 2
    double b = Math.sqrt(Math.pow(x_3 - x_2, 2) + Math.pow(y_3 - y_2, 2)); // distance from 2 to 3
    double c = Math.sqrt(Math.pow(x_1 - x_3, 2) + Math.pow(y_1 - y_3, 2)); // distance from 3 to 1
    //Get angles ***
    double triangleAngle1 = Math.acos((Math.pow(a, 2) + Math.pow(b, 2) - Math.pow(c, 2)) / (2 * a * b));
    double triangleAngle2 = Math.acos((Math.pow(b, 2) + Math.pow(c, 2) - Math.pow(a, 2)) / (2 * c * b));
    double triangleAngle3 = Math.acos((Math.pow(c, 2) + Math.pow(a, 2) - Math.pow(b, 2)) / (2 * a * c));
我把它改为使用余弦定律。

你需要使用而不是
atan
来表示你的角度。我假设这是Java,你应该用一种语言来标记你的问题,但不管是哪种语言,建议都适用。你需要使用而不是
atan
。我假设这是Java,你应该用一种语言来标记你的问题——但不管是哪种语言,建议都适用。