参见英文答案 > The smallest difference between 2 Angles 8个
如何计算Java中两个角度测量值(以度为单位)的差值,结果是在[0°,180°]范围内?
例如:
350° to 15° = 25°
250° to 190° = 60°
解决方法:
/**
* Shortest distance (angular) between two angles.
* It will be in range [0, 180].
*/
public static int distance(int alpha, int beta) {
int phi = Math.abs(beta - alpha) % 360; // This is either the distance or 360 - distance
int distance = phi > 180 ? 360 - phi : phi;
return distance;
}