Rotating all sides of a polygon around its centre such that one of its sides are parallell to the x-axis

295 Views Asked by At

I am currently trying to create an algorithm for optimal packing inside arbitrary polygons. In order to discretize the polygon in a set of points, I wish to rotate the polygon such that its longest side is parallel to an axis. This is because it is easier to generate points along an axis through built in functions in python. The result of rotation will hopefully be to have more control of the points generated inside the polygon with low effort.

I have a list of vertices in a polygon ((x1,y1),(x2,y2),...,(xn,yn)). I have found the polygons centroid and am able to rotate it using a rotation matrix as explained in this post: Rotate polygon around center and get the coordinates

However, I seem unable to find the correct angle of rotation such that at least one of its sides are parallel with the x-axis.

My question is, how can I find the correct angle to use in a rotation matrix such that one side of the polygon is parallel to the x-axis.

1

There are 1 best solutions below

0
On BEST ANSWER

The slope of one of the sides is given by $$\text{slope} = \tan \theta = \frac{y_2-y_1}{x_2-x_1} $$ So the required angle is $$\arctan \frac{y_2-y_1}{x_2-x_1} $$