# Rectangle translation and rotation

I have a rectangle which is centered to $\ (1,2)$ like in the image below

rectangle first position

And I want to rotate and move the center to $\ (4,5)$ like in the image below

rectangle final position

I know that I have first to rotate it and then translate it. So it's the opposite $\ T*R$, where $\ T$ is the translation matrix and $\ R$ the rotation matrix.

Translation matrix :

\begin{bmatrix}1&0&0&3\\0&1&0&3\\0&0&1&0\\0&0&0&1\end{bmatrix}

It's the difference between final position and first position. My question is, how do I proceed from here because I am not really sure about the rotation table.

I tried to calculate the rotation table for 90 degrees.

\begin{bmatrix}0&-1&0&0\\1&0&0&0\\0&0&1&0\\0&0&0&1\end{bmatrix}

Based on \begin{bmatrix}cos(90)&-sin(90)&0&0\\sin(90)&cos(90)&0&0\\0&0&1&0\\0&0&0&1\end{bmatrix}

And the final Result of $\ T*R$ is:

\begin{bmatrix}0&-1&0&3\\1&0&0&3\\0&0&1&0\\0&0&0&1\end{bmatrix}

How does that result correspond to the final desired position?

• The rotation matrix you have rotates around the origin. Therefore try translating the rectangle to the origin first, then rotating it, and finally translating it to the place you want. Each of those three operations has a matrix, and multiply those matrices to get a single matrix for the combined operation. BTW, I don't see why you use 4x4 matrices. Those are normally for 3d rotations. You are not using the z-axis, so you can just leave out the third column and third row of all the matrices here and use 3x3 matrices instead. – Jaap Scherphuis Aug 16 '17 at 11:29
• @JaapScherphuis So If I understood it correct, it should look like this. Forgive me that I am answering with image but it will be easier to explain if I just show you. imgur.com/oxmU8hZ – valkongr Aug 16 '17 at 12:27
• The centre of the rectangle moves from (1,2) to the origin (0,0), the rotation keeps the centre there at (0,0), and in the last step you need to move it from (0,0) to (4,5). You did the last translation by the wrong amount. Other than that it seems good. – Jaap Scherphuis Aug 16 '17 at 12:41
• Actually, you also did not multiply the first two matrices correctly - the last column is wrong. You made the same mistake in the question above. – Jaap Scherphuis Aug 16 '17 at 12:50
• Is it because of the order? It should have been R*T? – valkongr Aug 16 '17 at 12:57

## 2 Answers

As has been pointed out in the comments, the rotation matrix that you built rotates about the origin, but you need to rotate the rectangle about some other point. A systematic way to deal with this would be to first translate the center of rotation to the origin, rotate, and then translate the result into its final position.

Since you’re specifying the rectangle positions by their centers, we’ll rotate about the center of the rectangle. So, we first translate by $(-1,-2)$, rotate 90° conterclockwise, as you’ve done, and translate to the final position: $$\begin{bmatrix}1&0&4\\0&1&5\\0&0&1\end{bmatrix}\begin{bmatrix}0&-1&0\\1&0&0\\0&0&1\end{bmatrix}\begin{bmatrix}1&0&-1\\0&1&-2\\0&0&1\end{bmatrix}=\begin{bmatrix}0&-1&6\\1&0&4\\0&0&1\end{bmatrix}.\tag1$$ We check: $$\begin{bmatrix}0&-1&6\\1&0&4\\0&0&1\end{bmatrix}\begin{bmatrix}2\\4\\1\end{bmatrix}=\begin{bmatrix}2\\6\\1\end{bmatrix}.$$ I’ll leave the other corners for you to verify.

You can save having to do one of the matrix multiplications by skipping the first translation. Applying the rotation to the rectangle rotates it about the origin—its lower-left corner—so the necessary translation to then apply is the one that takes this new center to the target. The rotated center is $(-2,1)$, so the necessary translation is now $(4,5)-(-2,1)=(6,4)$, which, not coincidentally, is the last column of the matrix derived in (1). To see why this might be so, you can write and multiply these matrices in block form: $$\left[\begin{array}{c|c}I_2&\mathbf c_2 \\ \hline \mathbf 0^T&1 \end{array}\right]\left[\begin{array}{c|c}R & \mathbf 0 \\ \hline \mathbf 0^T&1\end{array}\right]\left[\begin{array}{c|c}I_2&-\mathbf c_1 \\ \hline \mathbf 0^T&1\end{array}\right]=\left[\begin{array}{c|c}R & \mathbf c_2-R\mathbf c_1 \\ \hline \mathbf 0^T&1 \end{array}\right]=\left[\begin{array}{c|c}I_2 & \mathbf c_2-R\mathbf c_1 \\ \hline \mathbf 0^T&1 \end{array}\right]\left[\begin{array}{c|c}R & 0 \\ \hline \mathbf 0^T&1 \end{array}\right].$$ Here, $I_2$ is the $2\times2$ identity matrix, $R$ is the $2\times2$ rotation matrix, and $\mathbf c_1$ and $\mathbf c_2$ are the respective rectangle centers. In the process, we’ve also shown that a translate-rotate-translate sequence is equivalent to a rotate-translate sequence.

Now, there’s a quicker way to build the transformation matrix that you’re looking for by using an important property of transformation matrices: their columns are the images of the basis vectors, but that’s getting ahead of where you appear to be in this subject.

Note that since you haven’t specified how the vertices of the original rectangle correspond to those of the target, there are several other rigid motions (a.k.a. isometries) that will map one onto the other. For instance, you could rotate the rectangle in the opposite direction. That will also change the translation component of the transformation. Or, you could reflect the rectangle—flip it over—in various ways to get it aligned the right way.

Such 2D operations are much easier performed with complex numbers. Those $4\times4$ matrix are overkill.

Let $p=p=x+iy$ be a point that you want to transform.

You translate the rectangle to center it at the origin with

$$p'=p-(1+i2).$$

Then you rotate it counterclockwise around the origin by multiplying by $e^{i\theta}$, equal to $i$ in your case.

$$p''=i(p-(1+i2))=ip+2-i.$$

Finally, you translate the center to the new desired location

$$p'''=ip+2-i+(4+i5)=ip+6+i4.$$

In other terms,

$$\begin{cases}x'''=-y+6,\\y'''=x+4.\end{cases}$$