Java:以度为单位计算两点之间的角度

时间:2012-04-02 02:37:22

标签: java point angle degrees

我需要为自己的Point类计算两点之间的角度,点a应该是中心点。

方法:

public float getAngle(Point target) {
    return (float) Math.toDegrees(Math.atan2(target.x - x, target.y - y));
}

测试1://返回45

Point a = new Point(0, 0);
    System.out.println(a.getAngle(new Point(1, 1)));

测试2://返回-90,预期:270

Point a = new Point(0, 0);
    System.out.println(a.getAngle(new Point(-1, 0)));

如何将返回的结果转换为0到359之间的数字?

7 个答案:

答案 0 :(得分:72)

您可以添加以下内容:

public float getAngle(Point target) {
    float angle = (float) Math.toDegrees(Math.atan2(target.y - y, target.x - x));

    if(angle < 0){
        angle += 360;
    }

    return angle;
}
顺便问一下,为什么你不想在这里使用双倍?

答案 1 :(得分:26)

我从johncarls解决方案开始,但需要调整它以获得我所需要的。 主要是,我需要它在角度增加时顺时针旋转。我还需要0度指向北方。他的解决方案让我很接近,但我决定发布我的解决方案,以防它帮助其他任何人。

我添加了一些额外的注释,以帮助解释我对函数的理解,以防您需要进行简单的修改。

/**
 * Calculates the angle from centerPt to targetPt in degrees.
 * The return should range from [0,360), rotating CLOCKWISE, 
 * 0 and 360 degrees represents NORTH,
 * 90 degrees represents EAST, etc...
 *
 * Assumes all points are in the same coordinate space.  If they are not, 
 * you will need to call SwingUtilities.convertPointToScreen or equivalent 
 * on all arguments before passing them  to this function.
 *
 * @param centerPt   Point we are rotating around.
 * @param targetPt   Point we want to calcuate the angle to.  
 * @return angle in degrees.  This is the angle from centerPt to targetPt.
 */
public static double calcRotationAngleInDegrees(Point centerPt, Point targetPt)
{
    // calculate the angle theta from the deltaY and deltaX values
    // (atan2 returns radians values from [-PI,PI])
    // 0 currently points EAST.  
    // NOTE: By preserving Y and X param order to atan2,  we are expecting 
    // a CLOCKWISE angle direction.  
    double theta = Math.atan2(targetPt.y - centerPt.y, targetPt.x - centerPt.x);

    // rotate the theta angle clockwise by 90 degrees 
    // (this makes 0 point NORTH)
    // NOTE: adding to an angle rotates it clockwise.  
    // subtracting would rotate it counter-clockwise
    theta += Math.PI/2.0;

    // convert from radians to degrees
    // this will give you an angle from [0->270],[-180,0]
    double angle = Math.toDegrees(theta);

    // convert to positive range [0-360)
    // since we want to prevent negative angles, adjust them now.
    // we can assume that atan2 will not return a negative value
    // greater than one partial rotation
    if (angle < 0) {
        angle += 360;
    }

    return angle;
}

答案 2 :(得分:2)

基于Saad Ahmed's answer,这是一种可用于任何两点的方法。

public static double calculateAngle(double x1, double y1, double x2, double y2)
{
    double angle = Math.toDegrees(Math.atan2(x2 - x1, y2 - y1));
    // Keep angle between 0 and 360
    angle = angle + Math.ceil( -angle / 360 ) * 360;

    return angle;
}

答案 3 :(得分:1)

Math.atan(double)的javadoc很清楚,返回值的范围可以从-pi / 2到pi / 2。所以你需要补偿那个回报值。

答案 4 :(得分:1)

angle = Math.toDegrees(Math.atan2(target.x - x, target.y - y));

现在用于定义圆形值以保持0到359之间的角度可以是:

angle = angle + Math.ceil( -angle / 360 ) * 360

答案 5 :(得分:0)

为什么每个人都会对此感到复杂?

唯一的问题是Math.atan2(x,y)

正确答案是Math.atan2(y,x)

他们所做的只是混合Atan2的可变顺序,使Atan2反转旋转度。

您所要做的就是查找语法 https://www.google.com/amp/s/www.geeksforgeeks.org/java-lang-math-atan2-java/amp/

答案 6 :(得分:-1)

如下:

angle = angle % 360;