Thursday, April 14, 2011

C implementation of Midpoint Circle Drawing Algorithm

Similar to previous post, following is the program for Midpoint Circle Drawing Algorithm in C.


PROGRAM CODE
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int gd=DETECT,gm,i,x,y,r,xmid,ymid;
float p;
void main()
{
clrscr();
printf("\nEnter radius of circle=");
scanf("%d",&r);
initgraph(&gd,&gm,"c:\\tc\\bgi");
x=0;
y=r;
p=1.25-r;
do
{
xmid=getmaxx()/2;
ymid=getmaxy()/2;

putpixel(xmid+x,ymid+y,15);
putpixel(xmid+y,ymid+x,15);
putpixel(xmid+x,ymid-y,15);
putpixel(xmid+y,ymid-x,15);
putpixel(xmid-x,ymid-y,15);
putpixel(xmid-x,ymid+y,15);
putpixel(xmid-y,ymid+x,15);
putpixel(xmid-y,ymid-x,15);
if(p<0)
{
x=x+1;
y=y;
p=p+2*x+1;
}
else
{
x=x+1;
y=y-1;
p=p+2*(x+1)+1-2*y;
}}while(x<y);
line(xmid,ymid,xmid+r,ymid);
char ch[20];
sprintf(ch,"%d",r);
outtextxy(xmid+(r/2)-10,ymid-10,ch);
outtextxy(getmaxx()-450,getmaxy()-20,"MidPoint Circle Algorithm :Program By 1st Kinecteer");
rectangle(getmaxx()-455,getmaxy()-25,getmaxx()-30,getmaxy()-7);
getch();
}

OUTPUT: