Computer Graphics : Bresenham Line Drawing Algorithm, Circle Drawing & Polygon Filling Downloaded from :www.comp.dit.ie/bmacnamee/materials/graphics/006-
Contents In today s lecture we ll have a loo at: Bresenham s line drawing algorithm Line drawing algorithm comparisons Circle drawing algorithms A simple technique The mid-point circle algorithm Polygon fill algorithms Summary raster drawing algorithms
3 The Bresenham Line Algorithm The Bresenham algorithm is another incremental scan conversion algorithm The big advantage this algorithm is that it uses only integer calculations J a c B r e s e n h a m wored for 7 years at IBM before entering academia. Bresenham developed his famous algorithms at IBM in t h e e a r l y 1960s
4 The Big Idea Move across the x axis in unit intervals and at each step choose between two different y coordinates For example, from 5 position (, 3) we (x +1, y +1) have to choose 4 (x, y ) between (3, 3) and 3 (3, 4) (x +1, y ) 3 4 5 We would lie the point that is closer to the original line
5 Deriving The Bresenham Line Algorithm At sample position x +1 the vertical separations from the mathematical line are labelled d upper and d lower y +1 y y d upper x +1 d lower The y coordinate on the mathematical line at x +1 is: y m( x 1) b
6 Deriving The Bresenham Line Algorithm (cont ) So, d upper and d lower are given as follows: and: d d y lower y upper m( x 1) ( y 1) b y We can use these to mae a simple decision about which pixel is closer to the mathematical line y y 1 m( x 1) b
7 Deriving The Bresenham Line Algorithm This simple decision is based on the difference between the two pixel positions: d lower d upper m( x 1) y b 1 Let s substitute m with y/ x where x and (cont ) y are the differences between the end-points: y x( dlower dupper) x( ( x 1) y b 1) x y x x y y x(b 1) y x x y c
8 Deriving The Bresenham Line Algorithm (cont ) So, a decision parameter p for the th step along a line is given by: p x( d d ) y x lower upper x The sign the decision parameter p is the same as that d lower d upper If p is negative, then we choose the lower pixel, otherwise we choose the upper pixel y c
9 Deriving The Bresenham Line Algorithm Remember coordinate changes occur along the x axis in unit steps so we can do everything with integer calculations At step +1 the decision parameter is given as: p y x x y c 1 1 1 Subtracting p from this we get: p p y( x 1 x ) x( y 1 1 (cont ) y )
10 Deriving The Bresenham Line Algorithm (cont ) But, x +1 is the same as x +1 so: p p y x( y 1 1 where y +1 - y is either 0 or 1 depending on the sign p The first decision parameter p0 is evaluated at (x0, y0) is given as: y ) p 0 y x
11 The Bresenham Line Algorithm BRESENHAM S LINE DRAWING ALGORITHM (for m < 1.0) 1. Input the two line end-points, storing the left end-point in (x 0, y 0 ). Plot the point (x 0, y 0 ) 3. Calculate the constants Δx, Δy, Δy, and (Δy - Δx) and get the first value for the decision parameter as: p 0 y x 4. At each x along the line, starting at = 0, perform the following test. If p < 0, the next point to plot is (x +1, y ) and: p 1 p y
1 The Bresenham Line Algorithm (cont ) Otherwise, the next point to plot is (x +1, y +1) and: 1 p y x 5. Repeat step 4 (Δx 1) times p ACHTUNG! The algorithm and derivation above assumes slopes are less than 1. for other slopes we need to adjust the algorithm slightly
13 Bresenham Example Let s have a go at this Let s plot the line from (0, 10) to (30, 18) First f calculate all the constants: Δx: 10 Δy: 8 Δy: 16 Δy - Δx: -4 Calculate the initial decision parameter p 0 : p0 = Δy Δx = 6
14 Bresenham Example (cont ) 18 17 16 15 14 13 1 11 10 0 1 3 4 5 6 7 8 9 30 p (x +1,y +1 ) 0 1 3 4 5 6 7 8 9
15 Bresenham Exercise Go through the steps the Bresenham line drawing algorithm for a line going from (1,1) to (9,16)
16 Bresenham Exercise (cont ) 18 17 16 15 14 13 1 11 10 0 1 3 4 5 6 7 8 9 30 p (x +1,y +1 ) 0 1 3 4 5 6 7 8
17 Bresenham Line Algorithm Summary The Bresenham line algorithm has the following advantages: An fast incremental algorithm Uses only integer calculations Comparing this to the DDA algorithm, DDA has the following problems: Accumulation round-f errors can mae the pixelated line drift away from what was intended The rounding operations and floating point arithmetic involved are time consuming
18 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
19 A Simple Circle Drawing Algorithm (cont ) y0 0 0 y1 0 1 0 0 y 0 0 y19 0 19 y0 0 0 6 0
0 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
1 Eight-Way Symmetry The first thing we can notice to mae 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)
Circle Generating Algorithm Properties Circle Circle is defined as the set points that are all at a given distance r from a center point (x c,y c ) y c r ө (x,y) x c For any circle point (x,y), the distance relationship is expressed by the Pythagorean theorem in Cartesian coordinate as: ( x xc ) ( y yc) r
3 Circle Generating Algorithm We could use this equation to calculate the points on a circle circumference by stepping along x-axis in unit steps from x c r to x c +r and calculate the corresponding y values as y y c r ( x x) c
4 The problems: Involves many computation at each step Spacing between plotted pixel positions is not uniform Adjustment: interchanging x & y (step through y values and calculate x values) Involves many computation too!
5 Another way: Calculate points along a circular boundary using polar coordinates r and ө x = x c + r cos ө y = y c + r sin ө Using fixed angular step size, a circle is plotted with equally spaced points along the circumference Problem: trigonometric calculations are still time consuming
6 Consider symmetry circles Shape the circle is similar in each quadrant i.e. if we determine the curve positions in the 1 st quadrant, we can generate the circle section in the nd quadrant the xy plane (the circle sections are symmetric with respect to the y axis) The circle section in the 3 rd and 4 th quadrant can be obtained by considering symmetry about the x axis One step further symmetry between octants
7 Circle sections in adjacent octants within 1 quadrant are symmetric with respect to the 45 line dividing the octants (-y,x) (y,x) (-x,y) 45 0 (x,y) (-x,-y) (-y,-x) (y,-x) (x,-y) Calculation a circle point (x, y) in 1 octant yields the circle points for the other 7 octants
8 Midpoint Circle Algorithm As in raster algorithm, we sample at unit intervals & determine the closest pixel position to the specified circle path at each step For a given radius, r and screen center position (x c,y c ), we can set up our algorithm to calculate pixel positions around a circle path centered at the coordinate origin (0,0) Each calculated position (x, y) is moved to its proper screen position by adding x c to x and y c to y
9 Midpoint Circle Algorithm Along a circle section from x=0 to x=y in the 1 st quadrant, the slope (m) the curve varies from 0 to - 1.0 i.e. we can tae unit steps in the +ve x direction over the octant & use decision parameter to determine which possible positions is vertically closer to the circle path Positions in the other 7 octants are obtained by symmetry
30 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 a l g o r i t h m w a s developed by Jac Bresenham, who we heard about earlier. Bresenham s patent for the algorithm can be v i e w e d h e r e.
31 Mid-Point Circle Algorithm (cont ) Assume that we have just plotted point (x, y ) The next point is a choice between (x +1, y ) and (x +1, y -1) We would lie to choose the point that is nearest to the actual circle (x, y ) So how do we mae this choice? (x +1, y ) (x +1, y -1)
3 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 thecircle ( x, y) is on thecircle ( x, y) is outside thecircle boundary boundary boundary By evaluating this function at the midpoint between the candidate pixels we can mae our decision
33 Mid-Point Circle Algorithm (cont ) Assuming we have just plotted the pixel at (x,y ) so we need to choose between (x +1,y ) and (x +1,y -1) Our decision variable can be defined as: p f ( x circ ( x 1) 1, y If p < 0 the midpoint is inside the circle and and the pixel at y is closer to the circle Otherwise the midpoint is outside and y -1 is closer ( y 1 ) 1 ) r
34 Mid-Point Circle Algorithm (cont ) To ensure things are as efficient as possible we can do all our calculations incrementally First consider: or: p p 1 f circ [( x x 1 1, y 1) 1] where y +1 is either y or y -1 depending on the sign p 1 1 y r 1 1 1 p ( x 1) ( y 1 y ) ( y 1 y ) 1
35 Mid-Point Circle Algorithm (cont ) The first decision variable is given as: (x o, y o =(0,r)) Then if p < 0 then the next decision variable is given as: If p > 0 then the decision variable is: r r r r f p circ 4 5 ) 1 ( 1 ) 1 (1, 0 1 1 1 x p p 1 1 1 1 y x p p
36 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 5 0 r 4 Starting with = 0 at each position x, perform the following test. If p < 0, the next point along the circle centred on (0, 0) is (x +1, y ) and: p 1 p x 1 1
37 The Mid-Point Circle Algorithm (cont ) Otherwise the next point along the circle is (x +1, y -1) and: p 1 p x 1 1 y 1 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 yc 6. Repeat steps 3 to 5 until x >= y
38 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 10
Mid-Point Circle Algorithm Example (cont ) 10 9 8 7 6 5 4 3 1 0 0 1 3 4 5 6 7 8 9 10 p (x +1,y +1 ) x +1 y +1 0 1 3 4 5 6-9 -6-1 6-3 8 5 (1,10) (,10) (3,10) (4,9) (5,9) (6,8) (7,7) 4 6 8 10 1 14 0 0 0 18 18 16 14
40 Mid-Point Circle Algorithm Exercise Use the mid-point circle algorithm to draw the circle centred at (0,0) with radius 15
41 Mid-Point Circle Algorithm Example (cont ) 16 15 14 13 1 11 10 9 8 7 6 5 4 3 1 0 0 1 3 4 5 6 7 8 9 10 11 1 1314 1516 p (x +1,y +1 ) x +1 y +1 0 1 3 4 5 6 7 8 9 10 11 1
4 Mid-Point Circle Algorithm Summary The ey insights in the mid-point circle algorithm are: Eight-way symmetry can hugely reduce the wor 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
43 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 nown as the scan-line algorithm
44 Scan-Line Polygon Fill Algorithm 10 Scan Line 8 6 4 0 4 6 8 10 1 14 16
45 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
46 Scan-Line Polygon Fill Algorithm (cont )
47 Line Drawing Summary Over the last couple lectures we have looed at the idea scan converting lines The ey thing to remember is this has to be FAST For lines we have either DDA or Bresenham For circles the mid-point algorithm
48 Anti-Aliasing
49 Summary Of Drawing Algorithms
50 Mid-Point Circle Algorithm (cont ) 6 5 4 3 1 3 4
51 Mid-Point Circle Algorithm (cont ) M 6 5 4 3 1 3 4
5 Mid-Point Circle Algorithm (cont ) M 6 5 4 3 1 3 4
53 Blan Grid
54 Blan Grid
55 Blan Grid 10 9 8 7 6 5 4 3 1 0 0 1 3 4 5 6 7 8 9 10
56 Blan Grid