Java 寻找向量之间的有符号角
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2150050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding Signed Angle Between Vectors
提问by Cerin
How would you find the signed angle theta from vector a to b?
你如何找到从向量 a 到 b 的带符号角 theta?
And yes, I know that theta = arccos((a.b)/(|a||b|)).
是的,我知道 theta = arccos((ab)/(|a||b|))。
However, this does not contain a sign (i.e. it doesn't distinguish between a clockwise or counterclockwise rotation).
但是,这不包含符号(即它不区分顺时针或逆时针旋转)。
I need something that can tell me the minimum angle to rotate from a to b. A positive sign indicates a rotation from +x-axis towards +y-axis. Conversely, a negative sign indicates a rotation from +x-axis towards -y-axis.
我需要一些可以告诉我从 a 旋转到 b 的最小角度的东西。正号表示从 +x 轴向 +y 轴旋转。相反,负号表示从 +x 轴向 -y 轴旋转。
assert angle((1,0),(0,1)) == pi/2.
assert angle((0,1),(1,0)) == -pi/2.
采纳答案by Sparr
If you have an atan2() function in your math library of choice:
如果您选择的数学库中有 atan2() 函数:
signed_angle = atan2(b.y,b.x) - atan2(a.y,a.x)
回答by Derek Ledbetter
What you want to use is often called the “perp dot product”, that is, find the vector perpendicular to one of the vectors, and then find the dot product with the other vector.
你要使用的通常称为“perp dot product”,即找到与其中一个向量垂直的向量,然后找到与另一个向量的点积。
if(a.x*b.y - a.y*b.x < 0)
angle = -angle;
You can also do this:
你也可以这样做:
angle = atan2( a.x*b.y - a.y*b.x, a.x*b.x + a.y*b.y );