[RESOLVED] Rotate Rectangle positioning maths
Hello, I was hoping to solve this one all by myself but I am stumped. Made loads of attempts at changing the code and cant work out for the life of me how to do it (Probably really simple)
I am rotating a rectangle to draw to the screen. At the moment I want to it rotate around its center so it spins on the spot. I have the code to rotate the rectangle I just cant center, it rotates around its top left corner :confused:
Code:
/*StartX, StartY, Width and Height are passed in as the grid coords for the start of the rectangle and its size. I want to rotate it around its StartX + (Width / 2) as this will be the center of the drawn rectangle
*/
int x;
int y;
int NewX;
int NewY;
for (x=0; x<Width; x++)
{
for (y=0; y < Height;y++)
{
//Rotate Points
NewX = (x * cos(Angle)) - (y * sin(Angle));
NewY = (x * sin(Angle)) + (y * cos(Angle));
//Write to buffer
GRIFFIN_DrawPixel(NewX + StartX, NewY + StartY, Color);
}
}
Anyone know what I am doing wrong :)
Thanks for looking ;)
Re: Rotate Rectangle positioning maths
Have you tried looping for x between -Width/2 and Width/2, and similarly for y between -Height/2 and Height/2?
Re: Rotate Rectangle positioning maths
Woohooo thank you. You dont know how many times I have comiled and tested that code to no avail. In fact I think the only code I didnt change was the for loops lol
Thankyou :o)
Re: [RESOLVED] Rotate Rectangle positioning maths
lol, no problem, glad it's working so quickly
Re: [RESOLVED] Rotate Rectangle positioning maths
The general recipe for rotating any polygon around a specific point is to move the coordinate system origin to this point, do the rotation and then restore the coordinate origin.
Namely, you first subtract the coordinates of the point from the coordinates of the polygon vertices, apply the rotation and add the point coordinates to those of the resulting vertices.