Midpoint Circle Drawing Algorithm


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void cplot(int,int,int,int);
void main()
{
int gd=DETECT,gm;
int x,y,p,xc,yc,r;
initgraph(&gd,&gm,"");
cleardevice();
printf("x,y,r : ");
scanf("%d%d%d",&xc,&yc,&r);
x=0;y=r;
p=1-r;
cplot(xc,yc,x,y);
while(x<y)
{
x++;
if(p<0)
p+=2*x+1;
else
{
y--;
p+=2*(x-y)+1;
}
cplot(xc,yc,x,y);
}
getch();
}
void cplot(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,15);
putpixel(xc-x,yc+y,15);
putpixel(xc+x,yc-y,15);
putpixel(xc-x,yc-y,15);
putpixel(xc+y,yc+x,15);
putpixel(xc-y,yc+x,15);
putpixel(xc+y,yc-x,15);
putpixel(xc-y,yc-x,15);
}

Popular posts from this blog

Simple Animation Using C

Personal Diary - A Mini Project Written in Turbo C With Graphical Interface

Random Bouncing Ball Animation