# How do I rotate a rectangle with the four vertices

## Recommended Posts

samsandeep_e    123

Hi,

I have the top left corner co ordinates of a rectangle.  And also the width and height.  Now, say, I want to rotate the rectangle 30 deg with the top left vertex as it s axis.  How do I calculate the four vertices.

I'm trying to do this in opengl without using the glRotatef function, but for some reason the texture gets distorted with the rectangle rotates.

I'm currently using this logic.

currentTopLeftX = Vertices[ 0 ].x;
currentTopLeftY = Vertices[ 0 ].y;

currentTopRightX = Vertices[ 0 ].x + w*cos(angle*PI/180);
currentTopRightY = Vertices[ 0 ].y + w*sin(angle*PI/180);

currentBottomRightX = Vertices[ 0 ].x + w*cos(angle*PI/180) - h*sin(angle*PI/180);
currentBottomRightY = Vertices[ 0 ].y + w*sin(angle*PI/180) - h*cos(angle*PI/180);

currentBottomLeftX = Vertices[ 0 ].x + h*sin(angle*PI/180);
currentBottomLeftY = Vertices[ 0 ].y + h*cos(angle*PI/180);

##### Share on other sites
Devroc    140
Equation for rotating a point around a center:
(x2,y2) = resulting rotated point
(x1,y1) = original point before rotation
(xc,yc) = center of rotation
x2 = xc+(x1-xc)*cos(angle)-(y1-yc)*sin(angle)
y2 = yc+(x1-xc)*sin(angle)+(y1-yc)*cos(angle)

##### Share on other sites
samsandeep_e    123

Thank you so much...

I'll try it out and let you know.. :D

##### Share on other sites
samsandeep_e    123
Equation for rotating a point around a center:
(x2,y2) = resulting rotated point
(x1,y1) = original point before rotation
(xc,yc) = center of rotation
x2 = xc+(x1-xc)*cos(angle)-(y1-yc)*sin(angle)
y2 = yc+(x1-xc)*sin(angle)+(y1-yc)*cos(angle)

Wow awesome.. it works...

Phew..  and it is so simple.. :|

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account