有没有一种快速的方法来计算两个旋转值之间的最小增量?
有两个视图:
viewA
和 viewB
。两者都是旋转的。
旋转的坐标系很奇怪:它从 0 到 179,999999 或 -179,99999 度。所以本质上 179,99999 和 -179,99999 非常接近!
我想计算这些旋转之间有多少度或弧度。
例如:
viewA 旋转 20 度
viewB 旋转 30 度
我可以这样做:rotationB -rotationA = 10。
但是这个公式的问题是:
viewA 旋转 179 度 viewB 旋转 -179 度,
这会出错:rotationB -rotationA = -179 - 179 = -358
358 显然是错误的,因为它们实际上非常接近。所以我可以做的一件事是检查绝对结果值是否大于 180,如果是,则以相反的方式计算以获得短的真实增量。但我觉得这完全是错误和糟糕的,因为可能存在浮点错误和不精确。因此,如果两个视图基本上相等地旋转 179,99999999999 度,如果幸运的话,我可能会得到一个奇怪的 180 或 0。
也许有一个带有 PI、正弦或其他有用内容的天才式数学公式可以解决这个问题?
There are two views:
viewA
and viewB
. Both are rotated.
The coordinate system for rotation is weird: It goes from 0 to 179,999999 or -179,99999 degrees. So essentially 179,99999 and -179,99999 are very close together!
I want to calculate how much degrees or radians are between these rotations.
For example:
viewA is rotated at 20 degrees
viewB is rotated at 30 degrees
I could just do: rotationB - rotationA = 10.
But the problem with this formula:
viewA is rotated at 179 degrees
viewB is rotated at -179 degrees
that would go wrong: rotationB - rotationA = -179 - 179 = -358
358 is plain wrong, because they are very close together in reality. So one thing I could do maybe is to check if the absolute result value is bigger than 180, and if so, calculate it the other way around to get the short true delta. But I feel this is plain wrong and bad, because of possible floating point errors and unprecision. So if two views are rotated essentially equally at 179,99999999999 degrees I might get a weird 180 or a 0 if I am lucky.
Maybe there's a genius-style math formular with PI, sine or other useful stuff to get around this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
编辑:原始答案(带有 Mod)是错误的。在某些情况下会给出 180 - 正确答案(例如,角度 30 和 -20 会给出 130 的答案,而不是 50 的正确答案):
对于所有情况都有两个正确答案:
如果
A1
和A2
是两个角度(在 -179.99999 和 179.99999 之间,Abs
表示取绝对值,它们之间的角距离表示为:
Angle Between = 180 - Abs(Abs(A1 - A2) - 180)
或者,使用 C 风格的三元运算符:
EDIT: Original answer (with Mod) was wrong. would have given 180 - right answer in certain circumstances (angles 30 and -20 for example would give answer of 130, not correct answer of 50):
Two correct answers for all scenarios:
If
A1
andA2
are two angles (between -179.99999 and 179.99999,and
Abs
means take the Absolute Value,The angular distance between them, is expressed by:
Angle between = 180 - Abs(Abs(A1 - A2) - 180)
Or, using C-style ternary operator:
从您最近提出的问题来看,您可能需要阅读单位圈 。这是三角学中的基本概念,也是使用 CGAffineTransforms 或 CATransform3D 进行旋转时计算角度的方式。
基本上,单位圆的范围是 0 到 360 度,或 0 到 2 * pi(M_PI 是 iPhone 上使用的常数)弧度。任何大于 360 度的角度都等于该角度减去 360 度的倍数。例如,当涉及到旋转了多少度的物体的结束位置时,740 度与 380 度相同,而 380 度与 20 度相同。
同样,负度数与添加 360 度的倍数相同。 -20 度与 340 度相同。
这些计算背后并没有什么魔力,您只需要注意何时有物体穿过圆上的 0 / 360 度点即可。在您描述的情况下,您可以将 360 添加到任何负值以将它们表示为正角度。减去角度时,如果结束角度小于起始角度,您可能还需要在结果中添加 360,以考虑到单位圆上穿过零点的情况。
Judging from the recent questions you've asked, you might want to read up on the unit circle. This is a fundamental concept in trigonometry, and it is how angles are calculated when doing rotations using CGAffineTransforms or CATransform3Ds.
Basically, the unit circle goes from 0 to 360 degrees, or 0 to 2 * pi (M_PI is the constant used on the iPhone) radians. Any angle greater than 360 degrees is the same as that angle minus a multiple of 360 degrees. For example, 740 degrees is the same as 380 degrees, which is the same as 20 degrees, when it comes to the ending position of something rotated by that much.
Likewise, negative degrees are the same as if you'd added a multiple of 360 degrees to them. -20 degrees is the same as 340 degrees.
There's no magic behind any of these calculations, you just have to pay attention to when something crosses the 0 / 360 degree point on the circle. In the case you describe, you can add 360 to any negative values to express them in positive angles. When subtracting angles, if the ending angle is less than the starting angle, you may also need to add 360 to the result to account for crossing the zero point on the unit circle.
让我们再试一次:
A 和 B 之间有两个角,其中一个是
另一个是
因此,只需取这两者中的最小值即可。
Let's try this again:
There are two angles between A and B. One of them is
The other is
So just take the minimum of those two.
除了布拉德·拉尔森的出色回答我想补充一点,你可以这样做:
In addition to Brad Larson's excellent answer I would add that you can do:
取差值,加上 360,然后除以 360。
Take the difference, add 360, and mod by 360.