Is this how rotation about a point is done?
Let's say I have a polygon with points:
(0,0)
(100,0)
(100,100)
(0,100)
Lets also let it's center be (50,50).
To rotate it would I add 50 to each component of each point, then do
x' = cos(theta)*x - sin(theta)*y
y' = sin(theta)*x + cos(theta)*y
Then subtract 50 from each component of eac开发者_运维问答h point?
Thanks
Thats right, except at the start you need to subtract 50 to bring the polygon center back to 0, then at the end add 50 to bring the polygon back to its orignal position.
Actually what you are using here is the rotation matrix in 2D:
You first have to translate (50, 50) to the origin, perform the rotation, and then translate the origin back to (50, 50).
More specifically, given matrices A, B, where A translates the center of rotation to the origin and usually takes the form:
|1 0 Sx| A = |0 1 Sy| |0 0 1 |
And B is your rotation matrix:
|cos θ -sin θ 0| B = |sin θ cos θ 0| | 0 0 1|
Then the acutal transformation you need for a vector p = <px py 1>
rotated about a point (Sx, Sy) by an angle θ is given by p' = ABA-1pT
. The extra dimension is needed in the matrices A and B and the vector p to account for the fact that since matrix multiplication is just a linear transformation, it results in the origin always being mapped to the origin. That means that we can't really do translation; the trick is to add an extra dimension and ensure that all transformations are on vectors away from the origin.
As Jason S. suggest, additional information can be found at Wikipedia's entry on Affine Transformations or Wolfram's entry on the same subject.
You should subtract 50 from each component (this causes (50,50) to be the center), do the rotation, and then add 50 to each component.
If you want to rotate around a point (call it center), you need to do the following transformations:
- Translate points from world positions into center relative positions.
- Rotate positions around the origin.
- Translate points from center relative points to world positions.
精彩评论