Drawing a circle on the screen is a little complex than drawing a line. There are two popular algorithms for generating a circle − Bresenham’s Algorithm and Midpoint Circle Algorithm. These algorithms are based on the idea of determining the subsequent points required to draw the circle. Let us discuss the algorithms in detail
The equation of circle is where r is radius.

Bresenham’s Algorithm
We cannot display a continuous arc on the raster display. Instead, we have to choose the nearest pixel position to complete the arc.
From the following illustration, you can see that we have put the pixel at (X, Y) location and now need to decide where to put the next pixel − at N (X+1, Y) or at S (X+1, Y-1).

This can be decided by the decision parameter d.
- If d <= 0, then N(X+1, Y) is to be chosen as next pixel.
- If d > 0, then S(X+1, Y-1) is to be chosen as the next pixel.
Algorithm
Step 1 − Get the coordinates of the center of the circle and radius, and store them in x, y, and R respectively. Set P=0 and Q=R.
Step 2 − Set decision parameter D = 3 – 2R.
Step 3 − Repeat through step-8 while P ≤ Q.
Step 4 − Call Draw Circle (X, Y, P, Q).
Step 5 − Increment the value of P.
Step 6 − If D < 0 then D = D + 4P + 6.
Step 7 − Else Set R = R - 1, D = D + 4(P-Q) + 10.
Step 8 − Call Draw Circle (X, Y, P, Q).
// Using Bresenham's algorithm
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int main()
{
int gd=DETECT,gm;
int r,x,y,p,xc,yc,midx,midy;
initgraph(&gd,&gm," ");
cleardevice();
midx=(getmaxx()/2);
midy=(getmaxy()/2);
line(0,midy,getmaxx(),midy);
line(midx,0,midx,getmaxx());
printf("Enter the center of circle");
scanf("%d%d",&xc,&yc);
printf("Enter the radius ");
scanf("%d",&r);
x=0;
y=r;
putpixel(xc+x,yc-y,WHITE);
p=3-(2*r);
for(x=0;x<=y;x++)
{ // check for decision parameter and correspondingly update d, x, y
if (p<0)
{
p=(p+(4*x)+6);
}
else
{
y=y-1;
p=p+((4*(x-y)+10));
}
// Function to put pixels at subsequent points
putpixel(midx+xc+x,midy-(yc-y),WHITE);
putpixel(midx+xc-x,midy-(yc-y),WHITE);
putpixel(midx+xc+x,midy-(yc+y),WHITE);
putpixel(midx+xc-x,midy-(yc+y),WHITE);
putpixel(midx+xc+y,midy-(yc-x),WHITE);
putpixel(midx+xc-y,midy-(yc-x),WHITE);
putpixel(midx+xc+y,midy-(yc+x),WHITE);
putpixel(midx+xc-y,midy-(yc+x),WHITE);
}
getch();
closegraph();
return 0;
}
#include<conio.h>
#include<graphics.h>
#include<math.h>
int main()
{
int gd=DETECT,gm;
int r,x,y,p,xc,yc,midx,midy;
initgraph(&gd,&gm," ");
cleardevice();
midx=(getmaxx()/2);
midy=(getmaxy()/2);
line(0,midy,getmaxx(),midy);
line(midx,0,midx,getmaxx());
printf("Enter the center of circle");
scanf("%d%d",&xc,&yc);
printf("Enter the radius ");
scanf("%d",&r);
x=0;
y=r;
putpixel(xc+x,yc-y,WHITE);
p=3-(2*r);
for(x=0;x<=y;x++)
{ // check for decision parameter and correspondingly update d, x, y
if (p<0)
{
p=(p+(4*x)+6);
}
else
{
y=y-1;
p=p+((4*(x-y)+10));
}
// Function to put pixels at subsequent points
putpixel(midx+xc+x,midy-(yc-y),WHITE);
putpixel(midx+xc-x,midy-(yc-y),WHITE);
putpixel(midx+xc+x,midy-(yc+y),WHITE);
putpixel(midx+xc-x,midy-(yc+y),WHITE);
putpixel(midx+xc+y,midy-(yc-x),WHITE);
putpixel(midx+xc-y,midy-(yc-x),WHITE);
putpixel(midx+xc+y,midy-(yc+x),WHITE);
putpixel(midx+xc-y,midy-(yc+x),WHITE);
}
getch();
closegraph();
return 0;
}
Output:
Comments
Post a Comment