4 connectivity 4 adjacency 8 connectivity 8 adjacency an
play

4-connectivity 4-adjacency 8-connectivity 8-adjacency An - PDF document

2D Line Scan Conversion Implicit representation: x y 0 Lines and Circles Explicit representation: y y 1 0 y mx B m (Chapter 3 in Foley & Van Dam) x x 1 0


  1. 2D Line Scan Conversion • Implicit representation: x y 0 � � � � � � Lines and Circles • Explicit representation: y y � 1 0 y mx B m � � � (Chapter 3 in Foley & Van Dam) x x � 1 0 • Parametric representation: x � � P � y P P ( P P ) t t [ 0 .. 1 ] � � � � � � � � 0 1 0 � � y P 1 y 1 x 0 x x 1 B y 0 P 0 Scan Conversion - Lines Scan Conversion - Lines y = mx + B y = mx + B Basic naïve algorithm (x 1 ,y 1 ) For x = x 0 to x 1 y = mx + B PlotPixel (x,round(y)) (x 0 ,y 0 ) slope = m = y 1 - y 0 end; x 1 - x 0 offset= B = y 1 -mx 1 Assume |m| d 1 For each iteration: 1 float multiplication, 1 addition, 1 Round Assume x 0 d x 1 4-connectivity 4-adjacency 8-connectivity 8-adjacency

  2. An 8-conncected closed A 4-connected open arc curve with a hole with a hole Incremental Algorithm: Symmetric Cases: |m| t 1 y i+1 = mx i+1 + B = m(x i + ' x) + B = y i + m ' x x = x 0 if ' x = 1 then y i+1 = y i + m For y = y 0 to y 1 PlotPixel(round(x),y) x = x + 1/m end; Special Cases: Algorithm m = ± 1 (diagonals) y=y 0 m = 0, f (horizontal, vertical) For x = x 0 to x 1 PlotPixel(x,round(y)) Symmetric Cases: y = y + m end; if x 0 > x 1 for |m| d 1 or y 0 > y 1 for |m| t 1 swap((x 0 ,y 0 ),(x 1 ,y 1 )) Basic Line Drawing : For each iteration: 1 addition, 1 Round. Drawback: • Accumulated error • float arithmetic • Round operations Pseudo Code for Basic Line Drawing: Assume x 1 >x 0 and line slope absolute value is d 1 ( x i +1,Round(y i +m) ) Line(x 0 ,y 0 ,x 1 ,y 1 ) begin float dx, dy, x, y, slope; dx := x 1 -x 0 ; dy := y 1 -y 0 ; slope := dy/dx; ( x i , y i ) y := y 0 ; ( x i +1, y i +m ) for x:=x 0 to x 1 do begin PlotPixel( x,Round(y) ); ( x i ,Round(y i ) ) y := y+slope; end; end;

  3. Midpoint (~Bresenham) Line Drawing Bresenham’s Line Algorithm Assumptions: • x 0 < x 1 , y 0 < y 1 • 0 < slope < 1 y i+1 d2 y d1 y i NE Q M x i x i+1 E (x p ,y p ) Given (x p ,y p ): next pixel is E = (x p +1,y p ) or NE = (x p +1,y p +1) y = m(x i + 1) + b Bresenham : sign(M-Q) determines NE or E d 1 = y - y i = m(x i +1) + b - y i d 2 = (y i + 1) - y = y i + 1 - m (x i + 1) - b M = (x p +1,y p +1/2) d1 - d2 > 0 ? The vertical distance is equivalent to the Euclidean distance Bresenham’s Line Algorithm Bresenham’s Line Algorithm d1 - d2 = 2m(x i + 1) - 2y i + 2b -1 Const1 = 2dy; d1 - d2 = 2(dy/dx)(x i + 1) - 2y i + 2b -1 Const2 = 2dy - 2dx; f = 2dy - dx; dx(d1-d2) = 2dy*x i + 2dy - 2dx*y i + 2dx*b - dx set_pixel(x1,y1); x = x1; y = y1; f i = dx(d1-d2) f i+1 - f i = 2dy(x i+1 - x i ) - 2dx(y i+1 - y i ) while (x++ < x2){ if (f < 0) If y is incremented then f i+1 = f i + 2dy-2dx f += Const1; else f i+1 = f i + 2dy else { f += Const2; y++; } set_pixel(x,y); } Offsets Bresenham’s Line Algorithm Const1 = 2dy; Const2 = 2dy - 2dx; • The image is a linear memory… p = A + n*y + x; offset_h = sign(dx); offset_d = sign(dx) + n*sign(dy) f = 2dy - dx; *p = color; l-n l-n+1 d8 = dx; while (d8--){ if (f < 0){ f += Const1; Address = l p += offset_h; } else { f += Const2; p += offset_d; } l-n-1 l+n *p = color; }

  4. Mid-point Mid-point In 8-connected choose either a h or d move The line passes above M so it is a d move to The midpoint M is located at (x +1,y +1/2) M M Current pixel Current pixel Mid-point Mid-point The line passes below M so it is a h In 4-connected choose either a h or v move move to The midpoint M is located at (x + 1/2,y +1/2) M M Current pixel Current pixel Mid-point Mid-point The line passes Below M so it is a h The line passes above M so it is a v move to move to M M Current pixel Current pixel

  5. Midpoint Line Drawing (cont.) How to update f - the value at M dy y = x + B dx M Implicit form of a line: M M f(x,y) = ax + by + c = 0 f(x,y) = dy x - dx y + B dx = 0 If was chosen Mi = (x,y) Mi+1 = (x+1,y), thus f f(x,y) < 0 = ax + by + c, and i f(x,y) = 0 f � = a(x+1) + by + c, or f(x,y) > 0 i 1 f � f = + a. i 1 i Since a is constant we denote Decision Variable : it with ' h, and we have: f = f(M) = f(x p +1,y p +1/2) = a(x p +1) + b(y p +1/2) + c f += ' h The sign of f defines the move Incremental Algorithm: How to update f - the value at M Initialization: First point = (x 0 ,y 0 ), first MidPoint = (x 0 +1,y 0 +1/2) f start = f(x 0 +1,y 0 +1/2) = a(x 0 +1) + b(y 0 +1/2) +c M = ax 0 + by 0 + c + a + b/2 = f(x 0 ,y 0 ) + a + b/2 = a + b/2 M M d start =dy - dx/2 If was chosen Enhancement: Mi = (x,y) Mi+1 = (x+1,y+1), thus To eliminate fractions, define: f = ax + by + c, and i f(x,y) = 2(ax + by + c) = 0 f � = a(x+1) + b(y+1) + c, or i 1 f � f d start =2dy - dx = + a + b. i 1 i Since a and b are constants we denote their sum with ' d, and we have: f += ' d Midpoint Line Drawing - Summary Mid-point Line Algorithm • The sign of f(x 0 +1,y 0 +1/2) indicates whether to move East or North-East . � h = 2dy; • At the beginning d=f(x 0 +1,y 0 +1/2)=2dy-dx. � d = 2dy - 2dx; • The increment in d (after this step) is: f = 2dy - dx; – If we moved East : � E =2dy set_pixel(x1,y1); – If we moved North-East: � 1( =2dy-2dx x = x1; y = y1; • Comments: while (x++ < x2){ – Integer arithmetic (dx and dy are if (f < 0) integers). f += � h ; – One addition for each iteration. else { – No accumulated errors. f += � d ; y++; } set_pixel(x,y); }

  6. Scan Conversion - Circles Drawing Circles • Implicit representation (centered at the origin with radius R): 2 2 2 x y R 0 Basic Algorithm � � � • Explicit representation: For x = -R to R y = sqrt(R 2 -x 2 ) 2 2 y R x � � � PlotPixel(x,round(y)) • Parametric representation: PlotPixel(x,-round(y)) x R cos t � � end; � � � � t [ 0 .. 2 ] � � � � � � � � � � � y R sin � � t � � � � Comments: • square-root operations are expensive. R • Float arithmetic. • Large gap for x values close to R. x Exploiting Eight-Way Symmetry For a circle centered at the origin: If (x,y) is on the circle then - (y,x) (y,-x) (x,-y) (-x,-y) (-y,-x) (-y,x) (-x,y) are on the circle as well. Therefore we need to compute only one octant (45 o ) segment. (-x,y) (x,y) (-y,x) (y,x) (-y,-x) (y,-x) (-x,-y) (x,-y) Threshold Criteria Circle Midpoint (for one octant) (The circle is located at (0,0) with radius R) d(x,y)=f(x,y) = x 2 + y 2 -R 2 = 0 • We start from (x 0 ,y 0 )=(0,-R). • One can move either h or d. f(x,y) = 0 • Again, f(x,y) will be a decision variable at the midpoint. f(x,y) < 0 f(x,y) > 0

  7. How to update f - the value at M Mid-point If was chosen, as in lines we update M The arc passes above M so it is a v move to Mi+1 = (x+1,y), thus f � 2 2 2 = (x+1) + y - R, i 1 and f � f = + 2x + 1. i 1 i Now, ' h is NOT a constant , M but a linear term, so we update it as well: ' h+1 = 2(x+1) + 1, which is Current pixel ' h+1 = ' h + 2. Similarly if was chosen Mid-point circle (for one octant) Algorithm One may mirror (*), creating the other seven octans. Initialize � h and � d (home exercise) 2 2 2 ( 1 / 2 ) ( R 1 / 2 ) R f = � � � � set_pixel(x = x1,y = y1); while (in the octant){ if (f < 0) { f += � h ; � h += 2 ; � X++; else { f += � v ; � h += 2 ; Y++; } set_pixel(x,y); }

Recommend


More recommend