CS 4204 Computer Graphics Scan Conversion Scan Conversion Yong Cao Yong Cao Virginia Tech Virginia Tech References: References: “Introduction to Computer Graphics Introduction to Computer Graphics” ” course notes by course notes by Petros Petros Faloutsos Faloutsos, UCLA , UCLA “
Primitives Representations for Representations for Representations for Lines and Curves Lines and Curves Lines and Curves
Representations for lines and Curves Line (in 2D) Line (in 2D) Line (in 2D) • Explicit Explicit • • Explicit • Implicit Implicit • • Implicit • Parametric Parametric • • Parametric
Circle • Explicit Explicit • • Explicit • Implicit Implicit • • Implicit • Parametric Parametric • • Parametric
Rasterization
Line rasterization
Line rasterization Desired properties Desired properties Desired properties • Straight Straight • • Straight • Pass through end points Pass through end points • • Pass through end points • Smooth Smooth • • Smooth • Independent of end point Independent of end point • • Independent of end point order order order • Uniform brightness Uniform brightness • • Uniform brightness • Brightness independent of Brightness independent of • • Brightness independent of slope slope slope • Efficient • Efficient • Efficient
Straightforward Implementation Line between two points Line between two points Line between two points DrawLine(int x1,int y1,int x2,int y2) x1,int y1,int x2,int y2) DrawLine(int DrawLine(int x1,int y1,int x2,int y2) { { { float y; float y; float y; int x; x; int int x; for (x=x1; x<=x2; x++) { for (x=x1; x<=x2; x++) { for (x=x1; x<=x2; x++) { y = y1 + (x- -x1)*(y2 x1)*(y2- -y1)/(x2 y1)/(x2- -x1) x1) y = y1 + (x y = y1 + (x-x1)*(y2-y1)/(x2-x1) SetPixel(x, Round(y) ); , Round(y) ); SetPixel(x SetPixel(x, Round(y) ); } } } } } }
Better Implementation How can we improve this algorithm? How can we improve this algorithm? How can we improve this algorithm? DrawLine(int x1,int y1,int x2,int y2) x1,int y1,int x2,int y2) DrawLine(int DrawLine(int x1,int y1,int x2,int y2) { { { float y; float y; float y; int x; x; int int x; for (x=x1; x<=x2; x++) { for (x=x1; x<=x2; x++) { for (x=x1; x<=x2; x++) { y = y1 + (x- -x1)*(y2 x1)*(y2- -y1)/(x2 y1)/(x2- -x1) x1) y = y1 + (x y = y1 + (x-x1)*(y2-y1)/(x2-x1) SetPixel(x, Round(y) ); , Round(y) ); SetPixel(x SetPixel(x, Round(y) ); } } } } } }
Better Implementation DrawLine(int x1,int y1,int x2,int y2) x1,int y1,int x2,int y2) DrawLine(int DrawLine(int x1,int y1,int x2,int y2) { { { float y,m y,m; ; float float y,m; int x; x; int int x; dx = x2 = x2- -x1 ; x1 ; dx dx = x2-x1 ; dy = y2 = y2- -y1 ; y1 ; dy dy = y2-y1 ; m = dy dy/ (float) / (float) dx dx ; ; m = m = dy/ (float) dx ; for (x=x1; x<=x2; x++) { for (x=x1; x<=x2; x++) { for (x=x1; x<=x2; x++) { y = y1 + m*(x- -x1) ; x1) ; y = y1 + m*(x y = y1 + m*(x-x1) ; SetPixel(x, , Round Round(y (y) ); ) ); SetPixel(x SetPixel(x, Round(y) ); } } } } } }
Even Better Implementation DrawLine(int x1,int y1,int x2,int y2) x1,int y1,int x2,int y2) DrawLine(int DrawLine(int x1,int y1,int x2,int y2) { { { float y,m y,m; ; float float y,m; int x; x; int int x; dx = x2 = x2- -x1 ; x1 ; dx dx = x2-x1 ; dy = y2 = y2- -y1 ; y1 ; dy dy = y2-y1 ; m = dy dy/ (float) / (float) dx dx ; ; m = m = dy/ (float) dx ; y = y1 + 0.5 ; y = y1 + 0.5 ; y = y1 + 0.5 ; for (x=x1; x<=x2; x++) { for (x=x1; x<=x2; x++) { for (x=x1; x<=x2; x++) { SetPixel(x, SetPixel(x , Floor Floor(y) ); (y) ); SetPixel(x, Floor(y) ); y = y + m ; y = y + m ; y = y + m ; } } } } } }
Midpoint algorithm (Bresenham) Line in the first quadrant ( 0<slope < 45 deg) Line in the first quadrant ( 0<slope < 45 deg) Line in the first quadrant ( 0<slope < 45 deg) Implicit function: Implicit function: Implicit function: F(x,y) = ) = xdy xdy - - ydx ydx + c, + c, F(x,y F(x,y) = xdy - ydx + c, Pixel Centers dx,dy > 0 and > 0 and dy/dx dy/dx <= 1.0 ; <= 1.0 ; dx,dy dx,dy > 0 and dy/dx <= 1.0 ; • Current choice P = ( Current choice P = (x,y x,y). ). • • Current choice P = (x,y). NE • How do we chose next of P, • How do we chose next of P, • How do we chose next of P, P’ ’= (x+1,y = (x+1,y’ ’) ? ) ? P P’= (x+1,y’) ? E
Midpoint algorithm (Bresenham) Line in the first quadrant ( 0<slope < 45 deg) Line in the first quadrant ( 0<slope < 45 deg) Line in the first quadrant ( 0<slope < 45 deg) Implicit function: Implicit function: Implicit function: F(x,y) = ) = xdy xdy - - ydx ydx + c, + c, F(x,y F(x,y) = xdy - ydx + c, Pixel Centers dx,dy > 0 and > 0 and dy/dx dy/dx <= 1.0 ; <= 1.0 ; dx,dy dx,dy > 0 and dy/dx <= 1.0 ; • Current choice P = ( Current choice P = (x,y x,y). ). • • Current choice P = (x,y). NE • How do we chose next of P, • How do we chose next of P, • How do we chose next of P, P’ ’= (x+1,y = (x+1,y’ ’) ? ) ? P P’= (x+1,y’) ? If( F(M) = F(x+1,y+0.5) < 0) If( F(M) = F(x+1,y+0.5) < 0) If( F(M) = F(x+1,y+0.5) < 0) E M above line so E M above line so E M above line so E else else else M below line so NE M below line so NE M below line so NE
Midpoint algorithm (Bresenham) DrawLine(int x1, x1, int int y1, y1, int int x2, x2, int int y2, y2, int int color) color) DrawLine(int DrawLine(int x1, int y1, int x2, int y2, int color) { { { int x,y,dx,dy int x,y,dx,dy; ; int x,y,dx,dy; y = Round(y1) ; y = Round(y1) ; y = Round(y1) ; for (x=x1; x<=x2; x++) { for (x=x1; x<=x2; x++) { for (x=x1; x<=x2; x++) { SetPixel(x, y ); , y ); SetPixel(x SetPixel(x, y ); NE if (F(x+1,y+0.5)>0) { if (F(x+1,y+0.5)>0) { if (F(x+1,y+0.5)>0) { y = y + 1 ; y = y + 1 ; y = y + 1 ; } } } E } } } } } }
Can we compute F in a smart way? • We are at pixel (x,y) we evaluate F at M = (x+1,y+0.5) and We are at pixel (x,y) we evaluate F at M = (x+1,y+0.5) and • E=(x+1,y) or NE=(x+1,y+1) accordingly. E=(x+1,y) or NE=(x+1,y+1) accordingly. (Reminder: F(x,y) = xdy xdy - - ydx ydx + c) + c) (Reminder: F(x,y) =
Can we compute F in a smart way? • We are at pixel (x,y) we evaluate F at M = (x+1,y+0.5) and We are at pixel (x,y) we evaluate F at M = (x+1,y+0.5) and • E=(x+1,y) or NE=(x+1,y+1) accordingly. E=(x+1,y) or NE=(x+1,y+1) accordingly. (Reminder: F(x,y) = xdy xdy - - ydx ydx + c) + c) (Reminder: F(x,y) = • If we chose E for x+1 the next criteria will be at M If we chose E for x+1 the next criteria will be at M’ ’: : • � +c � F(x+2,y+0.5) = [(x+1)dy +dy dy] ] - - (y+0.5)* (y+0.5)*dx dx +c F(x+2,y+0.5) = [(x+1)dy + dy � � F(x+2,y+0.5) = F(x+1,y+0.5) + dy F(x+2,y+0.5) = F(x+1,y+0.5) + F E = F + dy dy = F+ = F+ dF dF E F E = F + E
Can we compute F in a smart way? • We are at pixel (x,y) we evaluate F at M = (x+1,y+0.5) and We are at pixel (x,y) we evaluate F at M = (x+1,y+0.5) and • E=(x+1,y) or NE=(x+1,y+1) accordingly. E=(x+1,y) or NE=(x+1,y+1) accordingly. (Reminder: F(x,y) = xdy xdy - - ydx ydx + c) + c) (Reminder: F(x,y) = • If we chose E for x+1 the next criteria will be at M If we chose E for x+1 the next criteria will be at M’ ’: : • � +c � F(x+2,y+0.5) = (x+1)dy +dy dy - - (y+0.5)* (y+0.5)*dx dx +c F(x+2,y+0.5) = (x+1)dy + dy � � F(x+2,y+0.5) = F(x+1,y+0.5) + dy F(x+2,y+0.5) = F(x+1,y+0.5) + F E = F + dy dy F E = F + • If we chose NE then the next criteria If we chose NE then the next criteria • will be at M’’ ’’: : will be at M F(x+2,y+1+0.5) = F(x+2,y+1+0.5) = dx � � F(x+1,y+0.5) + dy dy – – dx F(x+1,y+0.5) + F NE = F + dy dy - - dx dx F NE = F +
Can we compute F in a smart way? • We are at pixel (x,y) we evaluate F at M = (x+1,y+0.5) and We are at pixel (x,y) we evaluate F at M = (x+1,y+0.5) and • E=(x+1,y) or NE=(x+1,y+1) accordingly. E=(x+1,y) or NE=(x+1,y+1) accordingly. (Reminder: F(x,y) = xdy xdy - - ydx ydx + c) + c) (Reminder: F(x,y) = • If we chose E for x+1 the next criteria will be at M If we chose E for x+1 the next criteria will be at M’ ’: : • F E = F + dy dy F E = F + • If we chose NE then the next criteria If we chose NE then the next criteria • will be at M’’ ’’: : will be at M F NE = F + dy dy – – dx dx F NE = F +
Criterion update Update Update F E F E = F + = F + dy dy = F + = F + dF dF E E F NE = F + dy dy - - dx dx = F + = F + dF dF NE F NE = F + NE Starting value? Starting value? Line equation: F(x,y F(x,y) = ) = xdy xdy- -ydx+c ydx+c Line equation: Assume line starts at pixel (x 0 ,y 0 ) Assume line starts at pixel (x 0 ,y 0 ) F start = F(x 0 +1,y 0 +0.5) = (x 0 +1)dy - -(y (y 0 +0.5)dx + c = F start = F(x 0 +1,y 0 +0.5) = (x 0 +1)dy 0 +0.5)dx + c = = (x 0 = (x 0 dy dy - - y y 0 0 dx + c )+ dx + c )+ dy dy - - 0.5dx = F (x 0.5dx = F (x 0 0 ,y ,y 0 0 ) + ) + dy dy - - 0.5dx. 0.5dx. (x 0 ,y 0 ) belongs on the line so: F (x 0 ,y 0 ) = 0 (x 0 ,y 0 ) belongs on the line so: F (x 0 ,y 0 ) = 0 Therefore: Therefore: F start F start = = dy dy - - 0.5dx 0.5dx
Recommend
More recommend