Tuesday, 16 July 2013

Draw a Circle Using Midpoint Circle algorithm


Draw a Circle Using Midpoint Circle algorithm


 
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void midptcircle(int,int,int);
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"d:\\turboc3");
int x1,y1,r;
cout<<"Enter the co-ordinates of the center of the circle:\n\n";
cout<<"x-axis:";
cin>>x1;
cout<<"y-axis:";
cin>>y1;
cout<<"\tEnter the radius of the circle:";
cin>>r;
midptcircle(x1,y1,r);
getch();
}
void midptcircle(int x1,int y1,int r)
{
int x=0;
int y=r;
int p=1-r;
putpixel(x1+x,y1+y,12);
putpixel(x1-x,y1+y,12);
putpixel(x1+x,y1-y,12);
putpixel(x1-x,y1-y,12);
putpixel(x1+y,y1+x,12);
putpixel(x1-y,y1+x,12);
putpixel(x1+y,y1-x,12);
putpixel(x1-y,y1-x,12);
while(x<y)
{
x+=1;
if(p<0)
{
p+=2*x+1;
}
else
{
y-=1;
p+=2*(x-y);
}


putpixel(x1+x,y1+y,12);
putpixel(x1-x,y1+y,12);
putpixel(x1+x,y1-y,12);
putpixel(x1-x,y1-y,12);
putpixel(x1+y,y1+x,12);
putpixel(x1-y,y1+x,12);
putpixel(x1+y,y1-x,12);
putpixel(x1-y,y1-x,12);
}
}


Output:
Enter the co-ordinates of the center of the circle:

x-axis:200
y-axis:300
     Enter the radius of the circle:50



       
 






























No comments: