如何找到两个角度之间的差异? [英] How can I find the difference between two angles?
问题描述
给定一个围绕坐标的-PI-> PI范围内的2个角度,它们之间2个角度中最小的角度值是什么?
Given 2 angles in the range -PI -> PI around a coordinate, what is the value of the smallest of the 2 angles between them?
考虑到PI和-PI之间的差异不是2个PI,而是零.
Taking into account that the difference between PI and -PI is not 2 PI but zero.
示例:
想象一个圆,有2条线从中心出来,这些线之间有2个角度,它们在内侧形成的角度又称为较小的角度,而在内侧形成的角度在外面,也就是更大的角度.将两个角度加在一起时,它们将成一个完整的圆.考虑到每个角度都可以在一定范围内,考虑到翻转,较小的角度值是什么
Imagine a circle, with 2 lines coming out from the center, there are 2 angles between those lines, the angle they make on the inside aka the smaller angle, and the angle they make on the outside, aka the bigger angle. Both angles when added up make a full circle. Given that each angle can fit within a certain range, what is the smaller angles value, taking into account the rollover
推荐答案
这将为任何角度给出一个带正负号的角度:
This gives a signed angle for any angles:
a = targetA - sourceA
a = (a + 180) % 360 - 180
请注意,使用多种语言的 modulo
操作返回的值与除数具有相同的符号(例如C,C ++,C#,JavaScript,
Beware in many languages the modulo
operation returns a value with the same sign as the dividend (like C, C++, C#, JavaScript, full list here). This requires a custom mod
function like so:
mod = (a, n) -> a - floor(a/n) * n
大约:
mod = (a, n) -> (a % n + n) % n
如果角度在[-180,180]以内,这也适用:
If angles are within [-180, 180] this also works:
a = targetA - sourceA
a += (a>180) ? -360 : (a<-180) ? 360 : 0
以更详细的方式:
a = targetA - sourceA
a -= 360 if a > 180
a += 360 if a < -180
这篇关于如何找到两个角度之间的差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!