计算角度,包括两个向量之间的反射角
我需要计算两个向量之间的角度。矢量可以指向任何方向,并且已经标准化。我希望在某些情况下从向量A到向量B顺时针测量角度,在其他情况下从向量A到向量B逆时针测量角度(换句话说,我不只是想知道最小角度)。
这是我所拥有的
if (clockwise) angle = Math.atan2(vectorA.y, vectorA.x) - Math.atan2(vectorB.y, vectorB.x);
else angle = -1*(Math.atan2(vectorA.y, -vectorA.x) - Math.atan2(vectorB.y, -vectorB.x));
,我猜这对反射角永远不起作用?那么如何计算 0->2pi 范围内的角度呢?
I need to calculate the angle between two vectors. The vectors may be pointing in any direction, and have been normalized. I want the angle to be measured clockwise from vectorA to vectorB in some cases, and anticlockwise from vectorA to vectorB in other cases (in other words, I don't just want to know the smallest angle).
Here's what I have
if (clockwise) angle = Math.atan2(vectorA.y, vectorA.x) - Math.atan2(vectorB.y, vectorB.x);
else angle = -1*(Math.atan2(vectorA.y, -vectorA.x) - Math.atan2(vectorB.y, -vectorB.x));
I guess this will never work for reflex angles? So how do I calculate an angle on the range 0->2pi?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
顺时针方向一致地计算它,并在需要逆时针方向时从 360(或 2*pi)中减去。
如果您需要标准化到特定的程度范围,那么您可以通过直接标准化代码的输出来实现。因此,计算顺时针角度,然后添加 2*pi 直到大于零,然后将结果 mod 2*pi,您将得到范围 [0, 2*pi) 的结果。
Calculate it consistently for the clockwise direction, and subtract from 360 (or 2*pi) when you need it counterclockwise.
If you need to normalize to a particular degree range, then you can do so by directly normalizing the output of your code. So calculate the clockwise angle, then add 2*pi until it is above zero, then take the result mod 2*pi, and you will get a result in the range [0, 2*pi).