Sunday, November 27, 2011

program to show movement of circle along a line

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
// function for drawing circle of radius r having centre at m,n
void draw(int m,int n,int r)
{int x,y,p;
x=0;
y=r;
putpixel(m+y,n+x,1);
putpixel(m-y,n+x,1);
putpixel(m+x,n+y,1);
putpixel(m+x,n-y,1);
p=3-(2*r);
while(y>x)
{  if (p<0)
   p=(p+(4*x)+6);
   else
   { y=y-1;
     p=p+((4*(x-y)+10));
   }
   x++;
   putpixel(m+x,n+y,1);
   putpixel(m+x,n-y,1);
   putpixel(m-x,n-y,1);
   putpixel(m-x,n+y,1);
   putpixel(m-y,n-x,1);
   putpixel(m-y,n+x,1);
   putpixel(m+y,n+x,1);
   putpixel(m+y,n-x,1);
}
}

  void main()
     { int dx,dy,s1,s2,x1,x2,y1,y2,x,y,temp,e,i,c,r;
       cout<<"Enter the radius ";
       cin>>r;
      cout<<"enter the end points of line to move the circle along ( x1 x2 y1 y2 )\n";
      cin>>x1>>y1>>x2>>y2;
      int gdriver=DETECT,gmode;
      initgraph(&gdriver,&gmode,"");
      x=x1;y=y1;
      dx=abs(x2-x1);dy=abs(y2-y1);
      if(x2>x1)
    s1=1;
    else s1=-1;
      if(y2>y1)
      s2=1;
      else s2=-1;
  if(dy>dx)
  { temp=dx;
  dx=dy;
  dy=temp;
  c=1;
  }
  else c=0;
  e=2*dy-dx;
  for(i=1;i<=dx;i++)
  {  delay(40);
      clrscr();
      // calling function draw the circle
      draw(x,y,r);
      line(x1,y1,x,y);
    while(e>0)
     { if(c==1)
    { x+=s1;}
       else y=y+s2;
    e-=2*dx;
     }
    if(c==1)
     y+=s2;
    else
     x=x+s1;
  e+=2*dy;
  }
  getch();
}