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

117
1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

Upload: primrose-johnston

Post on 19-Jan-2016

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

1

CSCE 441 Lecture 2:Scan Conversion of Lines

Jinxiang Chai

Page 2: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

2/110

Displays – Cathode Ray Tube

Page 3: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

3/110

Displays – Liquid Crystal Displays

Page 4: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

4/110

Displays – Plasma

Page 5: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

5/110

Displays – Pixels

Pixel: the smallest element of picture

- Integer position (i,j)

- Color information (r,g,b)

x

y

(0,0)

Page 6: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

6/110

Digital Scan Conversion

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

Page 7: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 8: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 9: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

9/110

Outline

How to draw a line?

- Digital Differential Analyzer (DDA)

- Midpoint algorithm

Required readings

- HB 6.1 to 6.8

Page 10: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 11: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 12: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 13: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

Line-drawing using “Paint”

Page 14: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

14/110

Special Lines - Horizontal

Page 15: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

15/110

Special Lines - Horizontal

Increment x by 1, keep y constant

Page 16: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

16/110

Special Lines - Vertical

Page 17: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

17/110

Special Lines - Vertical

Keep x constant, increment y by 1

Page 18: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

18/110

Special Lines - Diagonal

Page 19: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

19/110

Special Lines - Diagonal

Increment x by 1, increment y by 1

Page 20: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

20/110

How about Arbitrary Lines?

How to draw an arbitrary line?

P

Q

Page 21: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

21/110

Slope-intercept equation for a line:

Arbitrary Lines

bmxy m: slope

b: y-intercept

Page 22: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 23: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 24: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

24/110

Arbitrary Lines

Assume

bmxy

10 m

10 m

Page 25: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 26: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 27: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 28: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 29: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

29/110

Digital Differential Analyzer

Simple algorithm:

- sample unit intervals in one coordinate

- find the nearest pixel for each sampled point.

Page 30: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 31: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 32: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 33: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 34: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 35: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

35/110

DDA - Example

)2,0(

)4,6(

bmxy

Page 36: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

36/110

DDA - Example

)2,0(

)4,6(

bmxy

31

0624

LH

LH

xxyym

Page 37: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

37/110

DDA - Example

)2,0(

)4,6(

bmxy

2

0

y

x

31

0624

LH

LH

xxyym

Page 38: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

38/110

DDA - Example

)2,0(

)4,6(

bmxy

37

1

y

x

31

0624

LH

LH

xxyym

Page 39: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

39/110

DDA - Example

)2,0(

)4,6(

bmxy

37

1

y

x

31

0624

LH

LH

xxyym

Page 40: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

40/110

DDA - Example

)2,0(

)4,6(

bmxy

38

2

y

x

31

0624

LH

LH

xxyym

Page 41: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

41/110

DDA - Example

)2,0(

)4,6(

bmxy

38

2

y

x

31

0624

LH

LH

xxyym

Page 42: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

42/110

DDA - Example

)2,0(

)4,6(

bmxy

3

3

y

x

31

0624

LH

LH

xxyym

Page 43: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

43/110

DDA - Example

)2,0(

)4,6(

bmxy

3

3

y

x

31

0624

LH

LH

xxyym

Page 44: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

44/110

DDA - Example

)2,0(

)4,6(

bmxy

310

4

y

x

31

0624

LH

LH

xxyym

Page 45: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

45/110

DDA - Example

)2,0(

)4,6(

bmxy

310

4

y

x

31

0624

LH

LH

xxyym

Page 46: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

46/110

DDA - Example

)2,0(

)4,6(

bmxy

311

5

y

x

31

0624

LH

LH

xxyym

Page 47: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

47/110

DDA - Example

)2,0(

)4,6(

bmxy

311

5

y

x

31

0624

LH

LH

xxyym

Page 48: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

48/110

DDA - Example

)2,0(

)4,6(

bmxy

4

6

y

x

31

0624

LH

LH

xxyym

Page 49: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

49/110

DDA - Example

)2,0(

)4,6(

bmxy

4

6

y

x

31

0624

LH

LH

xxyym

Page 50: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 51: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

51/110

Outline

How to draw a line?

- Digital Differential Analyzer (DDA)

- Midpoint algorithm

Page 52: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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)

Page 53: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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)

Page 54: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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)

Page 55: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 56: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 57: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 58: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

58/110

Above/Below the Line?

)2,0(

)4,6(

Page 59: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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?

Page 60: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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(

Page 61: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 62: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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!

Page 63: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 64: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

64/110

Midpoint Algorithm – Implicit Forms

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

bxyLH

LH

xxyy

Page 65: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

65/110

Midpoint Algorithm – Implicit Forms

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

))(()( LHxxyy

LH xxbxyxxLH

LH

Page 66: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

66/110

Midpoint Algorithm – Implicit Forms

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

bxxxyyyxx LHLHLH )()()(

Page 67: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

67/110

Midpoint Algorithm – Implicit Forms

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

0)()()( bxxxyyyxx HLHLLH

Page 68: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 69: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 70: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 71: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

71/110

Midpoint Algorithm – Implicit Forms

x

y

)2,0(

)5,6(

263 xy

Page 72: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

72/110

Midpoint Algorithm – Implicit Forms

x

y

)2,0(

263 xy

1236),( xyyxf)5,6(

Page 73: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

73/110

Midpoint Algorithm – Implicit Forms

x

y

)2,0(

263 xy

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

Page 74: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

74/110

Midpoint Algorithm – Implicit Forms

x

y

)2,0(

263 xy

1236),( xyyxf

18)1,4( f

)5,6(

Page 75: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

75/110

Midpoint Algorithm – Implicit Forms

x

y

)2,0(

263 xy

1236),( xyyxf

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

Page 76: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

76/110

Midpoint Algorithm – Implicit Forms

x

y

)2,0(

263 xy

1236),( xyyxf)5,6(

Page 77: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

77/110

Two Issues

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

How to incrementally evaluate this?

Page 78: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

78/110

Above/Below the Line?

)2,0(

)4,6(

Page 79: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

79/110

Midpoint Algorithm

What about starting value?

What is the middle point?

(xL,yL)

Page 80: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

80/110

Midpoint Algorithm

What about starting value?

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

(xL,yL)

Page 81: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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)

Page 82: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

82/110

Midpoint Algorithm

What about starting value?

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

21

dcyxfyxf LLLL 21

21 ),(),1(

Page 83: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 84: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

84/110

Midpoint Algorithm

What about starting value?

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

21

dcyxf LL 21

21 ),1(

Page 85: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 86: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 87: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 88: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 89: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

89/110

Midpoint Algorithm

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

eydxcyxf )()2(),2( 21

21

Page 90: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

90/110

Midpoint Algorithm

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

eydxcyxf )()2(),2( 21

21

),1(),2( 21

21 yxfcyxf

Page 91: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

91/110

Midpoint Algorithm

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

eydxcyxf )()2(),2( 23

23

Page 92: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

92/110

Midpoint Algorithm

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

eydxcyxf )()2(),2( 23

23

),1(),2( 21

23 yxfdcyxf

Page 93: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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)

Page 94: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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)

Page 95: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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)

Page 96: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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)

Page 97: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 98: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 99: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 100: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 101: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 102: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 103: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 104: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 105: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 106: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 107: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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)

Page 108: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

108/110

Midpoint Algorithm: Circle

0)()( 220

20 ryyxx

Page 109: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

109/110

Midpoint Algorithm: Circle

0)()( 220

20 ryyxx

Page 110: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

110/110

Midpoint Algorithm: Circle

0)()( 220

20 ryyxx

Page 111: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

111/110

Midpoint Algorithm: Circle

0)()( 220

20 ryyxx

Two issues:- What are two possible solutions?

- What is the evaluation function?

Page 112: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

112/110

Midpoint Algorithm: Circle

Page 113: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

113/110

Midpoint Algorithm: Circle

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

Two issues:- What are two possible solutions?

- What is the evaluation function?

Page 114: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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?

Page 115: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 116: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

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

Page 117: 1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai

117/110

Next Lecture

How to draw a polygon?

- section 6.10- 6.13