0% found this document useful (0 votes)
139 views

6 Circle Algorithm

The document describes several algorithms for drawing circles in computer graphics: 1) A simple algorithm that uses the equation of a circle to calculate pixel positions on the circle centered at a given point. 2) An algorithm that uses polar coordinates to calculate pixel positions based on the radius and incrementing angle. 3) The midpoint circle algorithm, which samples points along the circle and uses the sign of a decision parameter to determine the next pixel position, avoiding square root calculations.

Uploaded by

rupak dangi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
139 views

6 Circle Algorithm

The document describes several algorithms for drawing circles in computer graphics: 1) A simple algorithm that uses the equation of a circle to calculate pixel positions on the circle centered at a given point. 2) An algorithm that uses polar coordinates to calculate pixel positions based on the radius and incrementing angle. 3) The midpoint circle algorithm, which samples points along the circle and uses the sign of a decision parameter to determine the next pixel position, avoiding square root calculations.

Uploaded by

rupak dangi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Computer Graphics Downloaded from: http://www.bsccsit.

com Circle Algorithms

Algorithm for Circle


Simple Algorithm:
The equation of circle centered at origin and radius r is given by x 2  y 2  r 2
 y   r 2  x2
Y
- Increment x in unit steps and determine corresponding value of y (x,y)
from the equation above. Then set pixel at position (x,y). r
- The steps are taken from –r to +r .
- In computer graphics, we take origin at upper left corner (0,0)
point on the display screen i.e. first pixel of the screen X
so any visible circle drawn would be centered at point other
than (0,0). If center of circle is (xc,yc) then the calculated
point from origin center should be moved to pixel position
by (x+xc, y+yc).
In general the equation of circle centered at (xc,yc) and radius r is

( 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);
}
}

Drawing circle using polar equations


If (x,y) be any point on the circle boundary with center (0,0) and radius r, then
x  r cos 
x  r sin 
i.e. ( x, y)  (r cos  , r sin  )
To draw circle using these co-ordinates approach, just increment angle starting from 0 to
360. Compute (x,y) position corresponding to increment angle. Which draws circle
centered at origin, but the circle centered at origin is not visible completely on the screen
since (0,0) is the starting pixel of the screen. If center of circle is given by (xc,yc) then
the pixel position (x,y) on the circle path will be computed as
x  xc  r cos 
y  yc  r sin 

Circle Function to draw circle using the polar transformation:

void drawcircle(int xc,int yc,int r)


{
int x,y;
float theta;
const float PI=3.14;

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

Symmetry in circle scan conversion:


We can reduce the time required for circle generation by using the symmetries in a circle
e.g. 4-way or 8-way symmetry. So we only need to generate the points for one quadrant
or octants and then use the symmetry to determine all the other points.

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.

Mid point circle Algorithm:


In mid point circle algorithm, we sample at unit intervals and determine the closest pixel
position to the specified circle path at each step.
For a given radius r, and screen center position ( xc, yc) , we can set up our
algorithm to calculate pixel positions around a circle path centered at (0,0) and then each
calculated pixel position ( x, y) is moved to its proper position by adding xc to x and yc
to y.
i.e. x = x + xc, y = y + yc.
To apply the mid point method, we define a circle function as:
f circle  x 2  y 2  r 2
To summarize the relative position of point ( x, y) by checking sign of f circle function,

<0, if ( x, y) lies inside the circle boundary


f circle ( x, y) =0, if ( x, y) lies on the circle boundary
>0, if ( x, y) lies outside the circle boundary.

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.

Where y k 1 is either y k or y k  1 depending upon sign of p k . and xk 1  xk  1


If p k is negative, y k 1 = y k so we get,
pk 1  pk  2 xk 1  1
If p k is positive, y k 1 = y k  1 so we get,
pk 1  pk  2 xk 1  1  2 y k 1
Where 2 xk 1  2 xk  2
2 y k 1  2 y k  2

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

Program for mid point circle algorithm in C

//mid point circle algorithm


#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
void drawpoints(int,int,int,int);
void drawcircle(int,int,int);

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");

/* read result of initialization */


errorcode = graphresult();
if (errorcode != grOk) /* an error
occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error
code */
}
printf("Enter the center co-ordinates:");
scanf("%d%d",&xc,&yc);
printf("Enter the radius");
scanf("%d",&r);
drawcircle(xc,yc,r);
getch();
closegraph();
}
void drawpoints(int x,int y, int xc,int yc)
{
putpixel(xc+x,yc+y,1);
putpixel(xc-x,yc+y,1);
putpixel(xc+x,yc-y,1);
putpixel(xc-x,yc-y,1);
putpixel(xc+y,yc+x,1);
putpixel(xc-y,yc+x,1);
putpixel(xc+y,yc-x,1);
putpixel(xc-y,yc-x,1);
}
void drawcircle(int xc,int yc,int r)
{
int p,x,y;
x=0;
y=r;
drawpoints(x,y,xc,yc);

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);
}
}

You might also like