Search This Blog

Rotation and Revolution of Two Wheels following a 'D' Shaped Path - Computer Graphics Experiment

This is a computer graphics lab experiment. The question was to animate both rotation and revolution of two wheels in opposite direction. Both the wheels move in a path in a of shape 'D'. If one moves in clockwise direction, the other moves in counter clockwise direction. The output of the program is as follows:
The program is as follows:


#include<stdio.h>
#include<graphics.h>
#include<math.h>
int theta1,theta2,xc,yc,ballradius=10;
float x1,y1,x2,y2,r=80;
float rad;
void drawD(int x0,int y0,int r)
{
line(x0,y0,x0,y0-r);
line(x0,y0,x0,y0+r);
arc(x0,y0,270,90,r);
}

void getPath1()//clockwise
{
if(x1<xc)
 {
 if(x1==xc-ballradius)
  {
  if(y1>yc-r)
   y1--;
  else
   x1++;
  }
 else if(x1>xc-ballradius)
  {
  if(y1<yc)
   x1++;
  else
   x1--;
  }
 }
else if(x1==xc)
 {
 if(y1<yc)
  x1=xc+1;
 else
  x1=xc-1;
 theta1=0;
 }
else {
 theta1+=1;
 if(theta1==180)
  {
  x1=xc;
  return;
  }
 rad=3.14f*theta1/180;
 y1=yc-r*cos(rad);
 x1=xc+r*sin(rad);
 }
}

void getPath2()//anti clockwise
{
if(x2==xc+ballradius)
 {
 if(y2<yc+r)
  y2++;
 else
  {
  x2++;
  theta2=10;
  }
 }
else if(x2>xc+ballradius)
 {
 theta2+=1;
 if(theta2==180-10)
  {
  x2=xc+ballradius;
  return;
  }
 rad=3.14f*theta2/180;
 y2=yc+r*cos(rad);
 x2=xc+r*sin(rad);
 }
}

void drawCircle(int x0,int y0)
{
static int theta=0;
int xp1,xp2,yp1,yp2,tt;
float rad;
theta=(theta+5)%360;
rad=3.14f*theta/180;
xp1=ballradius*cos(rad);
yp1=ballradius*sin(rad);
rad=3.14f*(theta+90)/180;
xp2=ballradius*cos(rad);
yp2=ballradius*sin(rad);
line(x0+xp1,y0+yp1,x0-xp1,y0-yp1);
line(x0+xp2,y0+yp2,x0-xp2,y0-yp2);
circle(x0,y0,ballradius);
}

void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI\\");
x1=xc=getmaxx()/2;
yc=getmaxy()/2;
y1=yc-r;
r=70;
x2=xc+ballradius;
y2=yc+r*cos(3.14f*10/180);


while(!kbhit())
{
r=80;
drawD(xc,yc,r);
r=90;//outer
getPath1();//outer
drawCircle(x1,y1);
r=70;
drawCircle(x2,y2);
getPath2();
delay(30);
cleardevice();
}


}

No comments: