You are on page 1of 3

18 19 A Simple Circle Drawing Algorithm

of A Simple Circle Drawing Algorithm of


39 39 (cont…)
The equation for a circle is:
x2 + y2 = r 2 y0 = 20 2 " 0 2 ! 20

where r is the radius of the circle y1 = 20 2 " 12 ! 20

So, we can write a simple circle drawing y2 = 20 2 " 2 2 ! 20


algorithm by solving the equation for y at
unit x intervals using:
y19 = 20 2 " 19 2 ! 6
2 2
y=± r !x y20 = 20 2 " 20 2 ! 0

20 A Simple Circle Drawing Algorithm 21


of of Eight-Way Symmetry
39 (cont…) 39

However, unsurprisingly this is not a brilliant The first thing we can notice to make our circle
solution! drawing algorithm more efficient is that circles
Firstly, the resulting circle has large gaps centred at (0, 0) have eight-way symmetry
where the slope approaches the vertical
(-x, y) (x, y)
Secondly, the calculations are not very
efficient
(-y, x) (y, x)
– The square (multiply) operations
– The square root operation – try really hard to R

avoid these!
2
(-y, -x) (y, -x)
We need a more efficient, more accurate
solution (-x, -y) (x, -y)

22 23
of
39
Mid-Point Circle Algorithm of
39
Mid-Point Circle Algorithm (cont…)
Similarly to the case with lines, Assume that we have
there is an incremental just plotted point (xk, yk) (xk, yk) (xk+1, yk)
algorithm for drawing circles – The next point is a
the mid-point circle algorithm
choice between (xk+1, yk) (xk+1, yk-1)
In the mid-point circle algorithm and (xk+1, yk-1)
we use eight-way symmetry so
The mid-point circle We would like to choose
only ever calculate the points algorithm was
the point that is nearest to
for the top right eighth of a developed by Jack
Bresenham, who we the actual circle
circle, and then use symmetry heard about earlier.
to get the rest of the points Bresenham’s patent So how do we make this choice?
for the algorithm can
be viewed here.
24 25
of
39
Mid-Point Circle Algorithm (cont…) of
39
Mid-Point Circle Algorithm (cont…)
Let’s re-jig the equation of the circle slightly Assuming we have just plotted the pixel at
to give us: (xk,yk) so we need to choose between
f circ ( x, y ) = x 2 + y 2 ! r 2 (xk+1,yk) and (xk+1,yk-1)
The equation evaluates as follows: Our decision variable can be defined as:
$< 0, if ( x, y ) is inside the circle boundary pk = f circ ( xk + 1, yk ! 1 )
2
!
f circ ( x, y ) #= 0, if ( x, y ) is on the circle boundary 1
= ( xk + 1) + ( yk ! ) 2 ! r 2
2

!> 0, if ( x, y ) is outside the circle boundary 2


" If pk < 0 the midpoint is inside the circle and
By evaluating this function at the midpoint and the pixel at yk is closer to the circle
between the candidate pixels we can make Otherwise the midpoint is outside and yk-1 is
our decision closer

26 27
of
39
Mid-Point Circle Algorithm (cont…) of
39
Mid-Point Circle Algorithm (cont…)
To ensure things are as efficient as possible The first decision variable is given as:
we can do all of our calculations p0 = f circ (1, r ! 1 )
incrementally 2
First consider: = 1 + (r ! 1 ) 2 ! r 2
2
(
pk +1 = f circ xk +1 + 1, yk +1 ! 1
2
) = 5 !r
4
(
= [( xk + 1) + 1]2 + yk +1 ! 1
2
)!r
2
2
Then if pk < 0 then the next decision variable
or: is given as:
pk +1 = pk + 2( xk + 1) + ( yk2+1 ! yk2 ) ! ( yk +1 ! yk ) + 1 pk +1 = pk + 2 xk +1 + 1
where yk+1 is either yk or yk-1 depending on If pk > 0 then the decision variable is:
the sign of pk pk +1 = pk + 2 xk +1 + 1 ! 2 yk + 1

28 29
of
39
The Mid-Point Circle Algorithm of
39
The Mid-Point Circle Algorithm (cont…)

MID-POINT CIRCLE ALGORITHM Otherwise the next point along the circle is (xk+1, yk-1)
• Input radius r and circle centre (xc, yc), then set the and:
coordinates for the first point on the circumference of a pk +1 = pk + 2 xk +1 + 1 ! 2 yk +1
circle centred on the origin as:
( x0 , y0 ) = (0, r ) 4. Determine symmetry points in the other seven octants
5. Move each calculated pixel position (x, y) onto the
• Calculate the initial value of the decision parameter as: circular path centred at (xc, yc) to plot the coordinate
p0 = 5 ! r values:
4
• Starting with k = 0 at each position xk, perform the x = x + xc y = y + yc
following test. If pk < 0, the next point along the circle 6. Repeat steps 3 to 5 until x >= y
centred on (0, 0) is (xk+1, yk) and:
pk +1 = pk + 2 xk +1 + 1
30 31 Mid-Point Circle Algorithm Example
of Mid-Point Circle Algorithm Example of
39 39 (cont…)
To see the mid-point circle algorithm in
10
action lets use it to draw a circle centred at k pk (xk+1,yk+1) 2xk+1 2yk+1
9
(0,0) with radius 10 8 0
7
1
6
5 2
4
3
3
2 4
1 5
0
6
0 1 2 3 4 5 6 7 8 9 10

32 33 Mid-Point Circle Algorithm Example


of Mid-Point Circle Algorithm Exercise of
39 39 (cont…)
Use the mid-point circle algorithm to draw 16
k pk (xk+1,yk+1) 2xk+1 2yk+1
15
the circle centred at (0,0) with radius 15 14
0
13
12 1
11 2
10 3
9
4
8
7 5
6 6
5 7
4 8
3
9
2
1 10
0 11
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 12

34
of
39
Mid-Point Circle Algorithm Summary
The key insights in the mid-point circle
algorithm are:
– Eight-way symmetry can hugely reduce the
work in drawing a circle
– Moving in unit steps along the x axis at each
point along the circle’s edge we need to
choose between two possible y coordinates

You might also like