7//7 CSC. Computer Graphics Lecture Kasun@dscs.sjp.ac.l Department of Computer Science Universit of Sri Jaewardanepura Line drawing algorithms DDA Midpoint (Bresenham s) Algorithm Circle drawing algorithms Simple Circle Algorithm Mid-Point Circle Algorithm Ellipse drawing algorithms Simple Ellipse Algorithm Midpoint Ellipse Algorithm 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP A line segment in a scene is defined b the coordinate positions of the line end-points (7, ) (, ) 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP But what happens when we tr to draw this on a piel based displa? Considerations to eep in mind: The line has to loo good Avoid jaggies It has to be lightening fast! Millions of lines need to be drawn in a tpical scene. How do we choose which piels to turn on? 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 6
7//7 Lines must create visuall satisfactor images. Lines should appear straight Lines should terminate accuratel Lines should have constant densit Line densit should be independent of line length and angle. Ideall, the following properties should be considered Smooth Continuous Pass through specified points Uniform brightness Efficient 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 7 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 8 There are three possible choices. Eplicit: = f() = m ( - ) + where m = d/d Parametric: = f(t), = f(t) = + t( - ), t in [,] = + t( - ) Implicit: f(, ) = F(,) = (-)d - (-)d if F(,) = then (,) is on line F(,) > then (,) is below line F(,) < then (,) is above line 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 9 DrawLine(int, int, int, int, int color) float ; int ; for (=; <=; ++) = + (-)*(-)/(-) WritePiel(, Round(), color ); 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP DrawLine(int,int,int,int, int color) float m,; int d,d,; d = - ; d = - ; m = d/d; = +.; for (=; <=; ++) WritePiel(, Floor(), color ); = + m; A Better Implementation 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP Advantages over Algorithm Eliminates multiplication thus improves speed Disadvantages Round-off error builds up Get piel drift Rounding and floating point arithmetic still time consuming Wors well onl for m < Need to loop in for m > Need to handle special cases 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP
7//7 Let s quicl review the equations involved in drawing lines end end Slope-intercept line equation: m c where: m end end c m 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP The slope of a line (m) is defined b its start and end coordinates The diagram below shows some eamples of lines and their slopes m = - / m = - / m = m = - m = - m = - m = m = m = m = m = / m = / m = 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP We could simpl wor out the corresponding coordinate for each unit coordinate Let s consider the following eample: (7, ) (, ) 7 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 6 7 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 6 (, ) 6 7 (7, ) First wor out m and b: m 7 c Now for each value wor out the value: ( ) ( ) ( ) ( 6) 6 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 7 Now just round off the results and turn on these piels to draw our line 7 6 6 7 8 ( ) ( ) ( ) ( 6) 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 8
7//7 However, this approach is just wa too slow In particular loo out for: The equation = m + c requires the multiplication of m b Rounding off the resulting coordinates We need a faster solution In the previous eample we chose to solve the parametric line equation to give us the coordinate for each unit coordinate What if we had done it the other wa around? Where: c m and m end end c m 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 9 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP Leaving out the details this gives us: ( ) ( ) We can see easil that 7 this line doesn t loo 6 ver good! We choose which wa to wor out the line piels based on the slope of the line 6 7 8 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP If the slope of a line is between - and then we wor out the coordinates for a line based on it s unit coordinates Otherwise we do the opposite coordinates are computed based on unit coordinates m = - / m = - / m = m = - m = - m = - m = m = m = m = m = / m = / m = 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP Given points P = (, ) and P = (, ) = + t( - ) = + t( - ) t is called the parameter. When t = we get (, ) t = we get (, ) As < t < we get all the other points on the line segment between (, ) and (, ). 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP
7//7 The digital differential analzer (DDA) algorithm taes an incremental approach in order to speed up scan conversion Simpl calculate + based on The original differential analzer w a s a p h s i c a l m a c h i n e developed b Vannevar Bush at MIT in the 9 s in order to solve ordina r differen tia l e q u a t i o n s. M o r e i n f o r m a t i o n h e r e. 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 6 Digital differential analser Y=m+c For m< =m For m> = /m 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 7 Procedure DDA(X,Y,X,Y :Integer); Var Length, I :Integer; X,Y,Xinc,Yinc :Real; Begin Length := ABS(X - X); If ABS(Y -Y) > Length Then Length := ABS(Y-Y); Xinc := (X - X)/Length; Yinc := (Y -Y)/Length; X := X; Y := Y; For I := To Length Do Begin Plot(Round(X), Round(Y)); X := X + Xinc; Y := Y + Yinc End For End; DDA 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 8 Compute which piels should be turned on to represent the line from (6,9) to (,). Length := Ma of (ABS(-6), ABS(-9)) = Xinc := Yinc :=.6 Values computed are: (6,9), (7,9.6), (8,.), (9,.8), (,.), (,) 9 6 7 8 9 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 9 Consider the list of points that we determined for the line in our previous eample: (, ), (, / ), (, / ), (, / ), (6, / ), (7, ) Notice that as the coordinates go up b one, the coordinates simpl go up b the slope of the line This is the e insight in the DDA algorithm 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP
7//7 When the slope of the line is between - and begin at the first point in the line and, b incrementing the coordinate b, calculate the corresponding coordinates as follows: m When the slope is outside these limits, increment the coordinate b and calculate the corresponding coordinates as follows: m 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP Again the values calculated b the equations used b the DDA algorithm must be rounded to match piel values ( +, round( +m)) (, ) ( +, +m) (, round( )) (round( + / m ), +) (, ) ( + / m, +) (round( ), ) 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP Let s tr out the following eamples: (, 7) 7 (7, ) (, ) (, ) 7 It is much faster than our previous attempt. No an multiplications involved. However, there are still two big issues: Accumulation of round-off errors can mae the pielated line drift awa from what was intended The rounding operations and floating point arithmetic involved are time consuming 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP It is another incremental scan conversion algorithm. The big advantage of this algorithm is that it uses onl integer calculations J a c B r e s e n h a m wored for 7 ears at IBM before entering academia. Bresenham developed his famous algorithms at IBM in t h e e a r l 96s 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 6 6
7//7 The midpoint algorithm is even better than the above algorithm in that it uses onl integer calculations. It treats line drawing as a sequence of decisions. For each piel that is drawn the net piel will be either N or NE, as shown below. It uses the implicit definition of the line, F(,) =. The N/NE decisions are made as follows. d = F( p +, p +.) if d < line below midpoint choose E if d > line above midpoint choose NE if E is chosen d new = F( p +, p +.) d new - d old = F( p +, p +.) - F( p +, p +.) Delta = d new -d old = d 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 7 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 8 If NE is chosen d new = F( p +, p +.) Delta = d-d Initialization d start = F( +, +.) = ( +- )d - ( +.- )d = d-d/ Integer onl algorithm F (,) = F(,) ; d = d d start = d - d Delta = Delta DrawLine(int, int, int, int, int color) int d, d, d, ince, incne,, ; d = - ; d = - ; d = *d - d; ince = *d; incne = *(d - d); = ; for (=; <=; ++) WritePiel(,, color); if (d>) d = d + incne; = + ; else d = d + ince; 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 9 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP The initial value for the decision variable, d, ma be calculated directl from the formula at point (,). d = f( +, + /) = b() - a(/) = b - a/ Therefore, the algorithm for a line from (,) to (a,b) in the first octant is: Note: The onl non-integer value is a/. If we then multipl b to get d' = d, we can do all integer arithmetic using onl the operations +, -, and left-shift. The algorithm still wors since we onl care about the sign, not the value of d. := ; := ; d := b - a/; For i := to a do Plot(,); If d >= Then := + ; := + ; d := d + b a Else := + ; d := d + b End End 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP To generalize lines with arbitrar slopes Consider smmetr between various octants and quadrants For m >, interchange roles of and, that is step in direction, and decide whether the value is above or below the line. If m >, and right endpoint is the first point, both and decrease. To ensure uniqueness, independent of direction, alwas choose upper (or lower) point if the line go through the mid-point. Handle special cases without invoing the algorithm: horizontal, vertical and diagonal lines 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 7
7//7 Generalize the algorithm to wor for lines beginning at points other than (,) b giving and the proper initial values. Note: This algorithm onl wors for lines with slopes between and Begin Bresenham for lines with slope between and a := ABS(end - start); b := ABS(end - start); d := *b - a; Incr := *(b-a); Incr := *b; If start > end Then := end; := end Else := start; := start End For I := to a Do Plot(,); := + ; If d >= Then := + ; d := d + incr Else d := d + incr End End For Loop End Bresenham Move across the ais in unit intervals and at each step choose between two different coordinates (, ) ( +, +) ( +, ) For eample, from position (, ) we have to choose between (, ) and (, ) We would lie the point that is closer to the original line 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP At sample position + the vertical separations from the mathematical line are labelled d upper and d lower + The coordinate on the mathematical line at + is: m( ) b d upper + d lower So, d upper and d lower are given as follows: and: d d lower upper m( ) b ( ) m( ) b We can use these to mae a simple decision about which piel is closer to the mathematical line 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 6 Eplicit: = f() R Usuall, we draw a quarter circle b incrementing from to R in unit steps and solving for + for each step. Parametric: Rcos Rsin - b stepping the angle from to 9 - avoids large gaps but still insufficient. Implicit: f() = + -R If f(,) = then it is on the circle. f(,) > then it is outside the circle. f(,) < then it is inside the circle. 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 8 8
7//7 The first thing we can notice to mae our circle drawing algorithm more efficient is that circles centred at (, ) have eight-wa smmetr (-, ) (, ) (-, ) (, ) (-, -) R (, -) (-, -) (, -) 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 9 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP The equation for a circle is: r where r is the radius of the circle So, we can write a simple circle drawing algorithm b solving the equation for at unit intervals using: r 9 9 6 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP This is not a brilliant solution! It has large gaps where the slope approaches the vertical. The calculations are not ver efficient The square (multipl) operations The square root operation tr reall hard to avoid these! We need a more efficient, more accurate solution X=r*cosθ+ c Y=r*sinθ+ c º θ 6º Or θ 6.8(*π) Problem: Deciding the increment in θ Cos, sin calculations 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 9
7//7 Similarl 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-wa smmetr so onl ever calculate the points for the top right eighth of a circle, and then use smmetr to get the rest of the points The mid-point circle a l g o r i t h m w a s developed b Jac Bresenham, who we heard about earlier. Bresenham s patent for the algorithm can USJP be v i e w e d here. 6 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of d old = F( p +, p +.) If d old <, E is chosen d new = F( p +, p -.) = d old +( p +) Delta E = p + If d old >=, SE is chosen d new = F( p +, p -.) = d old +( p - p +) Delta SE = p - p + Initialization d init = / R = - R 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 7 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 8 6 M 6 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 9 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 6
7//7 M 6 Assume that we have (, ) just plotted point (, ) The net point is a choice between ( +, ) and ( +, -) We would lie to choose the point that is nearest to the actual circle So how do we mae this choice? ( +, ) ( +, -) 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 6 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 6 Let s re-jig the equation of the circle slightl to give us: (, ) r f circ The equation evaluates as follows: f circ, if (, ) is inside the circle boundar (, ), if (, ) is on thecircle boundar, if (, ) is outsidethecircle boundar B evaluating this function at the midpoint between the candidate piels we can mae our decision Assuming we have just plotted the piel at (, ) so we need to choose between ( +, ) and ( +, -) Our decision variable can be defined as: p f circ ( (, ) ) ( ) If p < the midpoint is inside the circle and and the piel at is closer to the circle Otherwise the midpoint is outside and - is closer r 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 6 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 6 To ensure things are as efficient as possible we can do all of our calculations incrementall First consider: or: p f circ [(, r where + is either or - depending on the sign of p ) ] p p ( ) ( ) ( ) 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 6 The first decision variable is given as: p fcirc r (, ) r ( ) r r Then if p < then the net decision variable is given as: p p If p > then the decision variable is: p p 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 66