Friday, 29 June 2012

c++ program to draw circle using MID POINT Algorithm.

#include<iostream.h>
#include<conio.h>
#include<graphics.h>

class mid_circle
{
 public:
  int xc,yc,r;



 void input()
 {
  cout<<"\n enter radius :- ";
  cin>>r;
  cout<<"\n enter xcenter & ycenter :- ";
  cin>>xc>>yc;
 }

void ploatpoint(int x,int y)
 {
  putpixel(xc+x,yc+y,5);
  putpixel(xc-x,yc+y,5);
  putpixel(xc+x,yc-y,5);
  putpixel(xc-x,yc-y,5);
  putpixel(xc+y,yc+x,5);
  putpixel(xc-y,yc+x,5);
  putpixel(xc+y,yc-x,5);
  putpixel(xc-y,yc-x,5);
 }
 void draw_circle();
};//end of class

void mid_circle :: draw_circle()
 {
  int x,y;
  float p;
  x=0;
  y=r;
  ploatpoint(x,y);
  p=(5/4)-r;
  while(x<y)
  {
   x++;
   if(p<0)
    {
     p=p+(2*x)+1;
    }
   else
    {
     y--;
     p=p+(2*(x-y))+1;
    }
    ploatpoint(x,y);
   }
  }//end of function draw_circle

void main()
  {
   clrscr();
   mid_circle mc;
   int driver=DETECT,gmode;
   initgraph(&driver,&gmode,"");
   mc.input();
   mc.draw_circle();
   getch();
   }