首页 > 解决方案 > Calculate the rotation angle of a vector python

问题描述

I am trying to find the rotation angle of a 2D vector. I have found a few questions that use 3D vectors. The following df represents a single vector with the first row as the origin.

d = ({      
    'X' : [10,12.5,17,20,16,14,13,8,7],                 
    'Y' : [10,12,13,8,6,7,8,8,9],                             
     })

df = pd.DataFrame(data = d)

I can rotate a vector using the following equation:

angle = x
theta = (x/180) * numpy.pi

rotMatrix = numpy.array([[numpy.cos(theta), -numpy.sin(theta)], 
                         [numpy.sin(theta),  numpy.cos(theta)]])

But I'm not sure how I would find the angle at each point of time using the coordinates listed above. Apologies for using a df. It replicates my actual dataset

标签: pythonpandasmatrixrotation

解决方案


首先你应该将原点移动到(0, 0),然后你可以使用np.arctan2()它来计算角度并正确定义象限。结果已经以弧度 (theta) 为单位,因此您不需要以度 (alpha) 为单位。

d = {'X' : [10,12.5,17,20,16,14,13,8,7],                
     'Y' : [10.,12,13,8,6,7,8,8,9]}
df = pd.DataFrame(data = d)

# move the origin
x = df["X"] - df["X"][0]
y = df["Y"] - df["Y"][0]

df["theta"] = np.arctan2(y, x)
df["aplha"] = np.degrees(df["theta"])
df

推荐阅读