1. ## Rectangle Rotation problem

Hello friends i am facing a typical problem..
The following is my code to rotate a rectangle. It works fine for 60 and 90 degrees of rotation but not for 30 and 45 degrees. I am unable to figure out where i have went wrong.. Please have a look at this..

Code:
```#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm,deg;
int left,top,right,bottom;
clrscr();
initgraph(&gd,&gm,"");
printf("Enter the co-ordinates of the rectangle :");
scanf("%d%d%d%d",&left,&top,&right,&bottom);
printf("\nEnter the angle to rotate :");
scanf("%d",&deg);

line(left,bottom,right,bottom);
line(left,bottom,left,top);
line(right,bottom,right,top);
line(left,top,right,top);

line(fabs(x1),fabs(y1),fabs(x2),fabs(y2));
line(fabs(x1),fabs(y1),fabs(x3),fabs(y3));
line(fabs(x3),fabs(y3),fabs(x4),fabs(y4));
line(fabs(x2),fabs(y2),fabs(x4),fabs(y4));
getch();
}```
Can you please guide me to rectify the error...

2. Z rotation is:

final.x = (vertex.x * cosf(angle)) + (vertex.y * sinf(angle));
final.y = (vertex.x * -sinf(angle)) + (vertex.y * cosf(angle));

Note that while you can rotate once using this formula, cumulative rotations will cause the vertices to get wacky. In order to transform vertices correctly you must always start in local space with a scale of 1.0f on all axes (to do correct scaling), and with the vertices centered around the local origin (to rotate around center). To rotate the object around other points in 3D space you will have to translate to that point and then rotate again.

3. ## Thanks for the reply

can you please give the code using the following formula to rotate??

xdash=x*cos(angle)-y*sin(angle);
ydash=x*sin(angle)+y*cos(angle);

4. Why when that is the wrong formula?

5. how to make the centre of the rectangle to be at the origin always??

6. If you want to rotate a rectangle in place (using the same center point), isn't that a fairly straight forward equation?

With a square, it's quite elementary. With a rectangle with len != width, it's a bit more difficult, but all you should need is one point of reference for the new rectangle, and everything can work out from there, without any other sin and cos calculations.

This is a rectangle, after all.

If it's not a true rectangle, then yeah, you need lots of math.

7. ok thanks for the info.. I am going to modify the program by fixing the center of the rectangle now...

8. Just use vertices and transformations. It is the simplest and works in all situations