Mid-Point Circle Generation Algorithm (C++)

Problem Definition: Write a program to implement Mid-Point Circle Generation Algorithm.

#include <graphics.h>
#include <iostream.h>
#include <conio.h>
void main(){
int xc,yc,x,y,p,r;
int gdriver = DETECT, gmode;
initgraph(&gdriver,&gmode,”C:\TC\BGI”);
cout<<“Enter co-ordinates of centre: “;
cin>>xc>>yc;
cout<<“Enter radius of circle: “;
cin>>r;
x = 0;
y = r;
p = 1-r;
do{
if(p<0){
x++;
p += 2*x +1;
}
else{
x++;
y–;
p += 2*x – 2*y +1;
}
putpixel(xc+x,yc+y,WHITE);
putpixel(xc+y,yc+x,WHITE);
putpixel(xc+x,yc-y,WHITE);
putpixel(xc+y,yc-x,WHITE);
putpixel(xc-x,yc-y,WHITE);
putpixel(xc-y,yc-x,WHITE);
putpixel(xc-x,yc+y,WHITE);
putpixel(xc-y,yc+x,WHITE);
}
while(x<=y);
getch();
closegraph();
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s