# Rotating an object around any origin

I want to extend my program that generates PDF and I need like to rotate an object (for example -30deg clockwise):

1: original 2: rotated object (origin is bottom left)

The first problem is, that the software can only rotate at the top left edge, so that I need move the resulting object:

The next step is to move 2 down so that it looks, as if the object is rotated around the bottom left edge

The second problem is, that the origin can be at any place inside the bounding boxes of the object, for example at 90% to the right and 20% to the bottom, similar to this:

Question

How can I calculate the movement in x and y direction?

(this is another angle as in the examples above)

That is: I have object number 1, rotate it around the left top edge (object 2, no problem yet), but I need to find the vector (black arrows) that moves the object 2 to get to object 3.

Manually I'd move the resulting object so that the origin of 2 is on the horizontal axis of the origin of 1:

So that I now have this situation ...

... and then horizontally towards the origin of the original object:

and voila, I have the result I need.

Now I need to express the shifting in terms of sin and cos, to do that programmatically.

I guess it is not feasible to ask for a solution, so I humbly ask for a hint, how to start. I do know basic trigonometry, but somehow my brain blocks.

• ... my math seems to be too low level to understand the answers. It will take some time until I will understand them and I'll report back! Aug 29, 2014 at 12:17

Any planar rigid motion can be represented by a rotation about the origin (or any other reference point) followed by a translation: $$\begin{split} \mathbb R^2&\to\mathbb R^2, \\ p&\mapsto Rp+t. \end{split}$$ where $$R=\left[ \begin{array}{cc} \cos\theta &-\sin\theta\\ \sin\theta & \cos\theta \end{array} \right], t\in\mathbb R^2.$$ This map becomes a linear map under the homogeneous representation: $$\left[ \begin{array}{c} p\\ 1 \end{array} \right]\mapsto \left[ \begin{array}{cc} R&t\\ 0&1 \end{array} \right]\cdot\left[ \begin{array}{c} p\\ 1 \end{array} \right]$$ So a pure rotation about the origin is given by: $$\left[ \begin{array}{cc} R&0\\ 0&1 \end{array} \right]$$ If a different reference point, say $q\in\mathbb R^2$ is chosen, the corresponding transformation matrix is given by: $$\left[ \begin{array}{cc} R&(I-R)q\\ 0&1 \end{array} \right]=\left[ \begin{array}{cc} I&q\\ 0&1 \end{array} \right]\cdot \left[ \begin{array}{cc} R&0\\ 0&1 \end{array} \right]\cdot \left[ \begin{array}{cc} I&-q\\ 0&1 \end{array} \right]$$ where $I$ is the $2\times 2$ identity matrix. Intuitively, you move the reference point $q$ to the origin, performs the rotation, and move it back. This is called a conjugation operation. The resulting rotation group is a conjugate group of the group of rotation about the origin.

• What is $I$ in the last equation? Aug 29, 2014 at 12:25
• @topskip I added it. Aug 29, 2014 at 12:27

Hint: In coordinates where the rotating point is at $\begin{pmatrix}x\\y\end{pmatrix}=\begin{pmatrix}0\\0\end{pmatrix}=O$

A rotation in 2d can be expressed by a rotational matrix $M\in SO(2)$ $$M(\phi)=\begin{pmatrix}\cos\phi&-\sin\phi\\\sin\phi&\cos\phi\end{pmatrix}$$

so the rotated coordinate is $$p\to p'=M(\phi)p$$

Thus movement therefore is $p-p'=\left(1-M(\phi)\right)p$

Some related hints from vector analysis:

1. Find co-ordinates of each corner and position vectors in $x + i y$ complex vector form.
2. Multiply each position vector by $e^{i \alpha}$ where $\alpha$ is angle through which entire object is to be rotated.
3. Edge vectors are position vector differences.
4. If any translation is required add a constant vector $a + i b$.