views:

27

answers:

1

I am rotating an image around it center point but need to track a location on the image as it rotates.

Given:

  1. Origin at 0,0
  2. Image width and height of 100, 100
  3. Rotation point at C(50,50)
  4. Angle of "a" (say 90 degrees in this example)
  5. Point P starts at (25,25) and after the rotation it is at Pnew(75,25)

alt text

I haven't touched trig in 20 years but guessing the formula is simple...

+2  A: 

I haven't touched trig in 20 years but guessing the formula is simple...

Yep, fairly. To map the point (x1,y1) via rotation around the origin:

x2 = cos(a) * x1 + sin(a) * y1;
y2 = cos(a) * y1 - sin(a) * x1;

... so you just first need to translate to the origin i.e. (-50,-50) in your example and translate back after rotation.

x2 = cos(a) * (x1 - 50) + sin(a) * (y1 - 50) + 50;
y2 = cos(a) * (y1 - 50) - sin(a) * (x1 - 50) + 50;

... Or something like that. (You can generate a matrix which will do all three transformations, I'll leave that for another answer or as an exercise...)

davmac
Thanks much. At first I didn't think this was working correctly but alas, angles in geometry are effectively counter clockwise. My application treats all angles as clockwise. Just needed to add a couple of negative signs.
Andrew Robinson