6 Circle Algorithm
6 Circle Algorithm
( x xc) 2 ( y yc) 2 r 2
y yc r 2 ( x xc 2 ……………..(1)
Use this equation to calculate the position of points on the circle. Take unit step from xc-r
to xc+r for x value and calculate the corresponding value of y- position for pixel position
(x,y). This algorithm is simple but,
- Time consuming – square root and squares computations
- Non – uniform spacing , due to changing slope of curve. If non-uniform spacing is
avoided by interchanging x and y for slope |m|>y, this leads to more computation.
Following program demonstrates the simple computation of circle using the above
equation (1)
//program for circle
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<graphics.h>
#define SQUARE(x) ((x)*(x))
void drawcircle(int ,int,int);
void main()
{
int gd,gm,err;
int xc,yc,r;
gd=DETECT;
initgraph(&gd,&gm,"\\tc\\bgi");
err=graphresult();
if(err!=0)
{
printf("ERROR:%s",grapherrormsg(err));
printf("\nPress a key..");
getch();
exit(1);
1
Computer Graphics Downloaded from: http://www.bsccsit.com Circle Algorithms
}
xc=getmaxx()/2;
yc=getmaxy()/2;
r=50;
drawcircle(xc,yc,r);
getch();
closegraph();
}//end main
void drawcircle(int xc,int yc,int r)
{
int i,x,y,y1;
for(i=xc-r;i<=xc+r;i++)
{
x=i;
y=yc+sqrt(SQUARE(r)-SQUARE(x-xc));
y1=yc-sqrt(SQUARE(r)-SQUARE(x-xc));
putpixel(x,y,1);
putpixel(x,y1,1);
}
}
for(theta=0.0;theta<=360;theta+=1)
{
x= xc+r*cos(theta*PI/180.0);
y= yc+r*sin(theta*PI/180.0);
putpixel(x,y,1);
}
}
2
Computer Graphics Downloaded from: http://www.bsccsit.com Circle Algorithms
Y
Y (-y,x) (y,x)
● ●
(-x,y) ●
(x,y)
●
(-x,y) ● ● (x,y)
(0,0)
(0,0) ● ● X
X (-x,-y) (x,-y)
● ●(x,-y)
● ●
(-x,-y)
(-y,-x) (y,-x)
8-way symmetry
4-way symmetry
Problem of computation still persists using symmetry since there are square roots,
trigonometric functions are still not eliminated in above algorithms.
3
Computer Graphics Downloaded from: http://www.bsccsit.com Circle Algorithms
The circle function tests are performed for the mid positions between pixels near
the circle path at each sampling step. Thus the circle function is decision parameter in
mid point algorithm.
The figure, shows the mid point between the mid point
two candidate pixel at sampling position xk 1 , yk
Assuming we have just plotted the pixel ( xk , y k ),
yk-1
we next need to determine whether the pixel at
position ( xk 1, y k )or ( xk 1, y k 1) is closer to
the circle.
yk yk+1
Our decision parameter is circle function evaluated at the mid point
1
p k f circle ( xk 1, y k )
2
1
( xk 1) 2 ( y k 1 ) 2 r 2 ( xk 1) 2 y k y k r 2
2
2 4
If pk 0, then mid-point lies inside the circle, so point at y k is closer to boundary
otherwise, y k 1 closer to choose next pixel position.
Successive decision parameters are obtained by incremental calculation. The
decision parameter for next position is calculated by evaluating circle function at
sampling position xk 1 1 i.e. xk 2 as
1
p k 1 f circle ( xk 1 1, y k 1 )
2
1
{( xk 1 1)}2 ( y k 1 ) 2 r 2
2
1
( xk 1 ) 2 2 xk 1 1 ( y k 1 ) 2 ( y k 1 ) r 2
4
Now, pk 1 pk 2 xk 1 ( y 2 k 1 y k ) ( y k 1 y k ) 1
2
pk 1 pk 2 xk 1 ( y 2 k 1 y k ) ( y k 1 y k ) 1
2
i.e.
4
Computer Graphics Downloaded from: http://www.bsccsit.com Circle Algorithms
At the start position, (0,r), these two terms have the values 0 an 2r, respectively. Each
successive values are obtained by adding 2 to the previous value of 2x and subtracting 2
from previous value of 2y.
The initial decision parameter is obtained by evaluating the circle function at
starting position ( x0 , y0 ) (0, r ).
p0 f circle (1, r 1 )
2
1 (r 1 ) r 2
2
2
1
1 r2 r r2
4
5
r
4
If p 0 is specified in integer,
p 0 1 r.
The Algorithm:
1. Input radius r and circle centre ( xc , yc ), and obtain the first point on circle
centered at origin as.
( x0 , y0 ) (0, r ).
2. Calculate initial decision parameter
p0 5 r
4
3. At each x k position, starting at k 0, perform the tests:
If pk 0 next point along the circle centre at (0,0) is ( xk 1, y k )
pk 1 pk 2 xk 1 1)
Otherwise, the next point along circle is ( xk 1, y k 1)
pk 1 pk 2 xk 1 1 2 y k 1
Where 2 xk 1 2 xk 2. and 2 y k 1 2 y k 2.
4. Determine symmetry point on the other seven octants.
5. Move each calculated pixels positions ( x, y) in to circle path centered at
( xc , yc ) as
x x xc , y y y c
6. Repeat 3 through 5 until x y.
5
Computer Graphics Downloaded from: http://www.bsccsit.com Circle Algorithms
void main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int xc,yc,r;
/* initialize graphics and local
variables */
initgraph(&gdriver, &gmode, "\\tc\\bgi");
6
Computer Graphics Downloaded from: http://www.bsccsit.com Circle Algorithms
p=1-r;
while(x<y)
{
if(p<0)
{
x=x+1;
p=p+2*x+1;
}
else
{
x=x+1;
y=y-1;
p=p+2*(x-y)+1;
}
drawpoints(x,y,xc,yc);
}
}