# Rotate line around center

I have to use a propriertary graphics-engine for drawing a line. I can rotate the whole drawing by its origin point (P1). What I want, is to rotate it around its center point(M). So basically that it looks like the green line (L_correct instead of L_wrong).

I think, it should be possible to correct it, by moving it from P1 to P2. But I cannot figure out what formula could be used, to determine the distance. It must probably involve the angle, with and height... Can anyone give me a hint?

## 2 Answers

Trick is, to move the entire object so that the point about which you want to rotate is at the center. Then rotate and after that counter move it so that the point is were it was.

In fact this is not so much of a trick, as such, nearly all graphics engines work this way. It is just abstracted away in many cases. Most often you will see it done in matrix notation like this:

$$T^{-1} R T$$

Where T is to transformation from world to pivot and R is the rotation matrix. Since it can be easily packaged this way you end up with a function called rotate about the pivot point. This is usually the reason why drawing API's allow for a transformation matrix.

Image 1: Rotate about a pivot point, image source available for investigation. Sub images in RTL reading order: Original, inverse transform, inverse and rotate, inverse rotate and move back.

I am a bit unsure if my calculations are correct, but on my scribbling paper it seemed to work out.

P1 and P2 lie on a circle around M.

This allows us to measure the distance of the two points by just taking the radius (which is half the line length) of the circle and constructing two rectangular triangles. The accepted answer here provides a sketch for this construction. So given your rotation angle is $\gamma$ and the line is $l$ long it follows:

$x = 2* sin(\frac{\gamma}{2}) * \frac{l}{2}$

The lines M to P1 and M to P2 form a isosceles triangle with the third side being the line segment between P1 and P2.

The angle at point M is known to be the rotation angle $\gamma$ so the remaining two angles in this triangle are given by

$\alpha = \frac{180 - \gamma}{2}$

And therefore the angle between the wrong line and the needed translation vector is just

$\beta = \alpha + \gamma$

So now you rotate a unit vector that starts along the wrong line.

You rotate it around P1 for $\beta$ and scale it to length x.

This yields the translation vector from P1 to P2. The rotation of the line should indeed be correct.

• Though in general it is far easier to just use the construction joojaa explained in his answer. – Dragonseel Feb 16 '16 at 23:27