Draw a circle using midpoint algorithm
and fill colors using boundaryfill
algorithm
#include<iostream.h>
#include<math.h>
#include<graphics.h>
void midptcircle(int,int,int);
void boundaryfill(int,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);
}
boundaryfill(x1,y1,5,12);
}
void boundaryfill(int x1,int y1,int fillcolor,int
boundarycolor)
{
int c=getpixel(x1,y1);
if((c!=boundarycolor)&&(c!=fillcolor))
{
putpixel(x1,y1,5);
boundaryfill(x1+1,y1,fillcolor,boundarycolor);
boundaryfill(x1-1,y1,fillcolor,boundarycolor);
boundaryfill(x1,y1+1,fillcolor,boundarycolor);
boundaryfill(x1,y1-1,fillcolor,boundarycolor);
}
}
output
x-axis:200
y-axis:300
Enter the
radius of the circle:50
No comments:
Post a Comment