Friday 6 August 2021

Rotate xy points using multiple angles - python

I'm currently rotating xy points using the angle between a reference point and a single fixed point. I'm hoping to include two fixed points to rotate the points. I'm just not sure how this can be achieved.

Using below, the points that get rotated are displayed in x and y. The angle between X_Ref, Y_Ref and X_Fixed,Y_Fixed is used to rotate the points (displayed as the black vector in the initial distribution figure).

The current process is to transform the fixed/ref points to complex numbers to compute the rotation. I finally re-center the rotated points so X_Ref, Y_Ref is centred at 0,0 (shown in the after rotation figure).

However, I'm hoping to include the angle between X_Ref, Y_Ref and X2_Fixed,Y2_Fixed. The second figure below shows how only one angle is accounted for when rotating the points. I'm hoping to account for both to return the intended out. Any suggestions/advice is welcome.

df = pd.DataFrame({  
    'Period' : ['1','1','1','1'],        
    'Label' : ['A','B','C','D'],                             
    'x' : [0.0,-1.0,3.0,2.0],
    'y' : [0.0,-1.0,-1.0,0.0],     
    'X_Ref' : [1,1,1,1],
    'Y_Ref' : [1,1,1,1],        
    'X_Fixed' : [-2,-2,-2,-2],
    'Y_Fixed' : [-2,-2,-2,-2],      
    'X2_Fixed' : [4,4,4,4],
    'Y2_Fixed' : [-2,-2,-2,-2],           
    })

fig, ax = plt.subplots(figsize = (6,6))
ax.set_xlim(-5,5)
ax.set_ylim(-5,5)
ax.grid(False)

Initial distribution:

enter image description here

#transform fixed/ref points to complex numbers
for f in ['Ref', 'Fixed']:
    df[f] = df['X_'+f] + 1j*df['Y_'+f]
    df.drop(['X_'+f, 'Y_'+f], axis=1, inplace=True)

#compute the rotation  
df['angle'] = - np.angle(df['Ref'] - df['Fixed'])

#compute the rotation for every point 
df['rotated'] = (df['x'] + 1j*df["y"]) * np.exp(1j*df['angle'])
for f in ['Ref', 'Fixed']:
    df[f+'_Rotated'] = df[f] * np.exp(1j*df['angle'])

#center the dataset around the "reference" point 
df['translation'] = - df['Ref_Rotated']
df['NewPoint'] = df['rotated'] + df['translation']
for f in ['Ref', 'Fixed']:
    df[f+'_Transformed'] = df[f+'_Rotated'] + df['translation']

#revert to cartesian coordinates
df['x2'] = np.real(df['NewPoint'])
df['y2'] = np.imag(df['NewPoint'])
for f in ['Ref', 'Fixed']:
    df['NewX_'+f] = np.real(df[f+'_Transformed'])
    df['NewY_'+f] = np.imag(df[f+'_Transformed'])

After Rotation:

output = df[['Label', 'x2', 'y2', 'NewX_Ref', 'NewY_Ref', 'NewX_Fixed', 'NewY_Fixed']]

ax.scatter(output['NewX_Ref'], output['NewY_Ref'], marker = 'x', zorder = 5, color = 'black')
ax.scatter(output['NewX_Fixed'], output['NewY_Fixed'], marker = '+', zorder = 5, color = 'red')
ax.scatter(output['x2'], output['y2'], marker = 'o')

enter image description here

Intended rotation:

enter image description here



from Rotate xy points using multiple angles - python

No comments:

Post a Comment