In this program Hut is rotated about an axis parallel to the axis through one of the points on the hut. See the output to understand more clearly.
#include<graphics.h>
#include<math.h>
float x[5]={100,120,140,140,100};
float y[5]={100,80,100,140,140};
int i, j;
float d2r(float deg){
float rad = (M_PI*deg)/180.0;
return rad;
}
void rotate(float deg){
float rad = d2r(deg);
float x2,y2;
for(i=0;i<=4;i++)
{
x2 =(x[i]-320)*cos(rad)-(y[i]-240)*sin(rad) +320;
y2= (x[i]-320)*sin(rad)+(y[i]-240)*cos(rad)+240;
x[i]=x2;
y[i]=y2;
}
clearviewport();
}
void display(int c){
// moveto(x[0],y[0]);
for(i=0;i<4;i++)
line(x[i],y[i],x[i+1],y[i+1]);
line(x[i],y[i],x[0],y[0]);
}
void main()
{
int gd = DETECT, gm;
int i;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
setcolor(3);
while(!kbhit())
{
display(12);
delay(200);
rotate(320);
display(1);
}
getch();
closegraph();
}
#include<graphics.h>
#include<math.h>
float x[5]={100,120,140,140,100};
float y[5]={100,80,100,140,140};
int i, j;
float d2r(float deg){
float rad = (M_PI*deg)/180.0;
return rad;
}
void rotate(float deg){
float rad = d2r(deg);
float x2,y2;
for(i=0;i<=4;i++)
{
x2 =(x[i]-320)*cos(rad)-(y[i]-240)*sin(rad) +320;
y2= (x[i]-320)*sin(rad)+(y[i]-240)*cos(rad)+240;
x[i]=x2;
y[i]=y2;
}
clearviewport();
}
void display(int c){
// moveto(x[0],y[0]);
for(i=0;i<4;i++)
line(x[i],y[i],x[i+1],y[i+1]);
line(x[i],y[i],x[0],y[0]);
}
void main()
{
int gd = DETECT, gm;
int i;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
setcolor(3);
while(!kbhit())
{
display(12);
delay(200);
rotate(320);
display(1);
}
getch();
closegraph();
}
No comments:
Post a Comment