[英]Angle between two vectors in the interval [0,360]
I'm trying to find the angle between two vectors.我试图找到两个向量之间的角度。
Following is the code that I use to evaluate the angle between vectors ba
and bc
以下是我用来评估向量
ba
和bc
之间的角度的代码
import numpy as np
import scipy.linalg as la
a = np.array([6,0])
b = np.array([0,0])
c = np.array([1,1])
ba = a - b
bc = c - b
cosine_angle = np.dot(ba, bc) / (la.norm(ba) * la.norm(bc))
angle = np.arccos(cosine_angle)
print (np.degrees(angle))
My question is,我的问题是,
here in this code:在此代码中:
for both c = np.array([1,1])
and c = np.array([1,-1])
you get 45 degrees as the answer.对于
c = np.array([1,1])
和c = np.array([1,-1])
你得到 45 度作为答案。 I can understand this in a mathematical viewpoint because, from the dot product you always focus on the angle in the interval [0,180]
.我可以从数学的角度理解这一点,因为从点积来看,您始终关注区间
[0,180]
的角度。
But geometrically this is misleading as the point c
is in two different locations for [1,1]
and [1,-1]
.但在几何上这是误导,因为点
c
位于[1,1]
和[1,-1]
两个不同位置。
So is there a way that I can get the angle in the interval [0,360]
for a general starting point那么有没有一种方法可以让我获得区间
[0,360]
中的角度作为一般起点b = np.array([x,y])
Appreciate your help感谢你的帮助
Conceptually, obtaining the angle between two vectors using the dot product is perfectly alright.从概念上讲,使用点积获得两个向量之间的角度是完全没问题的。 However, since the angle between two vectors is invariant upon translation/rotation of the coordinate system, we can find the angle subtended by each vector to the positive direction of the x-axis and subtract one value from the other.
然而,由于两个向量之间的角度在坐标系平移/旋转时是不变的,我们可以找到每个向量与 x 轴正方向所对的角度,并从另一个值中减去一个值。
The advantage is, we'll use np.arctan2
to find the angles, which returns angles in the range [-π,π] and hence you get an idea of the quadrant your vector lies in.优点是,我们将使用
np.arctan2
来查找角度,它返回 [-π,π] 范围内的角度,因此您可以了解向量所在的象限。
# Syntax: np.arctan2(y, x) - put the y value first!
# Instead of explicitly referring by indices, you can unpack each vector in reverse, like so:
# np.arctan2(*bc[::-1])
angle = np.arctan2(bc[1], bc[0]) - np.arctan2(ba[1], ba[0])
Which you can then appropriately transform to get a value within [0, 2π].然后您可以适当地转换以获得 [0, 2π] 内的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.