Java:计算2D平面上两点之间的方位

时间:2016-10-20 14:00:54

标签: java angle

我正在使用此代码查找两个Point对象之间的角度:

private float getAngle(Point targetPoint, Point centerPoint){
    // north = 90; south = 270; west = 180; east = 0; 
    // but in my method west = 0 and east = 180
    double theta = Math.atan2(targetPoint.y - centerPoint.y, targetPoint.x - centerPoint.x);
    float angle = (float) Math.toDegrees(theta);

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

其中centerPoint是:[160,160]
targetPoints是:
1 - [x = 160,y = 80],2- [x = 80,y = 160],3 - [x = 240,y = 160],4 - [x = 160,y = 240]。

我认为1应该是90°,2 - 180°,3 - 0°和4 - 270°

但是我得到了这个:

angle between robLoc java.awt.Point[x=160,y=160] and p java.awt.Point[x=160,y=80] is 90.0
angle between robLoc java.awt.Point[x=160,y=160] and p java.awt.Point[x=80,y=160] is 0.0
angle between robLoc java.awt.Point[x=160,y=160] and p java.awt.Point[x=240,y=160] is 180.0
angle between robLoc java.awt.Point[x=160,y=160] and p java.awt.Point[x=160,y=240] is 270.0

如何调整计算方法以获得: 2 - 0°和3 - 180°?

就像这张照片:

enter image description here

0 个答案:

没有答案