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..
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm,deg;
int left,top,right,bottom;
float rad,x1,y1,x2,y2,x3,y3,x4,y4;
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",°);
rad=deg*(3.14/180);
line(left,bottom,right,bottom);
line(left,bottom,left,top);
line(right,bottom,right,top);
line(left,top,right,top);
x1=left*cos(rad)-bottom*sin(rad);
y1=left*sin(rad)+bottom*cos(rad);
x2=right*cos(rad)-bottom*sin(rad);
y2=right*sin(rad)+bottom*cos(rad);
x3=left*cos(rad)-top*sin(rad);
y3=left*sin(rad)+top*cos(rad);
x4=right*cos(rad)-top*sin(rad);
y4=right*sin(rad)+top*cos(rad);
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...


Sign In
Create Account

Back to top









