我正在使用此代码查找两个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°?
就像这张照片: