两个角度之间的最小差异

编程入门 行业动态 更新时间:2024-10-25 18:36:48
本文介绍了两个角度之间的最小差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

更多推荐

两个角度之间的最小差异

本文发布于:2023-11-29 00:15:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644527.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最小   差异   角度   两个

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!