Comuter Grahics Comuter Grahics Lecture 3 Line & Circle Drawing
Comuter Grahics Towards the Ideal Line We can onl do a discrete aroimation Illuminate iels as close to the true ath as ossible, consider bi-level disla onl Piels are either lit or not lit
Comuter Grahics What is an ideal line Must aear straight and continuous Onl ossible ais-aligned and 45 o lines Must interolate both defining end oints Must have uniform densit and intensit Consistent within a line and over all lines What about antialiasing? Must be efficient, drawn quickl Lots of them are required!!! 3
Comuter Grahics Simle Line Based on sloe-intercet algorithm from algebra: m b Simle aroach: increment, solve for Floating oint arithmetic required 4
Comuter Grahics Does it Work? It seems to work oka for lines with a sloe of or less, but doesn t work well for lines with sloe greater than lines become more discontinuous in aearance and we must add more than iel er column to make it work. Solution? - use smmetr. 5
Comuter Grahics Modif algorithm er octant OR, increment along -ais if d<d else increment along -ais 6
Comuter Grahics DDA algorithm DDA Digital Differential Analser finite differences 7 Treat line as arametric equation in t : ( ( ( ( t t t t, (, ( Start oint - End oint -
Comuter Grahics DDA Algorithm ( t ( t t( t( Start at t 0 At each ste, increment t b dt Choose aroriate value for dt Ensure no iels are missed: Imlies: d and dt < d dt < Set dt to maimum of d and d new new d d old old d dt d dt 8
Comuter Grahics DDA algorithm line(int, int, int, int { float,; int d -, d -; int n ma(abs(d,abs(d; float dt n, ddt d/dt, ddt d/dt; ; ; while( n-- { oint(round(,round(; ddt; ddt; } } n - range of t. 9
Comuter Grahics DDA algorithm Still need a lot of floating oint arithmetic. round s and adds er iel. Is there a simler wa? Can we use onl integer arithmetic? Easier to imlement in hardware. 0
Comuter Grahics Observation on lines. while( n-- { draw(,; move right; if( below line move u; }
Comuter Grahics Testing for the side of a line. Need a test to determine which side of a line a iel lies. Write the line in imlicit form: F (, a b c 0 If (b<0 F<0 for oints above the line, F>0 for oints below.
Comuter Grahics Testing for the side of a line. F(, a b c Need to find coefficients a,b,c. Recall elicit, sloe-intercet form : d m b and so b d 0 So: F(, d. d. c 0 3
Comuter Grahics Decision variable. Let s assume d/d < 0.5 (we can use smmetr Evaluate F at oint M Referred to as decision variable d F(, NE M E Previous Piel (, Choices for Current iel Choices for Net iel
Comuter Grahics Decision variable. Evaluate d for net iel, Deends on whether E or NE Is chosen : If E chosen : dnew F(, a( b( c Previous Piel (, NE E Choices for Current iel M Choices for Net iel d old But recall : a( So : F(, b( d new d d old old c a d 5
Comuter Grahics Decision variable. If NE was chosen : 3 3 dnew F(, a( b( c M So : Previous Piel (, NE E Choices for Current iel Choices for Net iel d new d d old old a b d d 6
Comuter Grahics Summar of mid-oint algorithm Choose between iels at each ste based uon the sign of a decision variable. Udate the decision variable based uon which iel is chosen. Start oint is siml first endoint (,. Need to calculate the initial value for d 7
Comuter Grahics Initial value of d. Start oint is (, d start F(, a( b( c a b c a F, a ( b b But (, is a oint on the line, so F(, 0 d start d d / Conventional to multil b to remove fraction doesn t effect sign. 8
Comuter Grahics Midoint algorithm void MidointLine(int,,, { int d-; int d-; int d*d-d; int incree*d; int incrne*(d-d; ; ; WritePiel(,; while ( < { if (d< 0 { dincre; } } } else { dincrne; ; ; } WritePiel(,; 9
Comuter Grahics Circle drawing. Can also use Bresenham to draw circles. Use 8-fold smmetr E M SE Previous Piel Choices for Current iel Choices for Net iel 0
Comuter Grahics Circle drawing. Imlicit form for a circle is: f (, ( ( r c c If SE is chosen d new d old ( 5 If E is chosen d new d old ( 3 Functions are linear equations in terms of (, Termed oint of evaluation
Comuter Grahics Summar of line drawing so far. Elicit form of line Inefficient, difficult to control. Parametric form of line. Eress line in terms of arameter t DDA algorithm Imlicit form of line Onl need to test for side of line. Bresenham algorithm. Can also draw circles.
Comuter Grahics Problems with Bresenham algorithm Piels are drawn as a single line unequal line intensit with change in angle. Piel densit.n iels/mm Piel densit n iels/mm Can draw lines in darker colours according to line direction. -Better solution : antialiasing! -(eg. Guta-Sroull algorithm 3
Comuter Grahics Guta-Sroull algorithm. Calculate the distance of the line and the iel center Adjust the colour according to the distance
Comuter Grahics Guta-Sroull algorithm. Calculate distance using features of mid-oint algorithm D v cosφ vd d d NE d d M D v Angle E 5
Comuter Grahics Guta-Sroull algorithm (cont Recall from the midoint algorithm: So ( a b 0 (doubled to avoid fraction F(, c For iel E: So: v (a c b a( b c v D Line to draw NE m v E Θ
Comuter Grahics Guta-Sroull algorithm (cont From revious slide: v a( c b From the midoint comutation, b d D Line to draw NE m v E Θ So: vd a( b c F(, /
Comuter Grahics Guta-Sroull algorithm (cont From the midoint algorithm, we had the decision variable (remember? d F( M F(, Going back to our revious equation: vd F(, a( b c a( b( / b / c F(, / b F( M b d b d d D Line to draw NE m E v Θ
Comuter Grahics So, Guta-Sroull algorithm (cont D d d d d And the denominator is constant Since we are blurring the line, we also need to comute the distances to oints and numerator numerator for for ( v d ( v d d vd d vd
Comuter Grahics Guta-Sroull algorithm (cont If the NE iel had been chosen: b F c b b a c b a F vd /, ( / / ( ( ( (, ( vd d d v vd d d v ( for numerator ( for numerator d d b d b M F b F ( /, (
Comuter Grahics Guta-Sroull algorithm (cont Comute midoint line algorithm, with the following alterations: At each iteration of the algorithm: If the E iel is chosen, set numerator d d If the NE iel is chosen, set numerator d d Udate d as in the regular algorithm Comute D numerator/denominator Color the current iel according to D Comute D uer (d-vd/denominator Comute D lower (dvd/denominator Color uer and lower accordingl