Circle & Ellipse Drawing
Algorithms
Assoc. Prof. Mohamed AbdelNasser
Circle Drawing
Since the circle is a frequently used component in
pictures and graphs, a procedure for generating
either full circles or circular arc is included in most
graphics packages.
Direct Algorithm
The equation for a circle is:
                    x2 + y2 = r2
Where r is the radius of the circle. So, we can write
a direct circle drawing algorithm by solving the
equation for y at unit x intervals using:
                  y = (r2 – x2)1/2       2
Direct Algorithm
To draw a circle with radius=20
 y(0) = (202 – 02)1/2  20
 y(1) = (202 – 12)1/2  20
 y(2) = (202 – 22)1/2  20
  …..
 y(19) = (202 – 192)1/2  6
 y(20) = (202 – 202)1/2 = 0
the resulting circle has large
gaps where the slope
approaches the vertical. And
the calculations are not very
efficient:
            ✓ The square (multiply) operations
            ✓ The square root operation
                                                 3
Eight-Way Symmetry
The first thing we can notice to make our circle
drawing algorithm more efficient is that circles
centred at (0, 0) have eight-way symmetry
                                    4
Mid-Point Circle Algorithm
As in the raster line 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 first set up our algorithm to
calculate pixel positions around a circle path
centered at the coordinate (0, 0).
• Then each calculated position (x, y) is moved to
its proper screen position by adding xc to x and yc
to y.
                                       5
Mid-Point Circle Algorithm
• Along the circle section from x = 0 to x = y in the
first quadrant, the slope of the curve varies from 0
to –1. Therefore, we can take unit steps in the
positive x direction over this octant and use a
decision parameter to determine which of the two
possible y positions is closer to the circle path at
each step positions in the other seven octants are
then obtained by symmetry.
• To apply the midpoint algorithm, we define a circle
function:
             fcircle(x, y) = x2 + y2 – r2
                                       6
Mid-Point Circle Algorithm
• The relative position of any point (x, y) can be
determined by checking the sign of the on the
boundary of the circle function:
              <0       If (x, y) is inside the circle boundary
fcircle(x, y) = 0      If (x, y) is on the circle boundary
           >0       If (x, y) is outside the circle boundary
The circle function tests are performed for the midpoints
positions between pixels near the circle path at each
sampling step. Thus, circle function is decision parameter in
the midpoint algorithm, and we can set up incremental
calculations for this function as we did in the line algorithm.
                                                 7
Mid-Point Circle Algorithm
Assuming we have just plotted point (xk, yk), we next need
to determine whether the pixel at position (xk+1, yk) or the
one at position (xk+1, yk–1) is closer to the circle path. Our
decision parameter is The circle function evaluated at the
midpoint between these two pixels:
                                              8
Mid-Point Circle Algorithm
                pk = fcircle(xk + 1,yk – 1/2)
                     =(xk + 1)2 + (yk – 1/2)2 – r2
• If pk < 0, this midpoint is inside the circle and the pixel at
yk is closer to the circle boundary.
• Otherwise the midpoint is outside or on the circle
boundary, and we select the pixel at yk–1.
✓ Successive decision parameters are obtained using
incremental calculations. We obtain a recursive expression
for the next decision parameter by evaluating the circle
function at sampling position xk+1 + 1 = xk + 2
                pk+1 = fcircle(xk+1 + 1,yk+1 – 1/2)
                     = ((xk + 1) + 1)2 + (yk – 1/2)2 – r2
                                                 9
Mid-Point Circle Algorithm
pk+1 = pk + 2(xk+1) + (yk +12 – yk 2 ) – (yk +1 – yk) + 1
Where yk+1 is either yk or yk-1 depending on the sign of pk
Increments for obtaining pk+1 are either 2xk+1 + 1 (if pk is
negative) or 2xk+1 + 1 – 2yk+1.
Evaluation of the terms 2xk+1 and 2yk+1 can also be done
incrementally as:
                    2xk+1 = 2xk+1 + 2
                    2yk+1 = 2yk+1 – 2
At the starting position (0, r), these two terms have the
values 0 and 2r, respectively. Each successive value is
obtained by adding 2 to the previous value of 2x and
subtracting 2 from the previous value of 2y.10
Mid-Point Circle Algorithm
The initial value of the decision parameter is obtained by
evaluating the circle function at the start position
(x0, y0) = (0, r)
        P0 =fcircle(1, r – 1/2)=1 + (r – 1/2)2 – r2
Or                        P0 = 5/4 – r
If the radius r is specified as an integer, we can simply round
P0 to
                    P0 = 1 – r (for integer r)
Since all increments are integers.
                                                 11
Mid-Point Circle Algorithm
1. Input radius r and circle centre (xc, yc), and obtain the
   first point on the circumference of a circle centred on the
   origin as:
                       (x0, y0) = (0, r)
2. Calculate the initial value of the decision parameter as:
                          P0 = 5/4 – r
3. At each position xk Starting with k=0, perform the
following test.
  If pk < 0, the next point along the circle centred on (0, 0)
   is (xk+1 , yk) and    pk+1 = pk + 2xk+1 + 1
 Otherwise the next point along the circle is (xk+1 , yk– 1)
  and     pk+1 = pk + 2xk +1 +1 – 2yk +1
where 2xk+1 = 2xk+1 + 2 and 2yk+1= 2yk+1
                                      12– 2
Mid-Point Circle Algorithm
4. Determine symmetry points in the other seven octants
5. Move each calculated pixel position (x, y) onto the circular
path centred at (xc, yc) and plot the coordinate values:
             X= x+ xc                 y = y+ yc
6. Repeat steps 3 to 5 until x >= y
                                               13
Mid-Point Circle Algorithm Example
Given a circle radius=10, we demonstrate the midpoint
circle algorithm by determining positions along the circle
octant in the first quadrant from x = 0 to x = y. the initial
value of the decision parameter is P0 = 1 – r = – 9
For the circle centred on the origin, the initial point is (x0,
y0) = (0, 10), and the initial increment term for calculating
the decision parameters are
                   2x0 = 0 and 2y0= 2r
                   2x0 = 0 and 2y0= 20
Successive decision parameters values and positions along
the circle path are calculated using midpoint algorithm as:
                                               14
Mid-Point Circle Algorithm
         xk+1 ,
k   Pk            2xk+1 2yk+1
         yk– 1
0   -9   (1, 10) 2       20
1   -6   (2, 10) 4       20
2   -1   (3, 10) 6       20
3   6    (4, 9)   8      18
4   -3   (5, 9)   10     18
5   8    (6, 8)   12     16
6   5    (7, 7)   14     14
                                                         10/1.4
A plot of the generated pixel positions in the first quadrant is
shown in the figure
                                               15
         Midpoint Ellipse Algorithm
General procedure (same like circle):
  1. Determine curve positions for an ellipse with center at
     (0, 0) (origin)
  2. Move to proper position, i.e. add xc and yc
  3. If required perform rotation
  4. Use decision parameter to determine closest pixel
  5. For other 3 quadrants use symmetry
3 Quadrants? In circle 7 octants!
So what is wrong?
Warning: Symmetry for ellipse only in quadrants
                                                               16
Centered at (xc, yc) with semimajor axis rx and semiminor axis ry.
                                                                     17
Calculation of a point (x, y) in one quadrant yields the ellipse points shown
for the other three quadrants.
                                                                         18
           Midpoint Ellipse Algorithm
 ▪ Ellipse function:
      f ellipse ( x , y ) = r x + r y − r r
                            y
                             2   2
                                       x
                                        2   2      2 2
                                                  x y
If any point (x,y) is inside the ellipse, fellipse(x,y)<0
If any point (x,y) is on the ellipse, fellipse(x,y)=0
If any point (x,y) is outside the ellipse, fellipse(x,y)>0
                                                        19
Over region 1, the magnitude of the ellipse slope is less than 1.0; over region 2, the
magnitude of the slope is greater than 1.0.
                                                                               20
    Midpoint Ellipse Algorithm: Decision
          Parameter {Region 1}
We can find a recursive expression for the next
decision parameter as:
◼   We can obtain the initial decision parameter
    as:
                                                   21
Between candidate pixels at sampling position xk + 1 along an
elliptical path.
                                                                22
Between candidate pixels at sampling position yk − 1 along an
elliptical path.                                                23
       Midpoint Ellipse Algorithm Decision
             Parameter {Region 2}
We can find a recursive expression for the next
decision parameter as:
◼   We can obtain the initial decision parameter:
                                                    24
  Midpoint Ellipse Algorithm Decision
               Parameter
Incrementing the decision parameter:
                                        25
 Midpoint Ellipse Algorithm Region 1-
               Region 2?
When to move from region 1 to region 2?
Answer: Decide on slope
But how to calculate slope?
                      2
Answer:     dy    2 ry x
                 =    2
            dx       2r y
                      X
At the boundary dy / dx = -1.0
If     2ry x  2rx y
          2       2
move from region1 to region 2             26
Midpoint Ellipse Algorithm
                         27
Example
          29
30
Pixel positions along an elliptical path centered on the origin with rx = 8 and
ry = 6, using the midpoint algorithm to calculate locations within the first
quadrant
                                                                        31