在一个坐标周围的-PI-> PI范围内有两个角度,它们之间的两个角度中最小的值是多少?
考虑到PI和-PI之间的差别不是2PI,而是零。
示例:
想象一个圆圈,有两条线从中心出来,这些线条之间有两个角度,它们在内部形成的角度又称为小角度,以及它们形成的角度外面,也就是更大的角度。两个角度加起来形成一个完整的圆。考虑到每个角度都可以在一定的范围内,考虑到滚动,角度值越小 解决方案对于任何角度,这给出了一个有符号的角度: $ p $ a = targetA - sourceA a =(a + 180)%360 - 180注意模数操作返回一个与红利相同符号的值(如C,C ++,C#,JavaScript,完整列表)。这需要定制 mod 函数,如下所示: ,n)→> a - floor(a / n)* n
左右:
mod =(a,n) - > (a%n + n)%n如果角度在[-180,180]之内,这也是works:
a = targetA - sourceA a + =(a> 180)? -360:(a <-180)≤ 360:0以更详细的方式:
a = targetA - sourceA a - = 360如果a> 180 a + = 360如果a < -180
Given 2 angles in the range -PI -> PI around a coordinate, what is the value of the smallest of the 2 angles between them?
Taking into account that the difference between PI and -PI is not 2 PI but zero.
Example:
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 - 180Beware 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) * nOr so:
mod = (a, n) -> (a % n + n) % nIf angles are within [-180, 180] this also works:
a = targetA - sourceA a += (a>180) ? -360 : (a<-180) ? 360 : 0In a more verbose way:
a = targetA - sourceA a -= 360 if a > 180 a += 360 if a < -180
更多推荐
两个角度之间的最小差异
发布评论