Calculating Rotation from centroid I have a polygon as such:


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:


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

Thank you
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)
$$
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)$.