dr. scott schaefer scan conversion of lines. 2/78 displays – cathode ray tube

78
Dr. Scott Schaefer Scan Conversion of Lines

Upload: brent-summers

Post on 27-Dec-2015

229 views

Category:

Documents


1 download

TRANSCRIPT

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

P

Q

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

P

Q

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

P

Q

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

bmxy m: slope

b: y-intercept

17/78

Slope-intercept equation for a line:

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

Arbitrary Lines

bmxy m: slope

b: y-intercept

)( , HH yx

)( , LL yx

18/78

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

b: y-intercept

)( , HH yx

)( , LL yx

19/78

Arbitrary Lines

Assume Other lines by swapping x, y or negating

Take steps in x and determine where to fill y

bmxy

10 m

20/78

Simple Algorithm

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

21/78

Simple Algorithm

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

22/78

Simple Algorithm

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

23/78

Simple Algorithm

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

24/78

Simple Algorithm

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

25/78

Simple Algorithm

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

26/78

Simple Algorithm - Example

)2,0(

)4,6(

bmxy

27/78

Simple Algorithm - Example

)2,0(

)4,6(

bmxy

31

0624

LH

LH

xxyym

28/78

Simple Algorithm - Example

)2,0(

)4,6(

bmxy

2

0

y

x

31

0624

LH

LH

xxyym

29/78

Simple Algorithm - Example

)2,0(

)4,6(

bmxy

37

1

y

x

31

0624

LH

LH

xxyym

30/78

Simple Algorithm - Example

)2,0(

)4,6(

bmxy

38

2

y

x

31

0624

LH

LH

xxyym

31/78

Simple Algorithm - Example

)2,0(

)4,6(

bmxy

3

3

y

x

31

0624

LH

LH

xxyym

32/78

Simple Algorithm - Example

)2,0(

)4,6(

bmxy

310

4

y

x

31

0624

LH

LH

xxyym

33/78

Simple Algorithm - Example

)2,0(

)4,6(

bmxy

311

5

y

x

31

0624

LH

LH

xxyym

34/78

Simple Algorithm - Example

)2,0(

)4,6(

bmxy

4

6

y

x

31

0624

LH

LH

xxyym

35/78

Simple Algorithm - 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

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

),1( 21 yx

38/78

Midpoint Algorithm

)2,0(

)4,6(

39/78

Midpoint Algorithm – Implicit Forms

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

bmxy

40/78

Midpoint Algorithm – Implicit Forms

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

bxyLH

LH

xxyy

41/78

Midpoint Algorithm – Implicit Forms

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

))(()( LHxxyy

LH xxbxyxxLH

LH

42/78

Midpoint Algorithm – Implicit Forms

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

bxxxyyyxx LHLHLH )()()(

43/78

Midpoint Algorithm – Implicit Forms

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

0)()()( bxxxyyyxx HLHLLH

44/78

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

45/78

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

46/78

Midpoint Algorithm – Implicit Forms

x

y

)2,0(

)5,6(

263 xy

47/78

Midpoint Algorithm – Implicit Forms

x

y

)2,0(

263 xy

1236),( xyyxf)5,6(

48/78

Midpoint Algorithm – Implicit Forms

x

y

)2,0(

263 xy

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

49/78

Midpoint Algorithm – Implicit Forms

x

y

)2,0(

263 xy

1236),( xyyxf

18)1,4( f

)5,6(

50/78

Midpoint Algorithm – Implicit Forms

x

y

)2,0(

263 xy

1236),( xyyxf

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

51/78

Midpoint Algorithm – Implicit Forms

x

y

)2,0(

263 xy

1236),( xyyxf)5,6(

52/78

Need value of to decide E or NE Build incremental algorithm Assume we have value of

Find value of if E chosenFind value of if NE chosen

Midpoint Algorithm

),1( 21 yxf

),1( 21 yxf

),2( 21 yxf

),2( 23 yxf

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 chosenFind value of if NE chosen

),1( 21 yxf

),1( 21 yxf

),2( 21 yxf

),2( 23 yxf

),1( 21 yxf

),2( 21 yxf

),2( 23 yxf

54/78

Midpoint Algorithm

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

eydxcyxf )()2(),2( 21

21

55/78

Midpoint Algorithm

If E was chosen, find

),1(),2( 21

21 yxfcyxf

),2( 21 yxf

eydxcyxf )()2(),2( 21

21

56/78

Midpoint Algorithm

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

eydxcyxf )()2(),2( 23

23

57/78

Midpoint Algorithm

If NE was chosen, find

),1(),2( 21

23 yxfdcyxf

),2( 23 yxf

eydxcyxf )()2(),2( 23

23

58/78

Midpoint Algorithm

What about starting value?

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

21

59/78

Midpoint Algorithm

What about starting value?

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

21

dcyxfyxf LLLL 21

21 ),(),1(

60/78

Midpoint Algorithm

What about starting value?

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

21

dcyxfyxf LLLL 21

21 ),(),1(

is on the line!),( LL yx

61/78

Midpoint Algorithm

What about starting value?

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

21

dcyxf LL 21

21 ),1(

62/78

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

63/78

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)

64/78

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)

65/78

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)

66/78

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)

67/78

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)

68/78

Midpoint Algorithm – Example

)2,0(

)4,6(

0x 2y 2sumx=xL y=yL

d=6 c=-2sum=2c+ddraw(x,y)

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

sum += 2dy++

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

69/78

Midpoint Algorithm – Example

)2,0(

)4,6(

0x 2y 2sumx=xL y=yL

d=6 c=-2sum=2c+ddraw(x,y)

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

sum += 2dy++

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

70/78

Midpoint Algorithm – Example

)2,0(

)4,6(

1x 2y 2sumx=xL y=yL

d=6 c=-2sum=2c+ddraw(x,y)

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

sum += 2dy++

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

71/78

Midpoint Algorithm – Example

)2,0(

)4,6(

2x 3y 6sumx=xL y=yL

d=6 c=-2sum=2c+ddraw(x,y)

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

sum += 2dy++

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

72/78

Midpoint Algorithm – Example

)2,0(

)4,6(

3x 3y 2sumx=xL y=yL

d=6 c=-2sum=2c+ddraw(x,y)

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

sum += 2dy++

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

73/78

Midpoint Algorithm – Example

)2,0(

)4,6(

4x 3y 2sumx=xL y=yL

d=6 c=-2sum=2c+ddraw(x,y)

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

sum += 2dy++

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

74/78

Midpoint Algorithm – Example

)2,0(

)4,6(

5x 4y 6sumx=xL y=yL

d=6 c=-2sum=2c+ddraw(x,y)

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

sum += 2dy++

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

75/78

Midpoint Algorithm – Example

)2,0(

)4,6(

6x 4y 2sumx=xL y=yL

d=6 c=-2sum=2c+ddraw(x,y)

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

sum += 2dy++

x++sum += 2cdraw(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 6.4-6.5 of the textbook

022 FEyDxCxyByAx

0)()( 220

20 ryyxx

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

P2 P3

P4

P5P6