Wednesday, February 8, 2012

W.A.P to implement Midpoint circle algorithm.


#include "graphics.h"
#include "stdlib.h"
#include "stdio.h"
#include "conio.h"
makeCircle(int,int,int,int);
int main(void)
{
   /* request auto detection */
   int gdriver = DETECT, gmode, errorcode;
   //int midx, midy;
   //int radius = 100;
   int R,x,y,cx,cy,d;

   /* initialize graphics and local variables */
   initgraph(&gdriver, &gmode, "");

   /* read result of initialization */
   errorcode = graphresult();
   if (errorcode != grOk)  /* an error occurred */
   {
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1); /* terminate with an error code */
   }

   /*
   midx = getmaxx() / 2;
   midy = getmaxy() / 2;
   setcolor(getmaxcolor());

   draw the circle
   circle(midx, midy, radius);
   */

   //MidPoint Circle algorithm:
   printf("enter co-ordinates for center");
   scanf("%d,%d",&cx,&cy);
   printf("enter the radius:");
   scanf("%d",&R);
   x=0,y=R;
   d=1-R;
   while(y>x)
   {

   makeCircle(cx,cy,x,y);

   if(d<0)
   {
    d=d+(2*x)+1;
   }
   else
   {
    d=d+(2*(x-y))+1;
    y--;
   }
   x++;

    makeCircle(cx,cy,x,y);
   }
   /* clean up */
   getch();
   closegraph();
   return 0;
}
makeCircle(int cx,int cy,int x,int y)
{
    putpixel(cx+x,cy+y);
    putpixel(cx-x,cy+y);
    putpixel(cx+x,cy-y);
    putpixel(cx-x,cy-y);
    putpixel(cx+y,cy+x);
    putpixel(cx-y,cy+x);
    putpixel(cx+y,cy-x);
    putpixel(cx-y,cy-x);
}

Download it from here: https://docs.google.com/viewer?a=v&pid=sites&srcid=ZGVmYXVsdGRvbWFpbnwwOWNlZ2l0fGd4OjY1MGQ1Y2NiYjc1ZWYxNDU