Overview Scan conversion Computer Graphics Rasterizaon and Clipping Polygon filling Clipping in D Aleksandra Pizurica Raster Display PIEL (picture element) RASTER (a rectangular array of points or dots) Scan Conversion SCAN LINE (a row of pixels) Asset: Control of every picture element (rich paoerns) Problem: limited resoluon 4 PloRng in a raster display Assume a bilevel display: each pixel is black or white Different paoerns of dots are created on the screen by serng each pixel to black or white (ie turning it on or off) A problem is that all the edges except perfectly horizontal and vercal ones show jaggies, ie, staircasing effect Line drawing 5
Line drawing: problem Line drawing: objecves A line segment is defined by its end points (x,y ) and (x,y ) with integer coordinates What is the best way to draw a line from the pixel (x,y ) to (x,y )? Such a line should ideally have the following properes Line drawing on a raster grid involves approximaon The process is called rasterizaon or scan- conversion pass through endpoints (appear) straight (appear) smooth independent of endpoint order uniform brightness slope- independent brightness efficient 7 8 Line drawing : brute force approach y (x i, y i ) What is wrong with this approach? x (x i, y i ) (x i, Round(y i )) Line Characterisa-on Explicit: y = mx + B Implicit: F(x,y) = ax + by + c = 0 Constant slope: Δy = m Δx The simplest strategy: ) Compute m; ) Increment x by starng with the leamost point; ) Calculate y i = mx i + B; 4) Intensify the pixel at (x i, Round(y i )), where Round(y i ) = Floor(05+y i ) 9 Line drawing: an incremental approach The previous approach is inefficient because each iteraon requires floang point operaon Mul%ply, Addi%on, and Floor We can eliminate the mulplicaon by nong that: yi+ = mxi + + B = m( xi + Δx) + B = yi + mδx If Δ x =, then y i+ = y i + m Thus, a unit of change in x changes y by m, which is the slope of the line A simple incremental algorithm (x i, y i ) (x i, Round(y i )) (x i +, Round(y i +m)) (x i +, y i +m) 0 Midpoint Line Algorithm: intro What is wrong with the incremental algorithm? Required floang- point operaons (Round) The me- consuming floang- point operaons are unnecessary because both endpoints are integers Bresenham (965) developed a classical algorithm that is aoracve because it uses only integer arithmec In Bresenham s algorithm, instead of incremenng y and then rounding it at each step, we just go to the right, or to the right and up using only integer quanes Midpoint Line Algorithm: idea Assume the line slope is m, 0 m, Previously selected pixel: P (x p, y p ) Choose between the pixel one increment to the right (the east pixel, E) or the pixel one increment to the right and one increment up (the northeast pixel, NE) Q: the intersecon point of the line being scan- converted with the grid line x = x p + M: the midpoint between E and NE If M lies above the line, pixel E is closer to the line; if M is below the line, pixel NE is closer to the line NE M E P = (x p, y p ) Q The problem becomes to decide on which side of the line the midpoint lies
Midpoint Line Algorithm: point posioning Midpoint Line Algorithm: decision variable () dy Explicit line form y = x + B gives: dx F( x, y) = x dy y dx + Bdx = 0 Comparing with the implicit form F( x, y) = ax + by + c = 0 yields a = dy, b = dx, and c = Bdx It can be shown that > 0 if (x,y) is below the line F( x, y) = 0 if (x,y) is on the line < 0 if (x,y) is above the line P (x,y ) P (x 0,y 0 ) dx=x x 0 dy=y-y0 Since we are trying to decide whether M lies below or above the line, we need only to compute F( M ) = F( x p +, y p + ) and to test its sign Define a decision variable d: P = (x p, y p ) NE d = F( xp +, y p + ) = a( xp + ) + b( y p + ) + c > 0, choose pixel NE = 0, choose pixel E < 0, choose pixel E M E Q 4 Midpoint Line Algorithm: decision variable () Midpoint Line Algorithm: decision variable () d old F( xp +, y + ) = p (x p, y p ) (x p +, y p +) (x p +, y p +) NE Q d M E (x p +, y p ) (x p +, y p ) F( xp +, y + ) = p If E is chosen, M is incremented by one step in the x direcon Then dnew = F( x p +, y p + ) = a( x p + ) + b( y p + ) + c = a( x p + ) + a + b( y p + ) + c = F( x p +, y p + ) + a = dold + a = dold + dy new On the other hand, if NE is chosen dnew = F( x p +, y p + ) = a( x p + ) + b( y p + ) + c = a( x p + ) + a + b( y p + ) + b + c = F( x p +, y p + ) + a + b = dold + a + b = dold + dy dx ( dnew dold ) = ΔNE = a + b = dy dx NE Inial condion: F ( x0 +, y0 + ) = a( x0 + ) + b( y0 + ) + c = F( x0, y0) + a + b/ ( d d ) = Δ = a dy new old E E = 5 d start = a + b/ = dy dx / 6 Midpoint Line Algorithm: summary () Start from the first endpoint, and the first decision variable is given by a+b/ Using d start, choose the second pixel, and so on At each step, choose between two pixels based on the sign of the decision variable d calculated in the previous iteraon Update the decision variable d by adding either Δ E or Δ NE to the old value, depending on the choice of the pixel *** Implementaon note: To eliminate the fracon in d start, the original F is mulplied by ; F(x,y) = (ax+by+c) This mulplies each constant in the decision variable (and the increments Δ E and Δ NE ) by Does not affect the sign of the decision variable, which is all that maoers for the midpoint test 7 Midpoint Line Algorithm: summary () Ini-alisa-on: d start = a + b = dy dx where dy = y y 0 and dx = x x 0 Incremental update: ) if E was chosen: Δ E = dy d new = d old + Δ E ) if NE was chosen: Δ NE = (dy dx) d new = d old + Δ NE Advantage: The arithmec needed to evaluate d new for any step is a simple integer addion No me- consuming mulplicaon involved The incremental update is quite simple, therefore An efficient algorithm Note: works for those line with slope (0, ) What about bigger slopes? 8
Line drawing: slope problem When the slope m is between 0 and we can step along x axis Other slopes can be handled by suitable reflecons around the principal axes Line drawing: Slope dependent intensity Problem: weaker intensity of diagonal lines Consider two scan- converted lines in the figure The diagonal line, B, has a slope of and hence is mes longer than the horizontal line A et the same number of pixels is drawn to represent each line If the intensity of each pixel is I, then the intensity per unit length of line A is I, whereas for line B it is only I / m <, can step along x m >, cannot step along x To handle this, apply a suitable reflecon Line B Line A 9 0 Scan converng circles Scan converng circles (-x,y) (x,y) (-y,x) (y,x) (-y,- x) (y,- x) (-x,- y) (x,- y) Suppose we want to rasterize a circle Think of a smart algorithm that makes use of the circle symmetry to avoid unnecessary computaons Which part of the circle do we need to scan convert, so that the rest follows by symmetry? Eight way symmetry: If the point (x,y) is on the circle, then we can trivially compute seven other points on this circle The midpoint circle scan conversion P (x p, y p ) M E SE Previous Choices for pixel current pixel M E M SE Choices for the next step if E or SE is chosen The midpoint circle scan conversion For a circle of radius R: F( x, y) = x + y R = 0 As for lines, the next pixel is chosen on the basis of the decision variable d, which is the value of the funcon at the midpoint dold = F( xp +, yp ) = ( xp + ) + ( yp ) R If d old < 0, E is chosen and we have dnew = F( xp +, yp ) = ( xp + ) + ( yp ) R resulng in Δ = x + E p If d old 0, SE is chosen and the new decision variable is dnew = F( xp +, yp ) = ( xp + ) + ( yp ) R and hence Δ = x + y + 5 SE p p 4
Scan converng ellipses The same reasoning can be applied for scan converng an ellipse, given by F( x, y) = b x + a y a b = 0 Division into four quadrants Two regions in the first quadrant E SE Tangent slope =- region region S SE Polygon filling 5 Polygons Vertex = point in space (D or D) Examples of polygons Polygon = ordered list of verces Each vertex is connected with the next one in the list The last vertex is connected with the first one A polygon can contain holes A polygon can also contain self- intersecons Simple polygon no holes or self- intersecons Such simple polygons are most interesng in Computer Graphics Efficient algorithms exist for polygon scan line rendering; this yields efficient algorithms for lighng, shading, texturing By using a sufficient number of polygons, we can get close to any reasonable shape 7 Simple Concave Convex Polygons Complex (self- intersecng) polygons 8 Drawing modes for polygons Polygon interior Draw lines along polygon edges Using eg midpoint line (Bresenham s) algorithm This is called wireframe mode Draw filled polygons Shaded polygons (shading modes) Flat shaded constant color for whole polygon Gouraud shaded interpolate vertex colors across the polygon We need to fill in (ie to color) only the pixels inside a polygon What is inside of a polygon? Parity (odd- even) rule commonly used Imagine a ray from the point to infinity Count the number of intersecons N with polygon edges If N is odd, the point is inside If N is even, the point is outside N = N = 4 N = 9 0
Polygon filling: scan line approach Span- filling is an important step in the whole polygon- filling algorithm, and is implemented by a three- step process: Find the intersecons of the scan line with all edges of the polygon Sort the intersecons by increasing x coordinates Fill in all pixels between pairs of intersecons that lie interior to the polygon Polygon filling: edge coherence How to find all intersecons between scan lines and edges? A brute- force technique would test each polygon edge against each new scan line This is inefficient and slow! A clever solu-on if an edge intersects with a scan line, and the slope of the edge is m, then successive scan line intersecons can be found from: x i+ = x i + /m span Edward Angel Interacve Computer Graphics where i is the scan line count Given that /m = (x x 0 )/(y y 0 ) the floang- point arithmec can be avoided by storing the numerator, comparing to the denominator, and incremenng x when it overflows How do we find and sort the intersecons efficiently? How do we judge whether a pixel is inside or outside the polygon? Span filling Rasterisation example (7) Scan line (0,0) START STOP START STOP O (0,0) P (,6) P (,0) P (7,0) P4 (4,) 4 Different ways of filling a polyline Alternave filling algorithms Parity rule Nonzero winding rule Nonexterior rule
Filled by parity rule Filling: winding rules + - Count the number of windings 0 Each region gets a winding index i Possible filling rules Fill with one color if i > 0 (non-zero winding fill) Fill with one color if mod (i, ) = 0 (parity fill) Fill with a separate color for each value of i 0 4 5 4 0 9 Count the number of windings + - Clipping 0 0 Clip a line segment at the edges of a rectangular window 4 5 4 0 Needs to be fast and robust Robust means: works for special cases too, and preferably in the same way as for the normal cases The method of Cohen- Sutherland (974) Very simple Suitable for hardware implementaon Can be directly extended to D Count the number of windings 4
Idea: Encode the posion of the end points with respect to lea, right, booom and top window edges and cut accordingly T B P (, ) P (, ) P (, ) P (, ) T B L P(,) Each endpoint is assigned a 4- bit code k = (k, k, k, k 4 ), k i {0,} and k = if < L (too much to the lea); k = if > R (too much to the right) k = if < B (too low) k 4 = if > T (too high) R O L R 4 Note: k and k cannot be simultaneously equal to, same holds for k and k 4 à Hence 9 possible code words (not 4 ) 44 k 4 = T k =k 4 =0 k=00 k=000 P(,) k k=000 k=0000 k=00 k=000 Cohen- Sutherland tries to solve first simple (trivial) cases Assign 4- bit code words to end points: P à k, P à k Step : if k = k = 0, P P is fully visible; otherwise go to Step Step : Find bit per bit logic AND: k=k ^k If k ^k 0, P P is fully and trivially non visible; otherwise go to Step B k = k=00 k=000 k=00 Step : find intersecons with lines extending from window edges; Go to Step O k = L k =k =0 R k = 45 46 Trivially visible (k =k = 0) and trivially invisible ( k ^k 0) examples k 4 = T k =k 4 =0 B k = O 4 k=00 k=000 k=000 k=00 k=0000 k=000 k=00 k=000 k=00 5 k = L k =k =0 R k = 6 47 k ^k = 0, and not trivially visible can imply parally visible (subject to shortening) or invisible segment (addional tesng needed) k 4 = T k =k 4 =0 B k = O k=00 k=000 k=00 k=000 k=0000 k=000 7 6 4 5 k=00 k=000 k=00 k = L k =k =0 R k = 48
In Step, k ^k = 0, but k 0 or k 0 (or both) Otherwise the segment would be fully visible (Step ) Suppose that k 0, which means that P is outside If not, switch P and P : ß à ; ß à ; k ß à k We need to bring P on the edge of the window Actually, replace P by the intersecon point of the segment P P and the corresponding window edge If k =, find intersecon with the lea edge L If k =, find intersecon with the the right edge R If k =, find intersecon with the booom edge B If k 4 = find intersecon with the the top edge T Go to Step and repeat unl P en P are found Search for intersecons P (,) T Intersecon with L := + (L- ) * ( - ) / ( - ) P (,) B := L L R Intersecon with R: same as above with R instead of L Intersecon with B := + (B- ) * ( - ) / ( - ) := B Intersecon with T: same as above with T instead of B Denominators cannot be equal to 0 (no division by 0) In the first case (bringing on L), this would mean that the segment is vercal, and too much to the lea à already eliminated 49 50 P (, ) P T (, ) B O L R P (, ) P (, ) T B P (, ) P (, ) O L R P (, ) P (, ) 5 5 Polygon clipping Polygon clipping Find the parts of polygons inside the clip window Polygon clipping = scissoring according to clip window Must deal with many different cases Clipping a single polygon can result in mulple polygons 5 54
Sutherland- Hodgman Clipping Algorithm Clipping to one window boundary at a me Sutherland- Hodgman Clipping Algorithm Clipping to one window boundary at a me 55 56 Sutherland- Hodgman Clipping Algorithm Clipping to one window boundary at a me Sutherland- Hodgman Clipping Algorithm Clipping to one window boundary at a me 57 58 Polygon clipping Clipping to one window boundary at a me P 5 P P P 59 60
P 5 P P 5 P P P P P 6 6 P 5 P P 5 P P P P P P 6 64 P 5 P P 5 P P P P P P P 65 66
Outside window Inside window Outside window Inside window P 5 P P P P P P P P P 67 68 Inside test Outside the clip rectangle Inside the clip rectangle Summary Scan conversion of lines: midpoint line algorithm P E Scan conversion of circles and ellipses: use symmetries P P N i (P - P E ) > 0 (outside) Clipping in D: Cohen- Sutherland algorithm based on assigning 4- bit code words to the end points of the line P N i (P - P E ) = 0 (on edge) Clipping polygons inside test N i N i (P - P E ) < 0 (inside) Define edge s outward normal N i For a point P i test the sign of the dot product N i (P i - P E ) 69 70