如何在Java中计算两个角度测量值(以度为单位)的差值,使结果在0°,180°范围内
例如:
350° to 15° = 25°
250° to 190° = 60°
发布于 2011-09-27 22:41:01
/**
* 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;
}
发布于 2015-06-17 17:11:58
除了Nickes的答案之外,如果你想要“有符号的差分”
int d = Math.abs(a - b) % 360;
int r = d > 180 ? 360 - d : d;
//calculate sign
int sign = (a - b >= 0 && a - b <= 180) || (a - b <=-180 && a- b>= -360) ? 1 : -1;
r *= sign;
编辑:
其中'a‘和'b’是两个要找出差异的角度。
'd‘是不同的。“R”是结果/最终差异。
发布于 2011-09-27 22:33:09
取其差值的绝对值,如果大于180,减去360°,取结果的绝对值。
https://stackoverflow.com/questions/7570808
复制相似问题