我需要计算我自己的 Point 类的两点之间的角度(以度为单位),点 a 应为中心点。
Method:
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之间的数字?
您可以添加以下内容:
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;
}
顺便问一下,为什么你不想在这里使用双精度呢?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)