Implementing Midpoint Circle Algorithm in C++

Here is C++ code for midpoint circle algorithm.

#include
#include
#include
#include
#include
#include
void main()
{
int gdriver=DETECT,gmode,errorcode;
initgraph(&gdriver,&gmode,”c:\\tc\\bgi”);
int xm=getmaxx()/2;
int ym=getmaxy()/2;
cout<<"\n Enter radius:"; int r; cin>>r;
int x=0;
int y=r;
float d=1.25-r;
while(x<=y) { putpixel(xm+x,ym-y,15); putpixel(xm+y,ym-x,15); putpixel(xm+y,ym+x,15); putpixel(xm+x,ym+y,15); putpixel(xm-x,ym+y,15); putpixel(xm-y,ym+x,15); putpixel(xm-y,ym-x,15); putpixel(xm-x,ym-y,15); if(d<0) { d+=(2*x)+3; } else { d=d+(2*(x-y))+5; y=y-1; } x=x+1; } getch(); closegraph(); } [/cpp] Output:

Output
Output

Leave a Reply

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