Calculating Rotation from centroid

946 Views Asked by At

I have a polygon as such: enter image description here

where the green polygon is the rotated polygon and the purple is the extent of the polygon.

Is there a way to calculate the angle of rotation of the green polygon from the extent?

I am using python to do this as well, so if you have it in python code, that would be helpful too, but not needed.

I created the following code:

def distance(ptA, ptB):
    """ calculates theta """
    diffX = math.pow((ptB[0] - ptA[0]), 2)
    diffY = math.pow((ptB[1] - ptA[1]), 2)
    return math.sqrt((diffX + diffY))
def theta(p1, p2, p3):
    """
    """
    radians = math.atan((distance(ptA=p2, ptB=p3)/distance(ptA=p1, ptB=p3)))
    degree = radians * 360 / math.pi
    return degree

p1,p2, and p3 are [X,Y] pairs.

Now I am using the following point: labeled image

The result doesn't seem to be the correct rotation value. Any suggestions?

Thank you

4

There are 4 best solutions below

3
On

When the angles are 90 degrees.

Make sure that the left bottom co-ordinate is $(0,0)$.

Then you have $(x,0)$ and $(0,y)$ to form a triangle.

Then you have

$$ \tan(\phi) = \frac{y}{x} $$

thus

$$ \phi = \arctan\left( \frac{y}{x} \right). $$


When you have arbitrary points of the triangle, say

$$ (x_0,y_0), \quad (x_1,x_1), \quad (x_2,y_2) $$

you have

$$ \tan(\phi) = \frac { \sqrt{ \big( x_1 - x_0 \big)^2 + \big( y_1 - y_0 \big)^2 } } { \sqrt{ \big( x_2 - x_0 \big)^2 + \big( y_2 - y_0 \big)^2 } } $$

thus

$$ \phi = \arctan\left( \frac { \sqrt{ \big( x_1 - x_0 \big)^2 + \big( y_1 - y_0 \big)^2 } } { \sqrt{ \big( x_2 - x_0 \big)^2 + \big( y_2 - y_0 \big)^2 } } \right) $$


For the adjusted image.

Let

$$ \theta = \angle p_1 p_2 p_3 $$

Then

$$ \theta = \arctan\left( \frac{|p_1-p_3|}{|p_2-p_3|} \right) $$

0
On

The sides of the rectangle form two different angles with the sides of the square (the extent is a square). They sum $90$ degrees. We are to calculate both.

Let's fix our attention in the two vertices of the rectangle that are nearest from the left down corner. Let $(x_1,y_1)$ the coordinates of the vertex at the vertical side, and $(x_2,y_2)$ the same for the horizontal side.

One of the angles is $$\alpha=\arctan\left(\frac{y_2-y_1}{x_2-x_1}\right)$$

And the other is $90^\circ-\alpha$.

1
On

Rotated from what? Are we to assume that the lower left edge of the rectangle was initially at or parallel to the lower edge of the "extent"? If so then:

Set up a coordinate system with $(0, 0)$ at the lower left corner of the "extent", the $x$-axis along the lower edge, and the y-axis along the left edge. Let the $x$-coordinate where the rectangle touches the lower edge be $x_0$ and the $y$-coordinate where the rectangle touches the left edge be $y_0$. Then angle is given by $\operatorname{arccot}\left(\frac{y_0}{x_0}\right)$.

0
On

Centroids are the same for both the green and the purple rectangle, given the last is the extent of the former.

If you just have information from the purple one, your answer is: it is complicated. Try to visualize that when the green one is rotated by pi/4 rad, there are infinite possibilities for green rectangles within the very same extent purple rectangle. And how to identify if a green rectangle is rotated by pi/2 rad or not rotated at all (0 rad)?

Now, if you have information from the purple one AND any of the points from the green one, you could easily identify other points using this green point together with the purple centroid. This way I hope you'll be able to go through the entire process.

Please, note that I've exchanged "polygon" by "rectangle". The purple one will always be a rectangle due to its "extent" nature. Now, if you really meant a generic "polygon" for the inner (green) one instead of a rectangle, one additional point to be known will not be enough. Sorry if I couldn't get you any further.