Presentation is loading. Please wait.

Presentation is loading. Please wait.

©Zachary Wartell, Larry F. Hodges Scan Conversion I Revision 1.6 8/9/2007 Copyright 2006, Dr. Zachary Wartell, UNCC, All Rights Reserved.

Similar presentations


Presentation on theme: "©Zachary Wartell, Larry F. Hodges Scan Conversion I Revision 1.6 8/9/2007 Copyright 2006, Dr. Zachary Wartell, UNCC, All Rights Reserved."— Presentation transcript:

1 ©Zachary Wartell, Larry F. Hodges Scan Conversion I Revision 1.6 8/9/2007 Copyright 2006, Dr. Zachary Wartell, UNCC, All Rights Reserved

2 ©Zachary Wartell, Larry F. Hodges Scan Conversion recall slide 23 and slide 24 from ITCS 4120-Introduction.pptslide 23slide 24 scan conversion – algorithm with parameters: CoordinateGeometryDescription D [IN], // !2D! Raster R [IN/OUT] // typically the framebuffer alter R by setting the appropriate pixel’s data values (RGB, etc) to represent pixels of D scan conversion of –1D geometry line segments curves (circle, ellipse, etc.) –2D geometry axis-aligned rectangle polygons b a D=Line(a,b) a b Pixels: P 0,0, P 1,1, P 2,2, P 3,2, …. P 7,7 Scan-Conversion

3 ©Zachary Wartell, Larry F. Hodges (0,0) 1 2 34 1 2 3 4 0.5 1.5 2.5 3.5 0.5 1.5 2.5 3.5 0 1 2 3 1 2 3 0.5 1.5 2.5 3.5 0.5 1.5 2.5 3.5 0 VS Center Lower-Left Corner (2.5,3.5) (2,3) LLC: Area of pixel (x,y) is square with lower-left corner at (x,y) (OpenGL) Scan Convert a Point (2,3) (2.5,3.5) - location of pixel address - location of “diagonal” point Address-Coordinate Mapping Conventions

4 ©Zachary Wartell, Larry F. Hodges 4 4 (0,0) (Lower-Left Corner) 4 4 0 (Center) 0 Scan Convert a Vertical/Horizontal Line 4 4 (Lower Left Corner) Pixel Length = Coordinate Length 4 = 4 (0,0) Line 1: ((0,0),(4,0)) Line 2: ((4,0),(4,4)) Overlap  No Overlap  Pixel Length ≠ Coordinate Length 5 ≠ 4 Gap  (OpenGL)

5 ©Zachary Wartell, Larry F. Hodges 3 3 0 (Center) 0 Scan Convert an Axis-Aligned Rectangle  Pixel Width & Area ≠ Coord. Width & Area 4 ≠ 3, 16 ≠ 9 Rect 1: ((0,0),(3,3)) Rect 2: ((3,0),(6,3)) (Lower Left Corner) (OpenGL) (Lower Left Corner) 3 3 (0,0) 3 3 Overlap  No Overlap Pixel Width = Coord. Width Pixel Area = Coord. Area (3 = 3, 9 = 9)

6 ©Zachary Wartell, Larry F. Hodges Scan Convert Example (LLC/OpenGL) Rect 1: ((0,0),(4,4)) 4 4 (0,0) Line 1: ((4,0),(4,4)) (0,0) 4 4 1) 2) Line 2: ((4,4),(0,4)) (0,0) 4 4 3) Line 3: ((0,4),(0,0)) (0,0) 4 4 4) Line 4: ((0,0),(4,0)) (0,0) 4 4 5) Overlap  Gap 

7 ©Zachary Wartell, Larry F. Hodges So do we go with Center or Lower-Left Corner? It depends … –actual API’s specification (such as OpenGL) are generally equivalent to the lower-left corner convention easy to mathematically specify adjacent, filled primitives’ pixels don’t overlap –but when first learning scan conversion algorithms its easier to use the center convention

8 ©Zachary Wartell, Larry F. Hodges Explicit (Slope-Intercept) Equation Slope-Intercept Formula For A Line Given a third point on the line: P = (X,Y) Slope: (Y - Y 1 )/(X - X 1 ) = (Y 2 - Y 1 )/(X 2 - X 1 ) Solving For Y Y = [(Y 2 -Y 1 )/(X 2 -X 1 )]X + [-(Y 2 -Y 1 )/(X 2 -X 1 )]X 1 + Y 1 or Y = mx + b Cartesian Coordinate System 2 4 1234 5 6 3 5 6 1 P1 = (X1,Y1) P2 = (X2,Y2) P = (X,Y) SLOPE = RISE RUN = Y2-Y1 X2-X1 L

9 ©Zachary Wartell, Larry F. Hodges Implicit Equation Cartesian Coordinate System 2 4 1234 5 6 3 5 6 1 P1 = (X1,Y1) P2 = (X2,Y2) F(X,Y) = 0 F(x,y): mx + b – y = 0 For arbitrary (x,y): F(x,y) < 0 ↔ (x,y) above L F(x,y) = 0 ↔ (x,y) on L F(x,y) > 0 ↔ (x,y) below L F(X,Y) < 0 F(X,Y) > 0 L

10 ©Zachary Wartell, Larry F. Hodges Parametric Form of a 2D Line Equation Given points P 1 = (X 1, Y 1 ) and P 2 = (X 2, Y 2 ) X = X 1 + t(X 2 -X 1 ) Y = Y 1 + t(Y 2 -Y 1 ) t is called the parameter. When t = 0 we get (X 1,Y 1 ) t = 1 we get (X 2,Y 2 ) As 0 < t < 1 we get all the other points on the line segment between (X 1,Y 1 ) and (X 2,Y 2 ).

11 ©Zachary Wartell, Larry F. Hodges Other Helpful Formulas Length of line segment between P 1 and P 2 : L = [ (X 2 -X 1 ) 2 + (Y 2 -Y 1 ) 2 ] 1/2 Midpoint of a line segment between P 1 and P 3 : P 2 = ( (X 1 +X 3 )/2, (Y 1 +Y 3 )/2 ) Two lines are perpendicular iff 1) M 1 = -1/M 2 2) Cosine of the angle between them is 0.

12 ©Zachary Wartell, Larry F. Hodges Basic Line and Circle Algorithms 1. Must compute integer coordinates of pixels which lie on or near a line or circle. 2. Pixel level algorithms are invoked millions or billions of times when an image is created or modified. 3. Lines must create visually satisfactory images. Lines should appear straight Lines should terminate accurately Lines should have constant density Line density should be independent of line length and angle. Lines sharing vertex should avoid/minimize overlap Lines sharing vertex should avoid/minimize gaps! 4. Line algorithm should always be defined.

13 ©Zachary Wartell, Larry F. Hodges Simple DDA Line Algorithm -Based on the parametric equation of a line: P = P 1 + t(P 2 -P 1 ) Procedure DDA(X1,Y1,X2,Y2 :Integer); Var Length, I:Integer; X,Y,Xinc,Yinc:Real; Begin Length := ABS(X2 - X1); If ABS(Y2 - Y1) > Length Then Length := ABS(Y2-Y1); Xinc := (X2 - X1)/Length; Yinc := (Y2 - Y1)/Length; X := X1; Y := Y1; For I := 0 To Length Do Begin Plot(Round(X), Round(Y)); X := X + Xinc; Y := Y + Yinc End {For} End; {DDA} avoid gaps

14 ©Zachary Wartell, Larry F. Hodges DDA Example Compute which pixels should be turned on to represent the line from (6,9) to (11,12). Length := Max of (ABS(11-6), ABS(12-9)) = 5 Xinc := 1 Yinc := 0.6 Values computed are: (6,9), (7,9.6), (8,10.2), (9,10.8), (10,11.4), (11,12) DDA creates good lines but it is too time consuming due to the round function and long operations on floating point values.

15 ©Zachary Wartell, Larry F. Hodges The Midpoint Method Assumptions: line with slope m between 0 and 1 (i.e. line lies in first octant). The general formula for a line is y = mx + b where m is the slope of the line and b is the y-intercept. The other octants algorithms are similar based on symmetry. Also we distinguish between x-major (|Δx|>|Δy|) and y-major (|Δy|>|Δx| lines.

16 ©Zachary Wartell, Larry F. Hodges Bresenham Line (Midpoint formulation) xkxk ykyk y k +1 x k+1 = x k +1 P k +1 L: y = mx + b PkPk ? P k – current pixel at ( x k,y k ) P k +1 – next pixel at ( x k+1,y k+1 )

17 ©Zachary Wartell, Larry F. Hodges From P k :( x k, y k ) do we step to P α: :( x k +1, y k ) or P β :( x k +1, y k +1) ? -Is P k+1 ( x k, y k +1 ) = P α or P β -Is P ά or P β closer to line L? d l > d u ↔ Δ d > 0 -Is mid-point M k below or above line L? f( x k +1, y k + ½) > 0 Bresenham Line (Midpoint formulation) xkxk ykyk y k +1 x k+1 = x k +1 PβPβ PαPα MkMk L: y = mx + b PkPk dudu dldl y = m ( x k +1)+ b Δd=dl-duΔd=dl-du Distance=1 y k +1 = (?)

18 ©Zachary Wartell, Larry F. Hodges Goal: Is mid-point M k below or above line L? f( x k +1, y k + ½) > 0 f( x k +1, y k + ½) = m ( x k +1) + b – ( y k + ½) = Δ y / Δ x ( x k +1) + b – ( y k + ½) Insight #1: avoid fractions, so use: g(…) > 0, K > 0 → K * g(…) > 0 Let K = 2 Δ x and define “decision parameter” p k p k = 2 Δ x * f(…) = 2 Δ y ( x k +1) + 2 Δ x b – Δ x (2 y k + 1) = 2 Δ y x k - 2 Δ x y k + [2 Δ y + Δ x (2 b -1)] (1) Avoid Fractions pkpk

19 ©Zachary Wartell, Larry F. Hodges Avoid Multiplications – Base Case Insight #2: Formulate as only iterative additions instead of multiplications #2.1 Base Case: p 0 = 2 Δ y x 0 - 2 Δ x y 0 + [2 Δ y + Δ x (2 b -1)] = 2 Δ y - Δ x p 0 = 2 Δ y - Δ x

20 ©Zachary Wartell, Larry F. Hodges Insight #2 : Formulate as iterative addition instead of multiplication #2.2 Induction Case: Compute p k +1 from p k p k = 2 Δ y x k - 2 Δ x y k + [2 Δ y + Δ x (2 b -1)] (2) from (1) [18]18 p k +1 = 2 Δ y x k +1 - 2 Δ x y k +1 + [2 Δ y + Δ x (2 b -1)] (3) so p k +1 = p k + 2 Δ y - 2 Δ x ( y k +1 – y k ) - from (2) and (3) yields 2 situations: i) y k +1 – y k = 0 iff we step to P α ii) y k +1 – y k = 1 iff we step to P β Avoid Multiplications – Induction Case Figure 20: “ y k +1 – y k =0” situation

21 ©Zachary Wartell, Larry F. Hodges Avoid Multiplications – Induction Case (Situation ii) Figure 21: “ y k +1 – y k =1” situation Insight #2 : Formulate as iterative addition instead of multiplication #2.2 Induction Case: Compute p k +1 from p k p k = 2 Δ y x k - 2 Δ x y k + [2 Δ y + Δ x (2 b -1)] (2) from (1) [18]18 p k +1 = 2 Δ y x k +1 - 2 Δ x y k +1 + [2 Δ y + Δ x (2 b -1)] (3) so p k +1 = p k + 2 Δ y - 2 Δ x ( y k +1 – y k ) - from (2) and (3) yields 2 situations: i) y k +1 – y k = 0 iff we step to P α ii) y k +1 – y k = 1 iff we step to P β

22 ©Zachary Wartell, Larry F. Hodges Bresenham’s Line Algorithm Begin {Bresenham for lines with slope between 0 and 1} Δx := ABS(xend - xstart); Δy := ABS(yend - ystart); p := 2*Δy - Δx; If xstart > xend Then Begin x := xend; y := yend End Else Begin x := xstart; y := ystart End; For I := 0 to Δx Do Begin Plot(x,y); x := x + 1; If p  0 Then Begin y := y + 1; p := p + 2*(Δy- Δx); Else p := p + 2*Δy End End {For Loop} End; {Bresenham}

23 ©Zachary Wartell, Larry F. Hodges In Class Exercise 3,5 9,9 Line: ((3,5),(9,9)) Ipstepxy 0 1 2 3 4 5 6 xstart =, ystart = xend =, yend = Δx =, 2*(Δy- Δx) = Δy =, 2*Δy = p 0 = 2*Δy- Δx =  { major, major_minor }

24 ©Zachary Wartell, Larry F. Hodges In Class Exercise (Answer) 3,5 9,9 Line: ((3,5),(9,9)) xstart = 3, ystart = 5 xend = 9, yend = 9 Δx = 6, 2*(Δy- Δx) = -4 Δy = 4, 2*Δy = 8 p 0 = 2*Δy- Δx = 2 Ipstepxy 02 major_minor 35 1-2 major 46 26 major_minor 56 32 67 4-2 major 78 56 major_minor 88 62 99

25 ©Zachary Wartell, Larry F. Hodges Generalizing to other Octant’s

26 ©Zachary Wartell, Larry F. Hodges Generalizing to other Octant’s octantmajorminormidpoint offset major-minor step cond. p0p0 Δp major Δp major_minor 1x +=1y +=1(+1,+1/2)p > 02*Δy - Δx2*Δy2*(Δy- Δx) 2 …………………… 8

27 ©Zachary Wartell, Larry F. Hodges Simple Circle Algorithms Since the equation for a circle on radius r centered at (0,0) is x 2 + y 2 = r 2, an obvious choice is to plot y = ±(r 2 - x 2 ) 1/2 as -r ≤ x ≤ r. This works, but is inefficient because of the multiplications and square root operations. It also creates large gaps in the circle for values of x close to R. A better approach, which is still inefficient but avoids the gaps is to plot x = r cosø y = r sinø as ø takes on values between 0 and 360 degrees.

28 ©Zachary Wartell, Larry F. Hodges Circle Drawing Algorithm We only need to calculate the values on the border of the circle in the first octant. The other values may be determined by symmetry. Assume a circle of radius r with center at (0,0). Procedure Circle_Points(x,y :Integer); Begin Plot(x,y); Plot(y,x); Plot(y,-x); Plot(x,-y); Plot(-x,-y); Plot(-y,-x); Plot(-y,x); Plot(-x,y) End;

29 ©Zachary Wartell, Larry F. Hodges Fast Circles Consider only the second octant of a circle of radius r centered on the origin. We begin by plotting point (0, r ) and end when x > y. The decision at each step is whether to choose the pixel directly right the current pixel or the pixel which is below and to the right (8-way stepping). Assume P k = ( x k, y k ) is the current pixel. P α = ( x k +1, y k ) is the pixel directly right P β = ( x k +1, y k -1) is the pixel right and below y=xy=x x 2 + y 2 – r 2 = 0

30 ©Zachary Wartell, Larry F. Hodges Fast Circles - The Decision Variable Implicit Equation: f(x,y) = x 2 + y 2 - r 2 f(x,y) = 0 ↔ (x,y) on C f(x,y) < 0 ↔ (x,y) in C (“below”) f(x,y) > 0 ↔ (x,y) outside C (“above”) M k : f(x k + 1, y k - 1/2) = (x k + 1) 2 + (y k - 1/2) 2 - r 2 p k = (x k + 1) 2 + (y k - 1/2) 2 - r 2 What about p k+1 ? p k+1 = (x k+1 + 1) 2 + (y k+1 – 1/2) 2 - r 2 = [(x k + 1) + 1] 2 + (y k+1 – 1/2) 2 - r 2 = p k + 2(x k + 1) + (y 2 k+1 - y 2 k ) – (y k+1 - y k ) + 1  y k+1 = y k iff we step to P α  y k+1 = y k - 1 iff we step to P β C: x 2 + y 2 - r 2 =0

31 ©Zachary Wartell, Larry F. Hodges Fast Circles - Decision Variable (cont.) The initial value of p k is p 0 = f(1,r - 1/2) = 1+ (r - 1/2) 2 - r 2 = 5/4 - r {1-r can be used if r is an integer} When point P α = (x k + 1, y k ) is chosen then p k+1 = p k + 2x k+1 + 1 When point P β = (x k + 1, y k - 1) is chosen then p k+1 = p k + 2x k+1 + 1 – 2y k+1

32 ©Zachary Wartell, Larry F. Hodges Fast Circle Algorithm Begin {Circle} x := 0; y := r; p := 1 - r; Repeat Circle_Points(x,y); x := x + 1; If p < 0 Then p := p + 2*x + 1 Else Begin y := y - 1; p := p + 2*(x-y) + 1 End Until x > y End; {Circle} y=x x 2 + y 2 – r 2 = 0

33 ©Zachary Wartell, Larry F. Hodges Revisions 1.5 –typos –fixing math variable fonts –added answer slide to In Class Exercise –Dumped #34, #35, #36 Antialiasing – Solutions 1.6 –Dumped slide 33 on ellipsoid rasterization, want to spend class time on other things


Download ppt "©Zachary Wartell, Larry F. Hodges Scan Conversion I Revision 1.6 8/9/2007 Copyright 2006, Dr. Zachary Wartell, UNCC, All Rights Reserved."

Similar presentations


Ads by Google