tags:

views:

69

answers:

2

My application allows rotating points around a center based on the mouse position. I'm basically rotating points around another point like this:

void CGlEngineFunctions::RotateAroundPointRad( const POINTFLOAT &center, const POINTFLOAT &in, POINTFLOAT &out, float angle )
{
 //x' = cos(theta)*x - sin(theta)*y
 //y' = sin(theta)*x + cos(theta)*y

 POINTFLOAT subtr;
 subtr.x = in.x - center.x;
 subtr.y = in.y - center.y;

 out.x = cos(angle)*subtr.x - sin(angle)*subtr.y;
 out.y = sin(angle)*subtr.x + cos(angle)*subtr.y;

 out.x += center.x;
 out.y += center.y;
}

where POINTFLOAT is simply

struct POINTFLOAT {
float x;
float y;
}

The issue is that the points need to be updated on mousemove. I am looking for a way to do this without doing the following:

Store original points
Rotate Original points
Copy result
Show Result
Rotate Original points
Copy result
Show Result....

I feel storing the originals seems messy and uses extra memory. Is there a way, knowing the previous rotation angle and the current one that I can somehow avoid applying the transformation to the original points all the time and just build upon the points i'm modifying?

*the center will never change until mouse up so thats not an issue

Thanks

+1  A: 

So, instead of storing the previous point, you'd rather store the previous angle?

Perhaps I can discourage you from doing that. Instead of keeping a struct of two floats, you'd keep a single float (angle), but add complexity to the application, for very little gain.

Revisit that code in 6 months and you'll wonder why you spent all that time fiddling with trigonometry just to save one float.

My 2¢. It is an interesting trig problem nonetheless :)

MPelletier
No, its not just to save 2 floats, it wouldnt be just 1 point, it might be thousands which means on mouse down copy thousands of points, then modify the originals on first mouse move, then recopy old ones, then modify them etc, its a lot...
Milo
Oh, it's for a polygon! I see. Then, yes, cHao has the right of it.
MPelletier
+1  A: 

If you subtract the old rotation from the new one, you'll get a value you should be able to use to rotate the already-modified points. Note that this requires storing the old rotation, and the coordinates will get less and less accurate the more you translate them around. It's close enough for government work, though.

cHao
So if ang was 3 and now its 4, I only need to rotate by 1 type thing?
Milo
@Milo: Right. That should work OK. Reread the part about the coordinates losing accuracy though -- if this is going to happen a lot with the same points, rounding errors might mess you up.
cHao
Would this margin of error every be visibly noticeable? I'm rotating polygons in a vector drawing application.
Milo
What I mean is will it get increasingly worse the longer I keep my mouse moving?
Milo
Yes. It'll slowly get worse. It'll start out unnoticeable, but the errors will accumulate slowly each time the points rotate. It should take a bunch of rotations for the error to become noticeable, though.
cHao