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

  • 1
    $\begingroup$ What are the input parameters? $\endgroup$ – johannesvalks Aug 11 '15 at 13:27
  • $\begingroup$ I know the coordinates of each vertice pt in the polygon and the extent. $\endgroup$ – code base 5000 Aug 11 '15 at 13:28
  • 1
    $\begingroup$ Given 8 co-ordinates means you know the co-ordinates of the triangles. Use cosinus law. $\endgroup$ – johannesvalks Aug 11 '15 at 13:30
  • $\begingroup$ cool, that's what I thought, thanks! $\endgroup$ – code base 5000 Aug 11 '15 at 13:31
  • 1
    $\begingroup$ I have worked it out using $\tan$... Good luck! $\endgroup$ – johannesvalks Aug 11 '15 at 13:38

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} $$


$$ \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 } } $$


$$ \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.


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


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

  • 2
    $\begingroup$ i seem to be getting the wrong theta, I added an additional image to try and better explain. $\endgroup$ – code base 5000 Aug 11 '15 at 14:55
  • $\begingroup$ I have added it to the post. $\endgroup$ – johannesvalks Aug 11 '15 at 15:51
  • 1
    $\begingroup$ You have made a small error in your code... Check the angle you got and check $90-\theta$. You have calculated $\angle p_2 p_1 p_3$... which is $90 - \theta$... $\endgroup$ – johannesvalks Aug 11 '15 at 15:54

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$.


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.


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


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.