两个角度之间的最小差异 [英] The smallest difference between 2 Angles

查看:171
本文介绍了两个角度之间的最小差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个坐标周围的-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 - 180

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

Or so:

mod = (a, n) -> (a % n + n) % n

If angles are within [-180, 180] this also works:

a = targetA - sourceA
a += (a>180) ? -360 : (a<-180) ? 360 : 0

In a more verbose way:

a = targetA - sourceA
a -= 360 if a > 180
a += 360 if a < -180

这篇关于两个角度之间的最小差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆