Tuesday 11 August 2015

Simulation of crank shaft mechanism



Sample Output Video

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<dos.h>
#include<graphics.h>


float d2r(float deg){
return  (M_PI*deg)/180;
}

void main(){

int gd= DETECT,gm,k=0;
float ang,x1,x2,x3,x4,y1,y2,y3,y4,c1,c2,xr,yr,xb,xa;
initgraph(&gd,&gm,"");
c1=425;c2=150;
x1=400;y1=150;
circle(c1,c2,25);
ang=d2r(2);
while(!kbhit()){
k+=1;
setcolor(RED);
rectangle(200-50,130,200+50,170);
xr =(x1-c1)*cos(ang) - (y1-c2)*sin(ang) + c1;
yr =(x1-c1)*sin(ang) + (y1-c2)*cos(ang) + c2;
x1=xr;
y1=yr;
xb=xr-sqrt(100*100-(150-yr)*(150-yr));
xa=xb-100;
setcolor(RED);
delay(20);
cleardevice();
setcolor(WHITE);
circle(c1,c2,25);
setcolor(YELLOW);
line(xa,150,xb,150);
setcolor(GREEN);
line(xb,150,xr,yr );


}
getch();

}

No comments:

Post a Comment