Dr. Scott Schaefer Scan Conversion of Lines
2/78 Displays – Cathode Ray Tube
3/78 Displays – Liquid Crystal Displays
4/78 Displays – Plasma
5/78 Displays – Pixels Pixel: the smallest element of picture - Integer position (i,j) - Color information (r,g,b) x y (0,0)
6/78 Problem Given two points (P, Q) on the screen (with integer coordinates) determine which pixels should be drawn to display a unit width line
7/78 Problem Given two points (P, Q) on the screen (with integer coordinates) determine which pixels should be drawn to display a unit width line
8/78 Problem Given two points (P, Q) on the screen (with integer coordinates) determine which pixels should be drawn to display a unit width line
9/78 Special Lines - Horizontal
10/78 Special Lines - Horizontal Increment x by 1, keep y constant
11/78 Special Lines - Vertical
12/78 Special Lines - Vertical Keep x constant, increment y by 1
13/78 Special Lines - Diagonal
14/78 Special Lines - Diagonal Increment x by 1, increment y by 1
15/78 How about Arbitrary Lines?
16/78 Slope-intercept equation for a line: Arbitrary Lines m: slope b: y-intercept
17/78 Slope-intercept equation for a line: How can we compute the equation from (x L,y L ), (x H,y H )? Arbitrary Lines m: slope b: y-intercept
18/78 Slope-intercept equation for a line: How can we compute the equation from (x L,y L ), (x H,y H )? Arbitrary Lines m: slope b: y-intercept
19/78 Arbitrary Lines Assume Other lines by swapping x, y or negating Take steps in x and determine where to fill y
20/78 Simple Algorithm Start from (x L, y L ) and draw to (x H, y H ) where x L < x H ( x 0, y 0 ) = ( x L, y L ) For ( i = 0; i <= x H -x L ; i++ ) DrawPixel ( x i, Round ( y i ) ) x i+1 = x i + 1 y i+1 = m x i+1 + b
21/78 Simple Algorithm Start from (x L, y L ) and draw to (x H, y H ) where x L < x H ( x 0, y 0 ) = ( x L, y L ) For ( i = 0; i <= x H -x L ; i++ ) DrawPixel ( x i, Round ( y i ) ) x i+1 = x i + 1 y i+1 = m ( x i + 1 ) + b
22/78 Simple Algorithm Start from (x L, y L ) and draw to (x H, y H ) where x L < x H ( x 0, y 0 ) = ( x L, y L ) For ( i = 0; i <= x H -x L ; i++ ) DrawPixel ( x i, Round ( y i ) ) x i+1 = x i + 1 y i+1 = m x i + m + b
23/78 Simple Algorithm Start from (x L, y L ) and draw to (x H, y H ) where x L < x H ( x 0, y 0 ) = ( x L, y L ) For ( i = 0; i <= x H -x L ; i++ ) DrawPixel ( x i, Round ( y i ) ) x i+1 = x i + 1 y i+1 = m x i + b + m
24/78 Simple Algorithm Start from (x L, y L ) and draw to (x H, y H ) where x L < x H ( x 0, y 0 ) = ( x L, y L ) For ( i = 0; i <= x H -x L ; i++ ) DrawPixel ( x i, Round ( y i ) ) x i+1 = x i + 1 y i+1 = m x i + b + m
25/78 Simple Algorithm Start from (x L, y L ) and draw to (x H, y H ) where x L < x H ( x 0, y 0 ) = ( x L, y L ) For ( i = 0; i <= x H -x L ; i++ ) DrawPixel ( x i, Round ( y i ) ) x i+1 = x i + 1 y i+1 = y i + m
26/78 Simple Algorithm - Example
27/78 Simple Algorithm - Example
28/78 Simple Algorithm - Example
29/78 Simple Algorithm - Example
30/78 Simple Algorithm - Example
31/78 Simple Algorithm - Example
32/78 Simple Algorithm - Example
33/78 Simple Algorithm - Example
34/78 Simple Algorithm - Example
35/78 Simple Algorithm - Problems Floating point operations Rounding Can we do better? ( x 0, y 0 ) = ( x L, y L ) For ( i = 0; i <= x H -x L ; i++ ) DrawPixel ( x i, Round ( y i ) ) x i+1 = x i + 1 y i+1 = y i + m
36/78 Midpoint Algorithm Given a point just drawn, determine whether we move E or NE on next step
37/78 Midpoint Algorithm Given a point just drawn, determine whether we move E or NE on next step Is the line above or below ? Below: move E Above: move NE
38/78 Midpoint Algorithm
39/78 Midpoint Algorithm – Implicit Forms How do we tell if a point is above or below the line?
40/78 Midpoint Algorithm – Implicit Forms How do we tell if a point is above or below the line?
41/78 Midpoint Algorithm – Implicit Forms How do we tell if a point is above or below the line?
42/78 Midpoint Algorithm – Implicit Forms How do we tell if a point is above or below the line?
43/78 Midpoint Algorithm – Implicit Forms How do we tell if a point is above or below the line?
44/78 Midpoint Algorithm – Implicit Forms How do we tell if a point is above or below the line?
45/78 Midpoint Algorithm – Implicit Forms How do we tell if a point is above or below the line? Properties f(x,y)=0 (x,y) on the line f(x,y)<0 (x,y) below the line f(x,y)>0 (x,y) above the line
46/78 Midpoint Algorithm – Implicit Forms
47/78 Midpoint Algorithm – Implicit Forms
48/78 Midpoint Algorithm – Implicit Forms
49/78 Midpoint Algorithm – Implicit Forms
50/78 Midpoint Algorithm – Implicit Forms
51/78 Midpoint Algorithm – Implicit Forms
52/78 Need value of to decide E or NE Build incremental algorithm Assume we have value of Find value of if E chosen Find value of if NE chosen Midpoint Algorithm
53/78 Midpoint Algorithm Need value of to decide E or NE Build incremental algorithm Assume we have value of Find value of if E chosen Find value of if NE chosen
54/78 Midpoint Algorithm If E was chosen, find
55/78 Midpoint Algorithm If E was chosen, find
56/78 Midpoint Algorithm If NE was chosen, find
57/78 Midpoint Algorithm If NE was chosen, find
58/78 Midpoint Algorithm What about starting value?
59/78 Midpoint Algorithm What about starting value?
60/78 Midpoint Algorithm What about starting value? is on the line!
61/78 Midpoint Algorithm What about starting value?
62/78 Midpoint Algorithm What about starting value? Multiplying coefficients by 2 doesn’t change sign of f
63/78 Midpoint Algorithm – Summary x=x L y=y L d=x H - x L c=y L - y H sum=2c+d draw(x,y) while ( x < x H ) if ( sum < 0 ) sum += 2d y++ x++ sum += 2c draw(x,y)
64/78 Midpoint Algorithm – Summary x=x L y=y L d=x H - x L c=y L - y H sum=2c+d //initial value draw(x,y) while ( x < x H ) if ( sum < 0 ) sum += 2d y++ x++ sum += 2c draw(x,y)
65/78 Midpoint Algorithm – Summary x=x L y=y L d=x H - x L c=y L - y H sum=2c+d //initial value draw(x,y) while ( x < x H ) // iterate until reaching the end point if ( sum < 0 ) sum += 2d y++ x++ sum += 2c draw(x,y)
66/78 Midpoint Algorithm – Summary x=x L y=y L d=x H - x L c=y L - y H sum=2c+d //initial value draw(x,y) while ( x < x H ) // iterate until reaching the end point if ( sum < 0 ) // below the line and choose NE sum += 2d y++ x++ sum += 2c draw(x,y)
67/78 Midpoint Algorithm – Example x=x L y=y L d=x H - x L c=y L - y H sum=2c+d draw(x,y) while ( x < x H ) if ( sum < 0 ) sum += 2d y++ x++ sum += 2c draw(x,y)
68/78 Midpoint Algorithm – Example x=x L y=y L d=6 c=-2 sum=2c+d draw(x,y) while ( x < x H ) if ( sum < 0 ) sum += 2d y++ x++ sum += 2c draw(x,y)
69/78 Midpoint Algorithm – Example x=x L y=y L d=6 c=-2 sum=2c+d draw(x,y) while ( x < x H ) if ( sum < 0 ) sum += 2d y++ x++ sum += 2c draw(x,y)
70/78 Midpoint Algorithm – Example x=x L y=y L d=6 c=-2 sum=2c+d draw(x,y) while ( x < x H ) if ( sum < 0 ) sum += 2d y++ x++ sum += 2c draw(x,y)
71/78 Midpoint Algorithm – Example x=x L y=y L d=6 c=-2 sum=2c+d draw(x,y) while ( x < x H ) if ( sum < 0 ) sum += 2d y++ x++ sum += 2c draw(x,y)
72/78 Midpoint Algorithm – Example x=x L y=y L d=6 c=-2 sum=2c+d draw(x,y) while ( x < x H ) if ( sum < 0 ) sum += 2d y++ x++ sum += 2c draw(x,y)
73/78 Midpoint Algorithm – Example x=x L y=y L d=6 c=-2 sum=2c+d draw(x,y) while ( x < x H ) if ( sum < 0 ) sum += 2d y++ x++ sum += 2c draw(x,y)
74/78 Midpoint Algorithm – Example x=x L y=y L d=6 c=-2 sum=2c+d draw(x,y) while ( x < x H ) if ( sum < 0 ) sum += 2d y++ x++ sum += 2c draw(x,y)
75/78 Midpoint Algorithm – Example x=x L y=y L d=6 c=-2 sum=2c+d draw(x,y) while ( x < x H ) if ( sum < 0 ) sum += 2d y++ x++ sum += 2c draw(x,y)
76/78 Midpoint Algorithm Only integer operations Exactly the same as the more commonly found Bresenham’s line drawing algorithm Extends to other types of shapes (circles)
77/78 Circle, ellipse, etc… Need implicit forms - circle: - ellipse: Equations for incremental calculations Initial positions More details in section of the textbook
78/78 OpenGL: Drawing Lines glBegin (GL_LINES); glVertex2f (p1.x, p1.y); glVertex2f (p2.x, p2.y); glVertex2f (p3.x, p3.y); glVertex2f (p4.x, p4.y); glVertex2f (p5.x, p5.y); glVertex2f (p6.x, p6.y); glEnd(); P1 P2P3 P4 P5P6