问题
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:
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.
Translate you drawing by
-M
, i.e., apply the transformationT(P) = P - M
to all pointsP
in your drawing. LetT(D)
be the translation ofD
.Now use the primitive to rotate
T(D)
by the desired anglephi
. LetR(T(D))
be the result.Now translate the previous result by
M
and get the rotated drawing. In other words, use the transformationT'(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
.
来源:https://stackoverflow.com/questions/35442860/rotate-line-around-center