Calculating xy coordinates of a rectangle, that is rotated with a given rotation origin.

trigonometry

Lets say I draw a square on a grid of coordinates, with its corner points being |2,3|3,3|2,4|3,4|
Now lets introduce a rotation point for example 2,1 and an angle of 30*. With this rotation point I want to rotate the whole grid around it which includes our square.
Now that everything is rotated, on the old non-rotated grid, how would I calculate the new points for the square?

I need this to be repeatable, with the origin point being able to be moved on the plane (its only positive coordinates) and the initial square(or rectangle) coordinates to be also changed while the formula can remain the same.

Any help will be appreciated! Im writing a programm that will use this in the future if anyone is wondering. I know this is a lot of trigonometry, but im bad at maths and cant seem to figure it out even though it sounds simple when I say it out loud.

Best Answer

Rotating a point $P(x, y)$ about a center $C(x_0, y_0)$ by an counter clockwise angle $\theta$ results in a new point $P'(x', y')$, computable as follows:

$P' = C + R (P - C) $

where all the points in the above equation are $ 2 \times 1 $ column vectors, and the matrix $R$ is given by

$R = \begin{bmatrix} \cos \theta && -\sin \theta \\ \sin \theta && \cos \theta \end{bmatrix} $

Translating this concise equation explicitly, the new coordinates $x'$ and $y'$ are

given by:

$x' = x_0 + \cos \theta (x - x_0) - \sin \theta (y - y_0) $

$y' = y_0 + \sin \theta (x - x_0) + \cos \theta (y - y_0) $

Now you can use the above two equation four times: once for each corner of the square, and this will give you the new coordinates of the four corners of the rotated square.