Tuesday 4 August 2015

Rotation of two connected lines about their joints

This program shows two connected lines rotating in the opposite directions. 
NOTE: Change the path of initgraph() function as per your system's directory.
Computer Graphics, Rotation of two connected lines about their joints

Computer Graphics, C program for Rotation of two lines about about their joints



#include<graphics.h>
#include<dos.h>
#include<math.h>
#define midx 320
#define midy 240

float x1,x2,y1,y2,xr,yr;
int i=0;
float rad;


void rotate1(float deg){
rad=M_PI*deg/180.0;
xr=(x1-midx)*cos(rad)-(y1-midy)*sin(rad) + midx;
yr=(x1-midx)*sin(rad)+(y1-midy)*cos(rad) + midy;
x1=xr;
y1=yr;
xr=(x2-midx)*cos(rad)-(y2-midy)*sin(rad) + midx;
yr=(x2-midx)*sin(rad)+(y2-midy)*cos(rad) + midy;
x2=xr;
y2=yr;
delay(40);
cleardevice();
setcolor(GREEN);
line(midx,midy,x1,y1);
setcolor(RED);
line(x1,y1,x2,y2);
}

void rotate2(float deg){
cleardevice();
rad=M_PI*deg/180.0;
xr=(x2-x1)*cos(rad)+(y2-y1)*sin(rad) + x1;
yr=(y2-y1)*cos(rad)-(x2-x1)*sin(rad)+  y1;
x2=xr;
y2=yr;
delay(40);
cleardevice();
setcolor(GREEN);
line(midx,midy,x1,y1);
setcolor(RED);
line(x1,y1,x2,y2);
}


void main(){

int gd=DETECT,gm;
float deg;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
x1=midx+100;
y1=midy;
x2=midx+100;
y2=midy-50;
//deg=90;
line(midx,midy,x1,y1);
line(x1,y1,x2,y2);
//cleardevice();
while(!kbhit())
{
  rotate1(5);
  rotate2(20);
}

}

No comments:

Post a Comment