W.A.P to implement Midpoint circle algorithm.

Post date: Feb 07, 2012 6:45:5 PM

#include <graphics.h>

#include <stdlib.h>

#include <stdio.h>

#include <conio.h>

void 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,2);

putpixel(cx-x,cy+y,2);

putpixel(cx+x,cy-y,2);

putpixel(cx-x,cy-y,2);

putpixel(cx+y,cy+x,2);

putpixel(cx-y,cy+x,2);

putpixel(cx+y,cy-x,2);

putpixel(cx-y,cy-x,2);

}