-1

I have set of points (X and Y cooridnates) of 2D geometry which is rotated at certain angle. Angle is unknown. I want to make this geometry without rotation. I do not have clue how should I make it without rotation.

Respective coordinates of rotated geometry are as below and image also shows the rotated geometry,

x = [-2.38, -1.68, -0.97, -0.26, -0.97, -0.26, 0.45, 1.15, 1.86, 2.57, 1.86, 1.15, 0.45, -0.26, -0.97, -1.68]
y = [-1.24, -0.53, 0.18, 0.88, 1.59, 2.3, 1.59, 0.88, 0.18, -0.53, -1.24, -0.53, 0.18, -0.53, -1.24, -1.94]

Geometry's centroid is placed at (0, 0).

enter image description here

I want to make my geometry like in the picture below,

enter image description here

My question: I want to rotate my geometry so that parts' principal axes align with the axes of the coordinate system at origin. How can I do that? I could not find any way to make this geometry without rotation. Kindly give some suggestions to me and help me providing a code.

Urvesh
  • 331
  • 4
  • 15
  • 1
    Try to find lines containing most points using slope coefficients for points pairs. For large point count - you can use Hough transform – MBo Aug 30 '22 at 02:20
  • 1
    Cross-posted to https://math.stackexchange.com/q/4521368/35416 – MvG Aug 30 '22 at 13:13
  • 1
    Are the points guranteed to be in order? If yes it should be pretty easy lemme try – NoBlockhit Aug 30 '22 at 13:42
  • Hello, would you please try with different coordinates? I just realized that I wrote values of coordinates which I was not suppose to write. – Urvesh Aug 30 '22 at 13:44
  • 1
    Yes, The order of the points are in order. I have changed my post a bit. It would be very useful for me if you try. – Urvesh Aug 30 '22 at 13:55
  • @NoBlockhit Kindly let me know when you finish trying. This solution is something important for me and I am not getting any clue right now. – Urvesh Aug 30 '22 at 14:26
  • 1
    Ye, im not home atm, i will prob have it in an hour or so, cant gurantee that tho, this is a free service after all... – NoBlockhit Aug 30 '22 at 16:38
  • Hallo..Sind Sie Deutcher? – Urvesh Aug 31 '22 at 07:08

1 Answers1

1

Ok, I worked out some code, this seems to be working out for me, sbNative is just there for logging purpuses, you can install it or delete the affected lines. Btw im rotating it along the center at point 0,0. If you wish to change that do that in the function call of rotate, youll find it.

EDIT: This code is by no means optimized, it is just written very explicitly so its easy to understand, and i didnt bother speeding it up cuz its not terrible.

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)


x = [-2.38, -1.68, -0.97, -0.26, -0.97, -0.26, 0.45, 1.15, 1.86, 2.57, 1.86, 1.15, 0.45, -0.26, -0.97, -1.68]
y = [-1.24, -0.53, 0.18, 0.88, 1.59, 2.3, 1.59, 0.88, 0.18, -0.53, -1.24, -0.53, 0.18, -0.53, -1.24, -1.94]

coordinates = list(zip(x, y))

points = [Point(*coors) for coors in coordinates]
point_pairs = [(points[i], points[i + 1]) for i in range(len(points) - 1)] + \
              [(points[-1], points[0])]

angles = [abs(arctan((p1.x - p2.x) / (p1.y - p2.y))) for p1, p2 in point_pairs]

angle_amounts = {}
for a in set(angles):
    angle_amounts[a] = angles.count(a)

final_rotation_angle = max(angle_amounts, key=angle_amounts.get)

new_points = [rotate(Point(0, 0), p2, final_rotation_angle) for p2 in points]

log(new_points)

new_x_coors, new_y_coors = zip(*new_points)

import matplotlib.pyplot as plt

plt.scatter(new_x_coors, new_y_coors)
plt.show()

This is the result

NoBlockhit
  • 369
  • 2
  • 15
  • Hello, I would like to give you one update on this. When I tried the same code to make another geometry without rotation, I got the result which was still rotated at some angle. The set of coordinates are as below, X = [0.0, 0.87, 1.37, 1.87, 2.73, 3.6, 4.46, 4.96, 5.46, 4.6, 3.73, 2.87, 2.0, 1.5, 1.0, 0.5, 2.37, 3.23, 4.1] Y = [0.0, 0.5, -0.37, -1.23, -0.73, -0.23, 0.27, -0.6, -1.46, -1.96, -2.46, -2.96, -3.46, -2.6, -1.73, -0.87, -2.1, -1.6, -1.1] would you please try using these coordinates and let me know what should I do now? – Urvesh Sep 04 '22 at 21:30
  • Hello, would you please check your function by giving different input coordinates? X = [2, 4, 4, 2, 3, 2, 4, 3, 1, 3, 4, 3, 1, 2, 0, 3, 4, 2, 0] and Y = [3, 4, 2, 1, 3, 2, 1, 0, 0, 2, 3, 4, 1, 4, 0, 1, 0, 0, 1]. I checked it and your function does not make the geomery without rotation. Would you please let me know what should I do? – Urvesh Sep 06 '22 at 15:05