[Math] Calculate the new position of a rotated rectangle by the corner

anglerectanglesrotationstrigonometry

I am trying to calculate the new position of a rotated rectangle around the center.
But it is rotated by moving the bottom right corner upwards.
Here is an example.

enter image description here

The rectangle is moved by dragging the right bottom corner upwards, but the rectangle is rotated around its center.

enter image description here

So my question is :
How to find the new position of the right bottom corner of the rectangle after rotation ?
Thanks

Best Answer

Translate the rectangle so that the center is the origin, and then apply the appropriate rotation matrix. Let's say the rectangle has width $w$ and height $h$. Then if we translate so that the center is the origin, then the bottom right corner is now the point $\left( \frac{w}{2}, \, -\frac{h}{2} \right)$. Let's say you want to rotate counterclockwise by angle $\theta$. Then multiply by the matrix $\left( \begin{array}{cc} \cos(\theta) & -\sin(\theta) \\ \sin(\theta) & \cos(\theta) \end{array} \right)$ to do the rotation. The new rotated point is then:

$$ \frac{1}{2} \left(\begin{array}{cc} \cos(\theta)&-\sin(\theta)\\ \sin(\theta) & \cos(\theta) \end{array}\right) \left(\begin{array}{c} w \\ -h \end{array}\right) = \frac{1}{2} \left( \begin{array}{c} w\cos(\theta) + h\sin(\theta) \\ w\sin(\theta) - h\cos(\theta) \end{array}\right) $$

So the new rotated point is $\left( \frac{w\cos(\theta)+h\sin(\theta)}{2}, \, \frac{2\sin(\theta)-h\cos(\theta)}{2}\right)$. Now just translate back to the original position. If the center of the rectangle was $(a,b)$, then add $(a,b)$ to the point we got above.