Drawing Circles In today s lecture we ll have a look at: Circle drawing algorithms A simple technique The mid-point circle algorithm Polygon fill algorithms Summary raster drawing algorithms
A Simple Circle Drawing Algorithm The equation for a circle is: where r is the radius the circle x So, we can write a simple circle drawing algorithm by solving the equation for y at unit x intervals using: y r y r x
3 A Simple Circle Drawing Algorithm (cont ) y0 0 0 0 y 0 0 y 0 0 y9 0 9 6 y0 0 0 0
4 A Simple Circle Drawing Algorithm (cont ) However, unsurprisingly this is not a brilliant solution! Firstly, the resulting circle has large gaps where the slope approaches the vertical Secondly, the calculations are not very efficient The square (multiply) operations The square root operation try really hard to avoid these! We need a more efficient, more accurate solution
5 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 (-x, y) (x, y) (-y, x) (y, x) (-y, -x) R (y, -x) (-x, -y) (x, -y)
6 Mid-Point Circle Algorithm Similarly to the case with lines, there is an incremental algorithm for drawing circles the mid-point circle algorithm In the mid-point circle algorithm we use eight-way symmetry so only ever calculate the points for the top right eighth a circle, and then use symmetry to get the rest the points The mid-point circle algorithm was developed by Jack Bresenham, who we heard about earlier. Bresenham s patent for the algorithm can be viewed here.
7 Mid-Point Circle Algorithm (cont ) Assume that we have just plotted point (x k, y k ) (x k, y k ) (x k +, y k ) The next point is a choice between (x k +, y k ) and (x k +, y k -) (x k +, y k -) We would like to choose the point that is nearest to the actual circle So how do we make this choice?
8 Mid-Point Circle Algorithm (cont ) Let s re-jig the equation the circle slightly to give us: ( x, y) x y r f circ The equation evaluates as follows: f circ 0, if ( x, y) 0, if 0, if ( x, y) is inside ( x, y) is the circle boundary on the circle boundary ( x, y) is outside the circle boundary By evaluating this function at the midpoint between the candidate pixels we can make our decision
9 Mid-Point Circle Algorithm (cont ) Assuming we have just plotted the pixel at (x k,y k ) so we need to choose between (x k +,y k ) and (x k +,y k -) Our decision variable can be defined as: p k fcirc ( xk, yk ) ( xk ) ( yk ) r If p k < 0 the midpoint is inside the circle and and the pixel at y k is closer to the circle Otherwise the midpoint is outside and y k - is closer
0 Mid-Point Circle Algorithm (cont ) To ensure things are as efficient as possible we can do all our calculations incrementally First consider: or: p k p k f circ [( x k x k, y ) ] where y k+ is either y k or y k - depending on the sign p k k y r k pk ( xk ) ( yk yk ) ( yk yk )
Mid-Point Circle Algorithm (cont ) The first decision variable is given as: Then if p k < 0 then the next decision variable is given as: If p k > 0 then the decision variable is: r r r r f p circ 4 5 ) ( ) (, 0 k k k x p p k k k k y x p p
The Mid-Point Circle Algorithm MID-POINT CIRCLE ALGORITHM Input radius r and circle centre (x c, y c ), then set the coordinates for the first point on the circumference a circle centred on the origin as: ( 0 x0, y ) (0, r) Calculate the initial value the decision parameter as: p 0 5 4 Starting with k = 0 at each position x k, perform the following test. If p k < 0, the next point along the circle centred on (0, 0) is (x k +, y k ) and: p r k pk xk
3 The Mid-Point Circle Algorithm (cont ) Otherwise the next point along the circle is (x k +, y k -) and: p k pk xk yk 4. Determine symmetry points in the other seven octants 5. Move each calculated pixel position (x, y) onto the circular path centred at (x c, y c ) to plot the coordinate values: x x x c y y 6. Repeat steps 3 to 5 until x >= y yc
4 Mid-Point Circle Algorithm Example To see the mid-point circle algorithm in action lets use it to draw a circle centred at (0,0) with radius 0
5 Mid-Point Circle Algorithm Example (cont ) 0 k p k (x k+,y k+ ) x k+ y k+ 9 8 7 6 5 4 3 0 0 3 4 5 6 7 8 9 0 0 3 4 5 6
6 Mid-Point Circle Algorithm Exercise Use the mid-point circle algorithm to draw the circle centred at (0,0) with radius 5
7 Mid-Point Circle Algorithm Example (cont ) 6 5 4 3 0 9 8 7 6 5 4 3 0 0 3 4 5 6 7 8 9 0 34 56 k p k (x k+,y k+ ) x k+ y k+ 0 3 4 5 6 7 8 9 0
8 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
9 Filling Polygons So we can figure out how to draw lines and circles How do we go about drawing polygons? We use an incremental algorithm known as the scan-line algorithm
0 Scan-Line Polygon Fill Algorithm 0 Scan Line 8 6 4 0 4 6 8 0 4 6
Scan-Line Polygon Fill Algorithm The basic scan-line algorithm is as follows: Find the intersections the scan line with all edges the polygon Sort the intersections by increasing x coordinate Fill in all pixels between pairs intersections that lie interior to the polygon
Scan-Line Polygon Fill Algorithm (cont )
3 Line Drawing Summary Over the last couple lectures we have looked at the idea scan converting lines The key thing to remember is this has to be FAST For lines we have either DDA or Bresenham For circles the mid-point algorithm
4 Anti-Aliasing
5 Summary Of Drawing Algorithms
6 Mid-Point Circle Algorithm (cont ) 6 5 4 3 3 4
7 Mid-Point Circle Algorithm (cont ) M 6 5 4 3 3 4
8 Mid-Point Circle Algorithm (cont ) M 6 5 4 3 3 4
9 Blank Grid
30 Blank Grid
3 Blank Grid 0 9 8 7 6 5 4 3 0 0 3 4 5 6 7 8 9 0
3 Blank Grid