[英]How to efficiently calculate full (2pi) angles between three vectors of 2d points
I have three numpy arrays of shape [n, 2] containing the a list of points. 我有形状[n,2]的三个numpy数组,其中包含点列表。 Let's call these a, b, and c.
我们称它们为a,b和c。 I want to find the full angle between ab and bc.
我想找到ab和bc之间的完整角度。 Using acos nets me only pi radians, but I want the full 2pi scale.
使用acos只能使我获得pi弧度,但是我想要完整的2pi比例。 I considered using atan2, but am unsure of how to calculate the y and x vectors necessory for atan2 - I tried using vector norms, but these are inherently positive.
我考虑过使用atan2,但是不确定如何计算atan2必需的y和x向量-我尝试使用向量范数,但这些方法本质上是肯定的。 Is there any way I can do this completely using numpy functions for efficiency?
有什么办法可以完全使用numpy函数来做到这一点?
Using the arccos
method alone only gives you the absolute angle between the vectors, not wether it is clock-wise or counter-clockwise. 单独使用
arccos
方法只能提供矢量之间的绝对角度,而不能顺时针或逆时针旋转。 You can augment this by checking if the dot product of a
against the perpendicular of b
is negative, signifying a counter-clockwise angle. 您可以通过检查
a
对于b
的垂直线的点积是否为负(表示逆时针角度)来扩大此范围。
import numpy as np
def dot(a, b):
return np.sum(a * b, axis=-1)
def mag(a):
return np.sqrt(np.sum(a*a, axis=-1))
def angle(a, b):
cosab = dot(a, b) / (mag(a) * mag(b)) # cosine of angle between vectors
angle = np.arccos(cosab) # what you currently have (absolute angle)
b_t = b[:,[1,0]] * [1, -1] # perpendicular of b
is_cc = dot(a, b_t) < 0
# invert the angles for counter-clockwise rotations
angle[is_cc] = 2*np.pi - angle[is_cc]
return angle
print(angle(
np.array([[1, 0], [1, 0]]),
np.array([[0, 1], [0, -1]])
))
Will print the float values of [pi/2, 3pi/2]
. 将打印浮点值
[pi/2, 3pi/2]
。
This function outputs in the range [0, 2*pi]
. 该函数的输出范围为
[0, 2*pi]
。
Unsurprisingly, the angle
function can be used here. 毫不奇怪,这里可以使用
angle
函数。 It takes a complex argument x + yi
: 它需要一个复杂的参数
x + yi
:
The advantage of this method is that relative angles are easily obtained. 该方法的优点是容易获得相对角度。 With the
atan2
this would be a bit trickier. 使用
atan2
这会有些棘手。
def get_angle(a,b,yx=False):
# make sure inputs are contiguous float
# swap x and if requested
a,b = map(np.ascontiguousarray, (a[...,::-1],b[...,::-1]) if yx else (a,b), (float,float))
# view cast to complex, prune excess dimension
A,B = (z.view(complex).reshape(z.shape[:-1]) for z in (a,b))
# to get the relative angle we must either divide
# or (probably cheaper) multiply with the conjugate
return np.angle(A.conj()*B)
a,b,c = np.random.randn(3,20,2)
# let's look at a roundtrip as a test
get_angle(a,b)+get_angle(b,c)+get_angle(c,a)
# array([ 0.00000000e+00, 1.66533454e-16, 4.44089210e-16, -2.22044605e-16,
# 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, -4.44089210e-16,
# 0.00000000e+00, -1.66533454e-16, 2.22044605e-16, 0.00000000e+00,
# 0.00000000e+00, 2.22044605e-16, 6.28318531e+00, 8.32667268e-17,
# 2.22044605e-16, -6.28318531e+00, -2.22044605e-16, 6.28318531e+00])
# some zeros, some 2pi and some -2pi ==> looks ok
# Let's also check the sum of angles of triangles abc:
get_angle(a-c,b-c)+get_angle(b-a,c-a)+get_angle(c-b,a-b)
# array([-3.14159265, -3.14159265, 3.14159265, -3.14159265, -3.14159265,
# 3.14159265, -3.14159265, -3.14159265, 3.14159265, -3.14159265,
# -3.14159265, 3.14159265, -3.14159265, -3.14159265, 3.14159265,
# 3.14159265, -3.14159265, -3.14159265, 3.14159265, 3.14159265])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.