Midpoint Circle Drawing Algorithm

Write a program to implement midpoint circle drawing algorithm.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void midpoint(int, int, int);
void plot(int, int, int, int);
int col=0;
void main()
{
	int x,y,r,gd=DETECT,gm;
	initgraph(&gd,&gm,"c:\\tc\\bgi");
	printf("\nEnter the center and radius of the circle: ");
	scanf("%d %d %d",&x,&y,&r);
	while(!kbhit())
	{
		midpoint(x,y,r);
		delay(500);
	}

	getch();
}

void midpoint(int xc, int yc, int r)
{
	int x=0,y=r,p=1-r;
	col=WHITE;
	plot(xc,yc,x,y);
	while(x<y)
	{
		if(p<0)
		{
			x++;
			plot(xc,yc,x,y);
			p=p+2*x+1;
		}
		else
		{
			x++;
			y--;
			plot(xc,yc,x,y);
			p=p+2*x+1-2*y;
		}
	}
}
void plot(int xc, int yc,int x,int y)
{

	putpixel(xc+x,yc+y,col);
	putpixel(xc+x,yc-y,col);
	putpixel(xc-x,yc+y,col);
	putpixel(xc-x,yc-y,col);
	putpixel(xc+y,yc+x,col);
	putpixel(xc+y,yc-x,col);
	putpixel(xc-y,yc+x,col);
	putpixel(xc-y,yc-x,col);
}



Output
1

Leave a Reply

Your email address will not be published. Required fields are marked *