Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to rotate a 2D rectangle using a rotation matrix. After the rotation, I want the points (x, y) of the rectangle to be:

(625.49939, 632.40015) top left
(636.49939, 632.40015) top right
(636.49939, 672.40015) bottom right
(625.49939, 672.40015) bottom left

Before the rectangle is rotated, it appears 40 units wide and 11 units tall. After the rotation, it will appear 11 units wide and 40 units tall.

I know that I have to rotate it 90 degrees, but how can I find exactly where to place the rectangle before the rotation so that it ends up in my desired position?

  1. The rectangle starts in position ??? so that it appears 40 units wide and 11 units tall
  2. The rectangle will be rotated 90 degrees clockwise (at its center point).
  3. The rectangle ends with the coordinates listed above.

How do I find the starting coordinates?

share|cite|improve this question
up vote 2 down vote accepted

One way for you to do this would be to center in on the fact that the center point doesn't change in a rotation. So find the center of the rectangle. Let's call it $(x,y)$ (although it's very easy to find).

Then, make it so that you can pretend your rectangle is at the origin. That is, subtract the center coordinates from each of the corners, so that you get for instance $(625.49939 - x,632.40015 - y) $ as one of the corners.

Then you can apply the standard rotation matrices to these coordinates. Since you have the coordinates after the rotation, you would use the rotation matrix that rotates by $-90^\circ$. This will rotate the rectangle.

All that's left is to translate the rectangle back to where it should be. You do this by adding the center back to each of the coordinates.

Does that make sense? I can furnish more details if this isn't clear.

share|cite|improve this answer
Thanks for the tip about the center point! I think this solved my problem but I won't be sure until I experiment with it some more. It seems to be working for now, though! Thanks a lot! – Rick Apr 2 '12 at 0:12

The calculation follow the steps of @mixedmath:

Let's denote the points of the rectangle with

\begin{align*} A&=(625.49939,632.40015)=(x_A,y_A)\\ B&=(636.49939,632.40015)=(x_B,y_A)\\ C&=(636.49939,672.40015)=(x_B,y_C)\\ D&=(625.49939,672.40015)=(x_A,y_C)\\ \end{align*}

1. Step: Center $M$

We calculate the center $M$ of the rectangle $\Box(A,B,C,D)$: \begin{align*} M=\frac{1}{2}(A+C)=\frac{1}{2}(B+D)=\frac{1}{2}(x_A+x_B,y_A+y_C) \end{align*}

2. Step: Shift $M$ to $(0,0)$

We shift the rectangle so that $M$ becomes the origin and obtain a rectangle $\Box(A^\prime, B^\prime, C^\prime, D^\prime)$ \begin{align*} A^\prime&=A-M=\frac{1}{2}(x_A-x_B,y_A-y_C)\\ B^\prime&=B-M=\frac{1}{2}(-x_A+x_B,y_A-y_C)\\ C^\prime&=C-M=\frac{1}{2}(-x_A+x_B,-y_A+y_C)\\ D^\prime&=D-M=\frac{1}{2}(x_A-x_B,-y_A+y_C)\\ \end{align*}

Intermezzo: Rotation matrix

We consider the rotation matrix $\Phi$ clockwise through $90^\circ$ which corresponds to $-\frac{\pi}{2}$ \begin{align*} \Phi=\begin{pmatrix} \cos \phi&-\sin \phi\\ \sin \phi& \cos \phi \end{pmatrix} = \begin{pmatrix} \cos\left(-\frac{\pi}{2}\right)&-\sin\left( -\frac{\pi}{2}\right)\\ \sin\left( -\frac{\pi}{2}\right)& \cos\left( -\frac{\pi}{2}\right) \end{pmatrix} = \begin{pmatrix} 0&1\\ -1& 0 \end{pmatrix} \end{align*}

A point $P=(x_P,y_P)$ will be transformed via $\Phi$ to

\begin{align*} \Phi P=\begin{pmatrix} 0&1\\ -1& 0 \end{pmatrix} \begin{pmatrix} x_P\\ y_P \end{pmatrix} = \begin{pmatrix} y_P\\ -x_P \end{pmatrix} \end{align*}

3. Step: Rotate rectangle clockwise through $-\frac{\pi}{2}$

We now rotate the rectangle $\Box(A^\prime, B^\prime, C^\prime, D^\prime)$ and obtain a rectangle $\Box(\Phi A^\prime, \Phi B^\prime, \Phi C^\prime ,\Phi D^\prime)$ \begin{align*} \Phi A^\prime&=\frac{1}{2}(y_A-y_C,-x_A+x_B)\\ \Phi B^\prime&=\frac{1}{2}(y_A-y_C,x_A-x_B)\\ \Phi C^\prime&=\frac{1}{2}(-y_A+y_C,x_A-x_B)\\ \Phi D^\prime&=\frac{1}{2}(-y_A+y_C,-x_A-x_B)\\ \end{align*}

4. Step: Shift rectangle back from origin to $M$

We reverse step 2 by adding $M$ to the points of $\Box(\Phi A^\prime, \Phi B^\prime, \Phi C^\prime ,\Phi D^\prime)$ \begin{align*} \Phi A^\prime+M&=\frac{1}{2}(x_A+x_B+y_A-y_C,-x_A+x_B+y_A+y_C)=(610.99939,657.90015)\\ \Phi B^\prime+M&=\frac{1}{2}(x_A+x_B+y_A-y_C,x_A-x_B+y_A+y_C)=(610.99939,646.90015)\\ \Phi C^\prime+M&=\frac{1}{2}(x_A+x_B-y_A+y_C,x_A-x_B+y_A+y_C)=(650.99939,646.90015)\\ \Phi D^\prime+M&=\frac{1}{2}(x_A+x_B-y_A+y_C,-x_A-x_B+y_A+y_C)=(650.99939,657.90015)\\ \end{align*}

Note: A code can be easily derived from the calculations

[2015-07-28] Add-on: According to @BROYs comment here's the rotational displacement of the rectangle using homogeneous matrices.

A translational displacement by a vector $U=\begin{pmatrix}x_U\\y_U\end{pmatrix}$ can be represented by the homogeneous matrix $T_U$ with \begin{align*} T_U=\begin{pmatrix} 1&0&x_U\\ 0&1&y_U\\ 0&0&1\\ \end{pmatrix} \end{align*}

The point $A=(x_A,y_A)$ in homogeneous coordinates is $\begin{pmatrix}x_A\\y_A\\1\end{pmatrix}$. The translation of $A$ in direction $U$ is \begin{align*} T_UA=\begin{pmatrix} 1&0&x_U\\ 0&1&y_U\\ 0&0&1\\ \end{pmatrix} \begin{pmatrix} x_A\\ y_A\\ 1 \end{pmatrix} = \begin{pmatrix} x_A+x_U\\ y_A+y_U\\ 1 \end{pmatrix} \end{align*} Observe $T_UA$ corresponds to $A+U$.

A rotational displacement by a vector $\Phi=-\frac{\pi}{2}$ can be represented by the homogeneous matrix $R_\Phi=R_{-\frac{\pi}{2}}$ with \begin{align*} T_\Phi&=\begin{pmatrix} \cos(\Phi)&-\sin(\Phi)&0\\ \sin(\Phi)&\cos(\Phi)&0\\ 0&0&1\\ \end{pmatrix}\\ T_{-\frac{\pi}{2}}&= \begin{pmatrix} 0&1&0\\ -1&0&0\\ 0&0&1\\ \end{pmatrix} \end{align*}

The rotation of $A$ through an angle $\Phi=-\frac{\pi}{2}$ \begin{align*} T_{-\frac{\pi}{2}}A= \begin{pmatrix} 0&1&0\\ -1&0&0\\ 0&0&1\\ \end{pmatrix} \begin{pmatrix} x_A\\ y_A\\ 1 \end{pmatrix} = \begin{pmatrix} y_A\\ -x_A\\ 1 \end{pmatrix} \end{align*}

Note: In the following it's more convenient to use vector notation for the points. I will omit the transpose symbol.

Homogeneous matrices in action

The players: The four points of the rectangle $A,B,C,D$ and the center $M$ of the rectangle

\begin{align*} A&=\begin{pmatrix} x_A\\ y_A\\ 1 \end{pmatrix} \qquad B=\begin{pmatrix} x_B\\ y_B\\ 1 \end{pmatrix} \qquad C=\begin{pmatrix} x_C\\ y_C\\ 1 \end{pmatrix} \qquad D=\begin{pmatrix} x_D\\ y_D\\ 1 \end{pmatrix}\\ M&=\frac{1}{2}(A+C)=\frac{1}{2}(B+D)= \frac{1}{2}\begin{pmatrix} x_A+x_B\\ y_A+y_C\\ 1 \end{pmatrix} \end{align*}

First we want to move $M$ to the origin. This is a translational displacement by $-M$ represented by the homogeneous matrix $T_{-M}$. Next we rotate through $-\frac{\pi}{2}$ represented by $R_{-\frac{\pi}{2}}$ and at the end we shift back by $T_M$. The complete displacement is given by

\begin{align*} T_M R_{-\frac{\pi}{2}} T_{-M}&= \begin{pmatrix} 1&0&\frac{1}{2}(x_A+x_B)\\ 0&1&\frac{1}{2}(y_A+y_C)\\ 0&0&1 \end{pmatrix} \begin{pmatrix} 0&1&0\\ -1&0&0\\ 0&0&1\\ \end{pmatrix} \begin{pmatrix} 1&0&-\frac{1}{2}(x_A+x_B)\\ 0&1&-\frac{1}{2}(y_A+y_C)\\ 0&0&1 \end{pmatrix}\\ &= \begin{pmatrix} 1&0&\frac{1}{2}(x_A+x_B)\\ 0&1&\frac{1}{2}(y_A+y_C)\\ 0&0&1 \end{pmatrix} \begin{pmatrix} 0&1&-\frac{1}{2}(y_A+y_C)\\ -1&0&-\frac{1}{2}(x_A+x_B)\\ 0&0&1 \end{pmatrix}\\ &= \begin{pmatrix} 0&1&-\frac{1}{2}(x_A+x_B-y_A-y_C)\\ -1&0&-\frac{1}{2}(x_A+x_B+y_A+y_C)\\ 0&0&1 \end{pmatrix} \end{align*}

The complete displacement of the point $A$ is obtained by

\begin{align*} T_MR_{-\frac{\pi}{2}}T_{-M}A&=T_MR_{-\frac{\pi}{2}}T_{-M}\begin{pmatrix} x_A\\ y_A\\ 1 \end{pmatrix}= \frac{1}{2}\begin{pmatrix} x_A+x_B+y_A-y_C\\ -x_A+x_B+y_A+y_C\\ 2 \end{pmatrix}= \begin{pmatrix} 610.99939\\ 657.90015\\ 1 \end{pmatrix} \end{align*}

and similarly for $B,C,D$.

share|cite|improve this answer
@BROY: Calculation based upon homogeneous matrices added – Markus Scheuer Jul 28 '15 at 9:15
@BROY: Thanks for the bounty. Best regards, – Markus Scheuer Aug 3 '15 at 19:48

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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