Header Ads

Bresenham's circle drawing algorithm

Circles have the property of being highly symmetrical, which is handy when it comes to
drawing them on a display screen.
Bresenham's circle algorithm calculates the locations of the pixels in the first 45 degrees. It
assumes that the circle is centered on the origin. So for every pixel (x,y) it calculates we draw a pixel in each of the 8 octants of the circle :
putPixel(CenterX + X, Center Y + Y)
putPixel(CenterX + X, Center Y - Y)
putPixel(CenterX - X, Center Y + Y)
putPixel(CenterX - X, Center Y - Y)
putPixel(CenterX + Y, Center Y + X)
putPixel(CenterX + Y, Center Y - X)
putPixel(CenterX - Y, Center Y + X)
putPixel(CenterX - Y, Center Y - X)

Input to the Algorithm


(xc, yc): Integer, Centre Co-ordinates
r: Integer, Radius of circle

Output of the Algorithm


Turning ON the pixels on circular boundary.

Data Variables


(xc,yc) : Integer, representing center co-ordinates.
r : Integer, radius of the circle
d : Integer, decision variable.
x,y : Integer, points used for turning ON the pixel.

Algorithm:


 Step 1.  Start
Step 2. x = 0
Step 3. y = r; { Initial point on the circle}
Step 4. d = 3 - 2 * r; { Initialise decision variable}
Step 5. if (x > y ) go to step no 17
Step 6. Plot(xc + x, yc +y);
Step 7. Plot(xc - x, yc + y);
Step 8. Plot(xc + x, yc - y);
Step 9. Plot(xc - x, yc - y);
Step 10. Plot(xc + y, yc + x);
Step 11. Plot(xc - Y,yc + x);
Step 12. Plot(xc +y, yc - x);
Step 13. Plot(xc - Y,yc - x); { Plot eight symmetric points}
Step 14. If (d > = 0) Then
{
d = d + 4 * (x - y) + 10;
y = y-1;
}
else
{
d = d + 4 * x + 6;
}
Step 15. x = x + 1
Step 16. go to step no 5
Step 17. stop.

Program:


#include<iostream>
#include<graphics.h>
using namespace std;

class pix
{
public: int x1,y1,r;
public:
void pix1(int x1,int y1)
{
putpixel(x1,y1,15);
}
};

class cir:public pix
{
public:
void circ()
{
cout<<"\n Enter Co-ordinates of Center :: ";
cin>>x1>>y1;
cout<<"\n\n Enter radius :: ";
cin>>r;
int gd=DETECT, gm=0,d,x,y;
initgraph(&gd,&gm,NULL);
d=3-2*r;
x=0;
y=r;
do{
pix1(x1+x,y1+y);
pix1(x1+y,y1+x);
pix1(x1+y,y1-x);
pix1(x1+x,y1-y);
pix1(x1-x,y1-y);
pix1(x1-y,y1-x);
pix1(x1-y,y1+x);
pix1(x1-x,y1+y);

if(d<0)
d= d+(4*x)+6;
else
{
d=d+(4*(x-y))+10;
y=y-1;
}
x=x+1;
}while(x<y);
getch();
closegraph();
}
};

int main()
{
cir c1;
c1.circ();
return 0;
}

Learn how to run C/C++ graphics programs on Ubuntu here

No comments:

Powered by Blogger.