Rotate line around center

969 views Asked by At

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 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, width and height...

So basically my question is, if there is a function to determine x2 and y2 based on my available data?

1

1

There are 1 answers

0
Leandro Caniglia On

Let's assume you have a primitive method that rotates a drawing by any given angle phi. What you want is to use that primitive to rotate a drawing D around a point M instead. Here is a sketch of how to proceed.

  1. Translate you drawing by -M, i.e., apply the transformation T(P) = P - M to all points P in your drawing. Let T(D) be the translation of D.

  2. Now use the primitive to rotate T(D) by the desired angle phi. Let R(T(D)) be the result.

  3. Now translate the previous result by M and get the rotated drawing. In other words, use the transformation T'(P) = P + M.

Note that in step 1 above M is mapped to the origin 0, where the rotation primitive is known to work. After rotating in step 2, the opposite translation of step 3 puts back the drawing on its original location as this time 0 is mapped to M.