简体   繁体   中英

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

First you should move the origin to (0, 0) , then you can use np.arctan2() which calculates the angle and defines the quadrant correctly. The result is already in radians (theta) so you don't need it in degrees (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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM