1 csce 441 lecture 2: scan conversion of lines jinxiang chai

Post on 19-Jan-2016

227 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1

CSCE 441 Lecture 2:Scan Conversion of Lines

Jinxiang Chai

2/110

Displays – Cathode Ray Tube

3/110

Displays – Liquid Crystal Displays

4/110

Displays – Plasma

5/110

Displays – Pixels

Pixel: the smallest element of picture

- Integer position (i,j)

- Color information (r,g,b)

x

y

(0,0)

6/110

Digital Scan Conversion

Convert graphics output primitives into a set of pixel values for storage in the frame buffer

7/110

OpenGL Geometric Primitives

All geometric primitives are specified by vertices

GL_QUAD_STRIPGL_QUAD_STRIP

GL_POLYGONGL_POLYGON

GL_TRIANGLE_STRIPGL_TRIANGLE_STRIP

GL_TRIANGLE_FANGL_TRIANGLE_FAN

GL_POINTSGL_POINTSGL_LINESGL_LINES

GL_LINE_LOOPGL_LINE_LOOPGL_LINE_STRIPGL_LINE_STRIP

GL_TRIANGLESGL_TRIANGLES

GL_QUADSGL_QUADS

8/110

OpenGL Geometric Primitives

All geometric primitives are specified by vertices

GL_QUAD_STRIPGL_QUAD_STRIP

GL_POLYGONGL_POLYGON

GL_TRIANGLE_STRIPGL_TRIANGLE_STRIP

GL_TRIANGLE_FANGL_TRIANGLE_FAN

GL_POINTSGL_POINTSGL_LINESGL_LINES

GL_LINE_LOOPGL_LINE_LOOPGL_LINE_STRIPGL_LINE_STRIP

GL_TRIANGLESGL_TRIANGLES

GL_QUADSGL_QUADS

9/110

Outline

How to draw a line?

- Digital Differential Analyzer (DDA)

- Midpoint algorithm

Required readings

- HB 6.1 to 6.8

10/110

Problem

Given two points (P, Q) on the screen (with integer coordinates) determine which pixels should be drawn to display a unit width line

P

Q

11/110

Problem

Given two points (P, Q) on the screen (with integer coordinates) determine which pixels should be drawn to display a unit width line

P

Q

12/110

Problem

Given two points (P, Q) on the screen (with integer coordinates) determine which pixels should be drawn to display a unit width line

P

Q

Line-drawing using “Paint”

14/110

Special Lines - Horizontal

15/110

Special Lines - Horizontal

Increment x by 1, keep y constant

16/110

Special Lines - Vertical

17/110

Special Lines - Vertical

Keep x constant, increment y by 1

18/110

Special Lines - Diagonal

19/110

Special Lines - Diagonal

Increment x by 1, increment y by 1

20/110

How about Arbitrary Lines?

How to draw an arbitrary line?

P

Q

21/110

Slope-intercept equation for a line:

Arbitrary Lines

bmxy m: slope

b: y-intercept

22/110

Slope-intercept equation for a line:

How can we compute the equation from (xL,yL), (xH,yH)?

Arbitrary Lines

bmxy m: slope

)( , HH yx

)( , LL yx

b: y-intercept

23/110

Slope-intercept equation for a line:

How can we compute the equation from (xL,yL), (xH,yH)?

Arbitrary Lines

LL

LH

LH

mxyb

xx

yym

bmxy m: slope

)( , HH yx

)( , LL yx

b: y-intercept

24/110

Arbitrary Lines

Assume

bmxy

10 m

10 m

25/110

Arbitrary Lines

Assume Other lines by swapping x, y or negating

- e.g., reverse the role of x and y if m>1

bmxy

10 m

10 m

26/110

Arbitrary Lines

Assume Other lines by swapping x, y or negating

- e.g., reverse the role of x and y if m>1

- e.g., negate y if

bmxy

10 m

10 m

01 m

27/110

Arbitrary Lines

Assume Other lines by swapping x, y or negating

Take steps in x and determine where to fill y

xmy

bmxy

10 m

10 m

28/110

Digital Differential Analyzer

Start from (xL, yL) and draw to (xH, yH)

where xL< xH

( x0, y0 ) = ( xL, yL )

For ( i = 0; i <= xH-xL; i++ )

DrawPixel ( xi, Round ( yi ) )

xi+1 = xi + 1

yi+1 = m xi+1 + b

29/110

Digital Differential Analyzer

Simple algorithm:

- sample unit intervals in one coordinate

- find the nearest pixel for each sampled point.

30/110

Digital Differential Analyzer (DDA)

Start from (xL, yL) and draw to (xH, yH)

where xL< xH

( x0, y0 ) = ( xL, yL )For ( i = 0; i <= xH-xL; i++ ) //sample unit intervals in x coordinate

DrawPixel ( xi, Round ( yi ) ) //find the nearest pixel for each sampled point.

xi+1 = xi + 1

yi+1 = m ( xi + 1 ) + b

31/110

DDA

Start from (xL, yL) and draw to (xH, yH)

where xL< xH

( x0, y0 ) = ( xL, yL )

For ( i = 0; i <= xH-xL; i++ )

DrawPixel ( xi, Round ( yi ) )

xi+1 = xi + 1

yi+1 = m xi + m + b

32/110

DDA

Start from (xL, yL) and draw to (xH, yH)

where xL< xH

( x0, y0 ) = ( xL, yL )

For ( i = 0; i <= xH-xL; i++ )

DrawPixel ( xi, Round ( yi ) )

xi+1 = xi + 1

yi+1 = m xi + b + m

33/110

DDA

Start from (xL, yL) and draw to (xH, yH)

where xL< xH

( x0, y0 ) = ( xL, yL )

For ( i = 0; i <= xH-xL; i++ )

DrawPixel ( xi, Round ( yi ) )

xi+1 = xi + 1

yi+1 = m xi + b + m

34/110

DDA

Start from (xL, yL) and draw to (xH, yH)

where xL< xH

( x0, y0 ) = ( xL, yL )

For ( i = 0; i <= xH-xL; i++ )

DrawPixel ( xi, Round ( yi ) )

xi+1 = xi + 1

yi+1 = yi + m

35/110

DDA - Example

)2,0(

)4,6(

bmxy

36/110

DDA - Example

)2,0(

)4,6(

bmxy

31

0624

LH

LH

xxyym

37/110

DDA - Example

)2,0(

)4,6(

bmxy

2

0

y

x

31

0624

LH

LH

xxyym

38/110

DDA - Example

)2,0(

)4,6(

bmxy

37

1

y

x

31

0624

LH

LH

xxyym

39/110

DDA - Example

)2,0(

)4,6(

bmxy

37

1

y

x

31

0624

LH

LH

xxyym

40/110

DDA - Example

)2,0(

)4,6(

bmxy

38

2

y

x

31

0624

LH

LH

xxyym

41/110

DDA - Example

)2,0(

)4,6(

bmxy

38

2

y

x

31

0624

LH

LH

xxyym

42/110

DDA - Example

)2,0(

)4,6(

bmxy

3

3

y

x

31

0624

LH

LH

xxyym

43/110

DDA - Example

)2,0(

)4,6(

bmxy

3

3

y

x

31

0624

LH

LH

xxyym

44/110

DDA - Example

)2,0(

)4,6(

bmxy

310

4

y

x

31

0624

LH

LH

xxyym

45/110

DDA - Example

)2,0(

)4,6(

bmxy

310

4

y

x

31

0624

LH

LH

xxyym

46/110

DDA - Example

)2,0(

)4,6(

bmxy

311

5

y

x

31

0624

LH

LH

xxyym

47/110

DDA - Example

)2,0(

)4,6(

bmxy

311

5

y

x

31

0624

LH

LH

xxyym

48/110

DDA - Example

)2,0(

)4,6(

bmxy

4

6

y

x

31

0624

LH

LH

xxyym

49/110

DDA - Example

)2,0(

)4,6(

bmxy

4

6

y

x

31

0624

LH

LH

xxyym

50/110

DDA - Problems

Floating point operations Rounding Can we do better?

( x0, y0 ) = ( xL, yL )

For ( i = 0; i <= xH-xL; i++ )

DrawPixel ( xi, Round ( yi ) )

xi+1 = xi + 1

yi+1 = yi + m

51/110

Outline

How to draw a line?

- Digital Differential Analyzer (DDA)

- Midpoint algorithm

52/110

How to Improve It?

)4,6(

bmxy

31

0624

LH

LH

xxyym

Question: if we draw the current pixel, where is the next pixel?

)2,0(

(xk,yk) (xk+1,yk+1)

53/110

How to Improve It?

)4,6(

bmxy

31

0624

LH

LH

xxyym

Question: if we draw the current pixel, where is the next pixel?

)2,0(

(xk,yk) (xk+1,yk+1)

54/110

How to Improve It?

)4,6(

bmxy

31

0624

LH

LH

xxyym

Question: if we draw the current pixel, where is the next pixel?

)2,0(

(xk,yk) (xk+1,yk+1)

55/110

How to Improve It?

)2,0(

)4,6(

bmxy

31

0624

LH

LH

xxyym

Question: if we draw the current pixel, where is the next pixel?

Next column: E or NE direction; why?

xk+1 = xk+1

yk+1 = yk or yk+1

56/110

Midpoint Algorithm

Given a point just drawn, determine whether we move E or NE on next step

xk+1 = xk+1

yk+1 = yk or yk+1

57/110

Midpoint Algorithm

Given a point just drawn, determine whether we move E or NE on next step

Is the line above or below midpoint ?

Below: move E

Above: move NE

),1( 21 yx

58/110

Above/Below the Line?

)2,0(

)4,6(

59/110

Two Issues

How to evaluate if the midpoint is above or below the line?

How to incrementally evaluate this?

How to avoid floating point operations?

60/110

Midpoint Algorithm – Implicit Forms

How do we tell if a point is above or below the line? bmxy

x

y

)2,0(

)5,6(

)7,3(

61/110

Midpoint Algorithm – Implicit Forms

How do we tell if a point is above or below the line? bmxy

x

y

)2,0(

)5,6(

)7,3(Properties

y=mx+b (x,y) on the line

y<mx+b (x,y) below the line

y>mx+b (x,y) above the line

62/110

Midpoint Algorithm – Implicit Forms

How do we tell if a point is above or below the line? bmxy

x

y

)2,0(

)5,6(

)7,3(Properties

y=mx+b (x,y) on the line

y<mx+b (x,y) below the line

y>mx+b (x,y) above the line

But this still requires floating point operations!

63/110

Midpoint Algorithm – Implicit Forms

How do we tell if a point is above or below the line? bmxy

bmxyyxf ),(

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

64/110

Midpoint Algorithm – Implicit Forms

How do we tell if a point is above or below the line?

bxyLH

LH

xxyy

65/110

Midpoint Algorithm – Implicit Forms

How do we tell if a point is above or below the line?

))(()( LHxxyy

LH xxbxyxxLH

LH

66/110

Midpoint Algorithm – Implicit Forms

How do we tell if a point is above or below the line?

bxxxyyyxx LHLHLH )()()(

67/110

Midpoint Algorithm – Implicit Forms

How do we tell if a point is above or below the line?

0)()()( bxxxyyyxx HLHLLH

68/110

Midpoint Algorithm – Implicit Forms

How do we tell if a point is above or below the line?

edycxyxf ),(HL yyc LH xxd )( HL xxbe

0)()()( bxxxyyyxx HLHLLH

69/110

Midpoint Algorithm – Implicit Forms

How do we tell if a point is above or below the line?

edycxyxf ),(HL yyc LH xxd )( HL xxbe

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

0)()()( bxxxyyyxx HLHLLH

70/110

Midpoint Algorithm – Implicit Forms

How do we tell if a point is above or below the line?

edycxyxf ),(HL yyc LH xxd )( HL xxbe

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

0)()()( bxxxyyyxx HLHLLH

LL

LH

LH

mxyb

xx

yym

Note that e is an integer because

71/110

Midpoint Algorithm – Implicit Forms

x

y

)2,0(

)5,6(

263 xy

72/110

Midpoint Algorithm – Implicit Forms

x

y

)2,0(

263 xy

1236),( xyyxf)5,6(

73/110

Midpoint Algorithm – Implicit Forms

x

y

)2,0(

263 xy

1236),( xyyxf 18)5,0( f)5,6(

74/110

Midpoint Algorithm – Implicit Forms

x

y

)2,0(

263 xy

1236),( xyyxf

18)1,4( f

)5,6(

75/110

Midpoint Algorithm – Implicit Forms

x

y

)2,0(

263 xy

1236),( xyyxf

0)4,4( f)5,6(

76/110

Midpoint Algorithm – Implicit Forms

x

y

)2,0(

263 xy

1236),( xyyxf)5,6(

77/110

Two Issues

How to evaluate if the midpoint is above or below the line?

How to incrementally evaluate this?

78/110

Above/Below the Line?

)2,0(

)4,6(

79/110

Midpoint Algorithm

What about starting value?

What is the middle point?

(xL,yL)

80/110

Midpoint Algorithm

What about starting value?

What is the middle point? (xL+1,yL+1/2)

(xL,yL)

81/110

Midpoint Algorithm

What about starting value?

What is the middle point?

eydxcyxf LLLL )()1(),1( 21

21

(xL+1,yL+1/2)

(xL,yL)

82/110

Midpoint Algorithm

What about starting value?

eydxcyxf LLLL )()1(),1( 21

21

dcyxfyxf LLLL 21

21 ),(),1(

83/110

Midpoint Algorithm

What about starting value?

eydxcyxf LLLL )()1(),1( 21

21

dcyxfyxf LLLL 21

21 ),(),1(

is on the line!),( LL yx

84/110

Midpoint Algorithm

What about starting value?

eydxcyxf LLLL )()1(),1( 21

21

dcyxf LL 21

21 ),1(

85/110

Midpoint Algorithm

What about starting value?

eydxcyxf LLLL )()1(),1( 21

21

dcyxf LL 2),1( 21

Multiplying coefficients by 2 doesn’t change sign of f

86/110

Midpoint Algorithm

Need value of to determine E or NE

Build incremental algorithm Assume we have value of

Find value of if E chosenFind value of if NE chosen

),1( 21 yxf

),1( 21 yxf

),2( 21 yxf

),2( 23 yxf

87/110

Midpoint Algorithm

Need value of to determine E or NE

Build incremental algorithm Assume we have value of

Find value of if E chosen

),1( 21 yxf

),1( 21 yxf

),2( 21 yxf

88/110

Midpoint Algorithm

Need value of to determine E or NE

Build incremental algorithm Assume we have value of

Find value of if E chosenFind value of if NE chosen

),1( 21 yxf

),1( 21 yxf

),2( 21 yxf

),2( 23 yxf

89/110

Midpoint Algorithm

If E was chosen, find ),2( 21 yxf

eydxcyxf )()2(),2( 21

21

90/110

Midpoint Algorithm

If E was chosen, find ),2( 21 yxf

eydxcyxf )()2(),2( 21

21

),1(),2( 21

21 yxfcyxf

91/110

Midpoint Algorithm

If NE was chosen, find ),2( 23 yxf

eydxcyxf )()2(),2( 23

23

92/110

Midpoint Algorithm

If NE was chosen, find ),2( 23 yxf

eydxcyxf )()2(),2( 23

23

),1(),2( 21

23 yxfdcyxf

93/110

Midpoint Algorithm – Summary

x=xL y=yL

d=xH - xL c=yL - yH

sum=2c+d draw(x,y)

while ( x < xH)if ( sum < 0 )

sum += 2dy++

x++sum += 2cdraw(x,y)

94/110

Midpoint Algorithm – Summary

x=xL y=yL

d=xH - xL c=yL - yH

sum=2c+d //initial valuedraw(x,y)

while ( x < xH) if ( sum < 0 )

sum += 2dy++

x++sum += 2cdraw(x,y)

95/110

Midpoint Algorithm – Summary

x=xL y=yL

d=xH - xL c=yL - yH

sum=2c+d //initial valuedraw(x,y)

while ( x < xH) // iterate until reaching the end pointif ( sum < 0 )

sum += 2dy++

x++sum += 2cdraw(x,y)

96/110

Midpoint Algorithm – Summary

x=xL y=yL

d=xH - xL c=yL - yH

sum=2c+d //initial valuedraw(x,y)

while ( x < xH) // iterate until reaching the end pointif ( sum < 0 ) // below the line and choose NE

sum += 2dy++

x++sum += 2cdraw(x,y)

97/110

Midpoint Algorithm – Summary

x=xL y=yL

d=xH - xL c=yL - yH

sum=2c+d //initial valuedraw(x,y)

while ( x < xH) // iterate until reaching the end pointif ( sum < 0 ) // below the line and choose NE

sum += 2dy++

x++sum += 2cdraw(x,y)

- Update the current pixel

- Update the function value of midpoint

98/110

Midpoint Algorithm – Summary

x=xL y=yL

d=xH - xL c=yL - yH

sum=2c+d //initial valuedraw(x,y)

while ( x < xH) // iterate until reaching the end pointif ( sum < 0 ) // below the line and choose NE

sum += 2dy++

x++sum += 2cdraw(x,y)

- Draw the pixel based on the function value

of midpoint

99/110

x=xL y=yL

d=xH - xL c=yL - yH

sum=2c+ddraw(x,y)

while ( x < xH)if ( sum < 0 )

sum += 2dy++

x++sum += 2cdraw(x,y)

Midpoint Algorithm – Example

)2,0(

)4,6(

0x 2y 2sum

d =6 c = -2

100/110

Midpoint Algorithm – Example

)2,0(

)4,6(

0x 2y 2sumx=xL y=yL

d=xH - xL c=yL - yH

sum=2c+ddraw(x,y)

while ( x < xH)if ( sum < 0 )

sum += 2dy++

x++sum += 2cdraw(x,y)

d =6 c = -2

101/110

Midpoint Algorithm – Example

)2,0(

)4,6(

1x 2y 2sumx=xL y=yL

d=xH - xL c=yL - yH

sum=2c+ddraw(x,y)

while ( x < xH)if ( sum < 0 )

sum += 2dy++

x++sum += 2cdraw(x,y)

d =6 c = -2

102/110

x=xL y=yL

d=xH - xL c=yL - yH

sum=2c+ddraw(x,y)

while ( x < xH)if ( sum < 0 )

sum += 2dy++

x++sum += 2cdraw(x,y)

Midpoint Algorithm – Example

)2,0(

)4,6(

2x 3y 6sum

d =6 c = -2

103/110

Midpoint Algorithm – Example

)2,0(

)4,6(

3x 3y 2sumx=xL y=yL

d=xH - xL c=yL - yH

sum=2c+ddraw(x,y)

while ( x < xH)if ( sum < 0 )

sum += 2dy++

x++sum += 2cdraw(x,y)

d =6 c = -2

104/110

Midpoint Algorithm – Example

)2,0(

)4,6(

4x 3y 2sumx=xL y=yL

d=xH - xL c=yL - yH

sum=2c+ddraw(x,y)

while ( x < xH)if ( sum < 0 )

sum += 2dy++

x++sum += 2cdraw(x,y)

d =6 c = -2

105/110

x=xL y=yL

d=xH - xL c=yL - yH

sum=2c+ddraw(x,y)

while ( x < xH)if ( sum < 0 )

sum += 2dy++

x++sum += 2cdraw(x,y)

Midpoint Algorithm – Example

)2,0(

)4,6(

5x 4y 6sum

d =6 c = -2

106/110

x=xL y=yL

d=xH - xL c=yL - yH

sum=2c+ddraw(x,y)

while ( x < xH)if ( sum < 0 )

sum += 2dy++

x++sum += 2cdraw(x,y)

Midpoint Algorithm – Example

)2,0(

)4,6(

6x 4y 2sum

d =6 c = -2

107/110

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)

108/110

Midpoint Algorithm: Circle

0)()( 220

20 ryyxx

109/110

Midpoint Algorithm: Circle

0)()( 220

20 ryyxx

110/110

Midpoint Algorithm: Circle

0)()( 220

20 ryyxx

111/110

Midpoint Algorithm: Circle

0)()( 220

20 ryyxx

Two issues:- What are two possible solutions?

- What is the evaluation function?

112/110

Midpoint Algorithm: Circle

113/110

Midpoint Algorithm: Circle

(xk+1,yk)(xk+1,yk-1)

Two issues:- What are two possible solutions?

- What is the evaluation function?

114/110

Midpoint Algorithm: Circle

(xk+1,yk) Midpoint: (xk+1,yk-1/2)(xk+1,yk-1)

Two issues:- What are two possible solutions?

- What is the evaluation function?

115/110

Midpoint Algorithm: Circle

(xk+1,yk) Midpoint: (xk+1,yk-1/2)(xk+1,yk-1)

Two issues:- What are two possible solutions?

- What is the evaluation function?

220

20 )()(),( ryyxxyxf

116/110

Circle, Ellipse, etc

Need implicit forms

- circle:

- ellipse:

Equations for incremental calculations Initial positions More details in section 6.4-6.6 of the

textbook

022 FEyDxCxyByAx

0)()( 220

20 ryyxx

117/110

Next Lecture

How to draw a polygon?

- section 6.10- 6.13

top related