CS 548: COMPUTER GRAPHICS DRAWING LINES AND CIRCLES SPRING 05 DR. MICHAEL J. REALE
OPENGL POINTS AND LINES
OPENGL POINTS AND LINES In OenGL, there are different constants used to indicate what ind of rimitive we are tring to draw For oints, we have GL_POINTS For lines, we have GL_LINES, GL_LINE_STRIP, and GL_LINE_LOOP
DRAWING POINTS: LEGACY VS. NEW To draw oints in legac OenGL: glbegingl_points); glverte3f,5,0); glverte3f,3,); glend); To draw oints in OenGL 3.0+, we set u our buffers and then call: gldrawarrasgl_points, 0, ointcnt); For other rimitives lie lines), the rocedure would be the same just relace GL_POINTS with what ou want instead.
DRAWING LINES Deending on which constant we choose, the lines will be drawn differentl GL_LINES = draw individual lines with ever vertices GL_LINE_STRIP = draw a olline connecting all vertices in sequence GL_LINE_LOOP = draw a olline, and then also connect the first and last vertices
DRAWING LINES glbegingl_lines); glverteiv) glverteiv); glverteiv3); glverteiv4); glverteiv5); glend); NOTE: 5 is ignored, since there isn t another verte to air it with ALSO NOTE: the v art means assing in an arra
DRAWING LINES glbegingl_line_strip); glverteiv) glverteiv); glverteiv3); glverteiv4); glverteiv5); glend); Creates a olline
DRAWING LINES glbegingl_line_loop); glverteiv) glverteiv); glverteiv3); glverteiv4); glverteiv5); glend); Creates a closed olline
DDA LINE DRAWING ALGORITHM
THE PROBLEM WITH PIXELS Prett much all modern dislas use a grid of iels to disla images Discrete digital) locations Problem: when drawing lines that aren t erfectl horizontal, vertical, or diagonal, the oints in the middle of the line do not fall erfectl into the iel locations I.e., have to round line coordinates to integers Lines end u having this stair-ste effect jaggies ) aliasing
LINE EQUATIONS A straight line can be mathematicall defined using the Cartesian sloe-intercet equation: m b We re dealing with line segments, so these have secified starting and ending oints: So, we can comute the sloe m and the intercet b as follows: m b 0 end end m 0 0 0 0, 0) end, end )
INTERVALS For a given interval δ along a line, we can comute the corresonding interval δ: m Similarl, we can get δ from δ: m
DDA ALGORITHM Digital differential analzer DDA) Scan-conversion line algorithm based on calculating either d or d Samle one coordinate at unit intervals find nearest integer value for other coordinate Eamle: 0 < m <=.0 sloe ositive, with δ > δ) Increment in unit intervals δ = ) Comute successive values as follows: Round value to nearest integer m
DDA ALGORITHM: M >.0 Problem: If sloe is ositive AND greater than.0 m >.0), then we increment b si iels in! Solution: swa roles of and! Increment in unit intervals δ = ) Comute successive values as follows: Round value to nearest integer m
DDA ALGORITHM: WHICH DO WE STEP IN? So, to summarize so far, which coordinate should be increment? Remember: If absd) > absd): Ste in X Otherwise: Ste in Y m end end 0 0 d d We ll see later in another algorithm an eas wa to do this is to swa the roles of and So is reall, and is reall
DDA ALGORITHM: LINES IN REVERSE We ve been assuming that the ending oint has a coordinate value greater than the starting oint: Left to right, if incrementing Bottom to to, if incrementing However, we could be going in reverse. If so, then: If right to left, δ = - If to to bottom, δ = -
DDA ALGORITHM: CODE // Get d and d int d = - 0; int d = - 0; int stes, ; float Increment, Increment; // Set starting oint float = 0; float = 0;
DDA ALGORITHM: CODE // Determine which coordinate we should ste in if absd) > absd)) stes = absd); else stes = absd); // Comute increments Increment = floatd) / floatstes); Increment = floatd) / floatstes);
DDA ALGORITHM: CODE // Let s assume we have a magic function called setpiel,) that sets a iel at,) to the aroriate color. // Set value of iel at starting oint setpielround), round)); // For each ste for = 0; < stes; ++) { // Increment both and += Increment; += Increment; } // Set iel to correct color // NOTE: we need to round off the values to integer locations setpielround), round));
DDA ALGORITHM: PROS AND CONS Advantage: Faster than using the sloe-intercet form directl no multilication, onl addition Caveat: initial division necessar Disadvantages: Accumulation of round-off error can cause the line to drift off true ath Rounding rocedure still time-consuming Question: can we do this with nothing but integers?
YES Yes, we can.
BRESENHAM S LINE DRAWING ALGORITHM
BRESENHAM S LINE ALGORITHM: INTRODUCTION Taes advantage of fact that sloe m is reall a fraction of integers Sa we have a ositive sloe and d > d so we re incrementing in ) We have a iel lotted at, ) Given +, the net value is going to be either: + The question is: which one is closer to the real line? +, ) or +, +)?
BRESENHAM S LINE ALGORITHM: THE IDEA The basic idea is to loo at a decision variable to hel us mae the choice at each ste Our revious osition:, ) d lower = distance of +, ) from the true line coordinate +, ) d uer = distance of +, + ) from the true line coordinate +, )
BRESENHAM S LINE ALGORITHM: MATH The value of for the mathematical line at + ) is given b: Ergo: b m ) b m d b m d uer lower ) ) )
BRESENHAM S LINE ALGORITHM: MORE MATH To determine which of the two iels is closer to the true line ath, we can loo at the sign of the following: Positive d uer is smaller choose + ) Negative d lower is smaller choose ) ) ) ) ) ) ) b m b m b m b m b m d d uer lower
BRESENHAM S LINE ALGORITHM: EVEN MORE MATH Remember that: Substituting with our current equation: We will let our decision variable be the following d d m end end 0 0 ) b d d uer lower c b b b b d d uer lower ) ) ) ) ) )
BRESENHAM S LINE ALGORITHM: DECISION VARIABLE c Multiling b Δ won t affect the sign of, since Δ > 0 Note that constant c does not deend on the current osition at all, so can comute it ahead of time: So, as before: c b ) ositive d uer is smaller choose + ) negative d lower is smaller choose
BRESENHAM S LINE ALGORITHM: UPDATING P K We can get the net value of the decision variable i.e., + ) using c c c c c c
BRESENHAM S LINE ALGORITHM: UPDATING P K However, we now: Therefore: So, we need to determine what + ) was: If was ositive + ) = If was negative + ) = 0
BRESENHAM S LINE ALGORITHM: SUMMARIZED. Inut two line endoints and store LEFT endoint in 0, 0 ). Plot first oint 0, 0 ) 3. Comute constants Δ, Δ, Δ, and Δ - Δ. Also comute first value of decision variable: 0 = Δ Δ 4. At each, test : If < 0 lot +, ) + = + Δ Otherwise lot +, +) + = + Δ - Δ 5. Perform ste 4 Δ ) times NOTE: Effectivel assuming line starts at 0,0) and thus b = 0 0) 0) 0) ) ) b ) NOTE: This version ONLY wors with 0 < m <.0!!! Ergo, Δ and Δ are ositive here!
BRESENHAM S LINE ALGORITHM: CODE // NOTE: d and d are ABSOLUTE VALUES in this code int d = fabs - 0); int d = fabs - 0); int = *d - d; int twod = *d; int twodminusd = *d - d); int,;
BRESENHAM S LINE ALGORITHM: CODE // Determine which endoint to use as start osition if0 > ) { = ; = ; = 0; } else { = 0; = 0; } // Plot first iel setpiel,);
BRESENHAM S LINE ALGORITHM: CODE while < ) { ++; } if < 0) += twod; else { ++; += twodminusd; } setpiel,);
BRESENHAM S LINE ALGORITHM: GENERALIZED What we re taled about onl wors with 0 < m <.0 For other sloes, we tae advantage of smmetr: If d > d swa and WARNING: Would then need to call setpiel, ) After swaing endoints and otentiall swaing and, if 0 > decrement rather than increment NOTE: ma actuall be if ou swaed them Two more warnings: ) In the samle code, d and d are ABSOLUTE VALUES ) In the net image, when I sa and, I mean the actual and
BRESENHAM S LINE ALGORITHM: SPECIAL CASES To save time, if ou have a line that is: Δ = 0 vertical) Δ = 0 horizontal) Δ = Δ diagonal) ou can just draw it directl without going through the entire algorithm.
MIDPOINT ALGORITHM A more general wa of viewing Bresenham s Algorithm that can be alied to other conics is called the Midoint Algorithm: Uses the imlicit reresentation e.g., imlicit line equation If a oint is on the inside, F,) < 0 If a oint is on the outside, F,) > 0 If a oint is eactl on the boundar, F,) = 0 Test whether the oint F+, + ½) is inside or outside choose closest oint F, ) A B C 0
MIDPOINT CIRCLE ALGORITHM
INTRODUCTION A circle can be defined b its imlicit form: c, c ) center of circle r = radius Since a circle is smmetric in all 8 octants just comute one octant and relicate in others 0 ) ) ), r F c c
DECISION VARIABLE Assume the circle is centered at 0,0) We re going to start b: Incrementing b Choose whether to go down or not in To determine our net choice, we will loo at our decision variable based on the midoint +, - ½): If < 0 midoint inside circle choose If > 0 midoint outside circle choose - F ), r
UPDATING THE DECISION VARIABLE To figure out how to udate the decision variable, let s loo at the net value: ), r F ), r F Remember:
HOLD ON TO YOUR MATHEMATICAL HATS ), r F Remember: ) ) ) 4 4 ) ) 4 4 ) 4 4 4 3 ) 4 4 4 4 ) r r r r r r
NEXT DECISION VARIABLE + is: if < 0 - ) if > 0 ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) )
UPDATING THE DECISION VARIABLE If < 0 add + + If > 0 add + + + We can also udate + and + incrementall: ) )
INITIAL VALUES We will start at 0,r) Initial decision variable value: 0 F, r 5 4 r ) r r r r r 4 However, if our radius r is an integer, we can round 0 to 0 = r, since all increments are integers
MIDPOINT CIRCLE ALGORITHM SUMMARIZED. Inut radius r and circle center c, c ); first oint = 0, 0 ) = 0,r). Calculate initial decision variable value: 5 0 r 4 3. At each, test : If < 0 net oint is +, ) and: Otherwise net oint is +, - ) and: Where: 4. For each calculated osition,), lot + c, + c ) 5. Plot corresonding smmetric oints in other seven octants 6. Reeat stes 3 through 5 UNTIL >=