I have a function which delete the rotation of the geometry. That means, function aligns the principal axes of the geometry such that geometry's principal axes align parallel to the reference axes (X and Y).
I want my code in such a way that, if I give input (X and Y coordinate) of geometry, my function delete the rotation of geometry only if there is a rotation. If there is not a rotation, my function should do nothing.
I have a image before process as below,
For this image, sets coordinates are as
X = [2, 4, 4, 2, 3, 2, 4, 3, 1, 3, 4, 3, 1, 2, 0, 3, 4, 2, 0]
Y = [3, 4, 2, 1, 3, 2, 1, 0, 0, 2, 3, 4, 1, 4, 0, 1, 0, 0, 1]
My Code is as below,
from sbNative.debugtools import log, cleanRepr
from numpy import arctan, degrees
import math
def rotate(origin, point, angle):
"""
https://stackoverflow.com/questions/34372480/rotate-point-about-another-point-in-degrees-python
Rotate a point counterclockwise by a given angle around a given origin.
The angle should be given in radians.
"""
qx = origin.x + math.cos(angle) * (point.x - origin.x) - math.sin(angle) * (point.y - origin.y)
qy = origin.y + math.sin(angle) * (point.x - origin.x) + math.cos(angle) * (point.y - origin.y)
return qx, qy
@cleanRepr()
class Point:
def __init__(self, *pts):
pt_names = "xyzw"
for n, v in zip(pt_names[:len(pts)], pts):
self.__setattr__(n, v)
coordinates1 = list(zip(X, Y))
points1 = [Point(*coors) for coors in coordinates1]
point_pairs1 = [(points1[i], points1[i + 1]) for i in range(len(points1) - 1)] + \
[(points1[-1], points1[0])]
angles1 = [abs(math.atan2((p1.y - p2.y), (p1.x - p2.x))) for p1, p2 in point_pairs1]
angle_amounts1 = {}
for a in set(angles1):
angle_amounts1[a] = angles1.count(a)
final_rotation_angle1 = max(angle_amounts1, key=angle_amounts1.get)
new_points1 = [rotate(Point(0, 0), p2, final_rotation_angle1) for p2 in points1]
log(new_points1)
X1_coordinate_aligned, Y1_coordinate_aligned = zip(*new_points1)
X1_coordinate_aligned = [round(num,1) for num in X1_coordinate_aligned]
Y1_coordinate_aligned = [round(num,1) for num in Y1_coordinate_aligned]
df_1['X1_coordinate_aligned'] = X1_coordinate_aligned
df_1['Y1_coordinate_aligned'] = Y1_coordinate_aligned
plt.scatter(X1_coordinate_aligned, Y1_coordinate_aligned)
plt.show()
If the above mentioned coordinates are passed in the code given above, I got the plot of geometry like this,
My expected Output: I am expecting a geometry as it was before. Basically my aim is to delete the rotation of geometry if there is any. If there is no rotation, then I should get the similar geometry as it was before passing into the code given above.
Kindly let me know where I am doing wrong. If there is a better function to delete the rotation of geometry, the please provide that.