1

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

2
  • What do the coordinates in your dataset represent? Does each (x,y) pair represent a vector from the origin to the point (x,y)? Between which two "lines" are you measuring this angle? Commented Nov 8, 2018 at 2:41
  • @Carol Ng, the question has been updated sorry. It is a single vector. I want to measure the rotation angle at each point of time. So between points Commented Nov 8, 2018 at 3:05

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Tomas. This just calculate the rotation angle referenced from the origin doesn't it.
Yes, and by origin I mean first row of df, (10, 10)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.