我怎样才能找到两个角度之间的差异? [英] How can I find the difference between two angles?
问题描述
给定范围内的 2 个角度 -PI -> PI 围绕一个坐标,它们之间的 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、此处为完整列表).这需要一个自定义的 mod
函数,如下所示:
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屋!