Transcript
Page 1: Rasterization: Bresenham’s Midpoint Algorithmcs4600/lectures/Wk2_Rasterization.pdf · Fall 2015 CS4600 1 Rasterization: Bresenham’s Midpoint Algorithm CS4600 Computer Graphics

Fall 2015

CS4600 1

Rasterization: Bresenham’s Midpoint

Algorithm

CS4600 Computer Graphics

adapted from

Rich Riesenfeld’s slidesFall 2015

Rasterization

General method:– Use equation for geometry

– Insert equation for primitive

– Derive rasterization method

Page 2: Rasterization: Bresenham’s Midpoint Algorithmcs4600/lectures/Wk2_Rasterization.pdf · Fall 2015 CS4600 1 Rasterization: Bresenham’s Midpoint Algorithm CS4600 Computer Graphics

Fall 2015

CS4600 2

Line Characterizations

• Explicit:

• Implicit:

• Constant slope:

• Constant derivative:

Bmxy

0),( cbyaxyxF

kx

y

kxf )(

Line Characterizations - 2

• Parametric:

where,

• Intersection of 2 planes

• Shortest path between 2 points

• Convex hull of 2 discrete points

PtPttP 10)()( 1

PPPP 10 )(;)0( 1

Page 3: Rasterization: Bresenham’s Midpoint Algorithmcs4600/lectures/Wk2_Rasterization.pdf · Fall 2015 CS4600 1 Rasterization: Bresenham’s Midpoint Algorithm CS4600 Computer Graphics

Fall 2015

CS4600 3

Two Line Equations

• Explicit:

• Implicit:

Define:

Hence,

Bmxy 0),( cbyaxyxF

01

01

xxdx

yydy

Bxdx

dyy

From previous

We have,

Hence,

Bxdx

dyy

0 Byxdx

dy

Page 4: Rasterization: Bresenham’s Midpoint Algorithmcs4600/lectures/Wk2_Rasterization.pdf · Fall 2015 CS4600 1 Rasterization: Bresenham’s Midpoint Algorithm CS4600 Computer Graphics

Fall 2015

CS4600 4

Relating Explicit to Implicit Eq’s

Recall, Multiply through by dx,

where,

0 Byxdx

dy

0)()()( Bdxydxxdy

0)()()(),( BdxydxxdyyxF

)();();( dxBcdxbdya

Discrete Lines

• Lines vs. Line Segments

• What is a discrete line segment?

– This is a computer graphics problem

– How to generate a discrete line?

Page 5: Rasterization: Bresenham’s Midpoint Algorithmcs4600/lectures/Wk2_Rasterization.pdf · Fall 2015 CS4600 1 Rasterization: Bresenham’s Midpoint Algorithm CS4600 Computer Graphics

Fall 2015

CS4600 5

“Good” Discrete Line

• No gaps in adjacent pixels

• Pixels close to ideal line

• Consistent choices; same pixels in same situations

• Smooth looking

• Even brightness in all orientations

• Same line for as for

• Double pixels stacked up?

PP 10 PP 01

How to Draw a Line?

Plug into the equation directly

1. Compute slope

2. Start at on point (xo, yo)

3. Increment X and draw• How to figure this out?

Page 6: Rasterization: Bresenham’s Midpoint Algorithmcs4600/lectures/Wk2_Rasterization.pdf · Fall 2015 CS4600 1 Rasterization: Bresenham’s Midpoint Algorithm CS4600 Computer Graphics

Fall 2015

CS4600 6

Derive from Line Equation

Y = mX + b

Yi = mXi + b

Xi+1 = Xi + Yi+1 = mXi+1 + b

Yi+1 = (y / x)(Xi+1) + b

Page 7: Rasterization: Bresenham’s Midpoint Algorithmcs4600/lectures/Wk2_Rasterization.pdf · Fall 2015 CS4600 1 Rasterization: Bresenham’s Midpoint Algorithm CS4600 Computer Graphics

Fall 2015

CS4600 7

What about slope?

Page 8: Rasterization: Bresenham’s Midpoint Algorithmcs4600/lectures/Wk2_Rasterization.pdf · Fall 2015 CS4600 1 Rasterization: Bresenham’s Midpoint Algorithm CS4600 Computer Graphics

Fall 2015

CS4600 8

Derive from Line Equation

Y = mX + b

Yi = mXi + b

Yi+1 = mXi+1 + b

mXi+1 = Yi+1 - b

Xi+1 = (Yi+1 - b) / m

Xi+1 = (Yi+1 - b) / (y/x)

Xi+1 = (Yi+1 - b) * (x/y)

Page 9: Rasterization: Bresenham’s Midpoint Algorithmcs4600/lectures/Wk2_Rasterization.pdf · Fall 2015 CS4600 1 Rasterization: Bresenham’s Midpoint Algorithm CS4600 Computer Graphics

Fall 2015

CS4600 9

What about slope?

If |y1 – y0| > |x1 – x0|

increment Y

else

increment X

• Line segment in first octant with

0 < m < 1

• After we derive this, we’ll look at the

other cases (other octants)

Restricted Form

Page 10: Rasterization: Bresenham’s Midpoint Algorithmcs4600/lectures/Wk2_Rasterization.pdf · Fall 2015 CS4600 1 Rasterization: Bresenham’s Midpoint Algorithm CS4600 Computer Graphics

Fall 2015

CS4600 10

Incremental Function Eval

• Recall

• Characteristics

– Fast

– Cumulative Error

• Need to define

)()()1( ixixfixf

)( oxf

Investigate Sign of FVerify that

Look at extreme values of y

0

),( yxF

above line

below line

on line

Page 11: Rasterization: Bresenham’s Midpoint Algorithmcs4600/lectures/Wk2_Rasterization.pdf · Fall 2015 CS4600 1 Rasterization: Bresenham’s Midpoint Algorithm CS4600 Computer Graphics

Fall 2015

CS4600 11

The Picture

x

y

0 ),( yxF

above line

below line

0 ),( yxF

Key to Bresenham Algorithm

“Reasonable assumptions” have reduced the problem to making a binary choice at each pixel:

(Previous)

NE (next)

E (next)

Page 12: Rasterization: Bresenham’s Midpoint Algorithmcs4600/lectures/Wk2_Rasterization.pdf · Fall 2015 CS4600 1 Rasterization: Bresenham’s Midpoint Algorithm CS4600 Computer Graphics

Fall 2015

CS4600 12

Decision Variable d (logical)

Define a logical decision variable d– linear in form

– incrementally updated (with addition)

– tells us whether to go E or NE

The Picture

midpoint

E

NE

previous

Q

1 pxx

pxx

pyy

1 pyy

M

Page 13: Rasterization: Bresenham’s Midpoint Algorithmcs4600/lectures/Wk2_Rasterization.pdf · Fall 2015 CS4600 1 Rasterization: Bresenham’s Midpoint Algorithm CS4600 Computer Graphics

Fall 2015

CS4600 13

The Picture (again)

E

NE

previous

Q

),( pp yx

),(2

11 pp yx

)1,1( pp yx

),( 1 pp yx

midpointM

Observe the relationships

• Suppose Q is above M, as before.

• Then , M is below the line

• So, means line is above M,

• Need to move NE, increase y value

0)( MF

0)( MF

Page 14: Rasterization: Bresenham’s Midpoint Algorithmcs4600/lectures/Wk2_Rasterization.pdf · Fall 2015 CS4600 1 Rasterization: Bresenham’s Midpoint Algorithm CS4600 Computer Graphics

Fall 2015

CS4600 14

The Picture (again)

midpoint

E

NE

previous

Q

),( pp yx

),(2

11 pp yx

)1,1( pp yx

),( 1 pp yx

Observe the relationships

• Suppose Q is below M, as before.

• Then F(M) < 0 , implies M is above the

line

• So, F(M) < 0 , means line is below M,

• Need to move to E; don’t increase y

Page 15: Rasterization: Bresenham’s Midpoint Algorithmcs4600/lectures/Wk2_Rasterization.pdf · Fall 2015 CS4600 1 Rasterization: Bresenham’s Midpoint Algorithm CS4600 Computer Graphics

Fall 2015

CS4600 15

M = Midpoint = : ),(211 pp yx

• Want to evaluate at M

• Will use an incremental

decision variable d

• Let,

),(211 pp yxFd

cybxad pp )((21)1

How will d be used?

Let,

Therefore,

cybxad pp )((2

1)1

)(arbitrary 0

line) ideal above(midpoint 0

line) ideal below(midpoint 0

E

E

NE

d

Page 16: Rasterization: Bresenham’s Midpoint Algorithmcs4600/lectures/Wk2_Rasterization.pdf · Fall 2015 CS4600 1 Rasterization: Bresenham’s Midpoint Algorithm CS4600 Computer Graphics

Fall 2015

CS4600 16

Case E: Suppose E is chosen

• Recall

• ...

cybxad ppold )((2

1)1

cybxa

yxFd

pp

ppnew

)((

(

2

12

1

)2

),2

1: ; , yE x x y

...

add

cybxa

cybxadd

oldnew

pp

ppoldnew

)(1(

)(2(

2

1

2

1

)

)

Case E: Suppose E is chosen

Page 17: Rasterization: Bresenham’s Midpoint Algorithmcs4600/lectures/Wk2_Rasterization.pdf · Fall 2015 CS4600 1 Rasterization: Bresenham’s Midpoint Algorithm CS4600 Computer Graphics

Fall 2015

CS4600 17

...

add

cybxa

cybxadd

oldnew

pp

ppoldnew

)(1(

)(2(

2

1

2

1

)

)

Case E: Suppose E is chosen

...

add

cybxa

cybxadd

oldnew

pp

ppoldnew

)(1(

)(2(

2

1

2

1

)

)

Case E: Suppose E is chosen

Page 18: Rasterization: Bresenham’s Midpoint Algorithmcs4600/lectures/Wk2_Rasterization.pdf · Fall 2015 CS4600 1 Rasterization: Bresenham’s Midpoint Algorithm CS4600 Computer Graphics

Fall 2015

CS4600 18

...

add

cybxa

cybxadd

oldnew

pp

ppoldnew

)(1(

)(2(

2

1

2

1

)

)

Case E: Suppose E is chosen

Review of Explicit to Implicit

Recall,

Or,

where,

0 Byxdx

dy

0)()()( Bdxydxxdy

0)()()(),( BdxydxxdyyxF

)();();( dxBcdxbdya

Page 19: Rasterization: Bresenham’s Midpoint Algorithmcs4600/lectures/Wk2_Rasterization.pdf · Fall 2015 CS4600 1 Rasterization: Bresenham’s Midpoint Algorithm CS4600 Computer Graphics

Fall 2015

CS4600 19

Case E: .

new oldd d a

increment we add if is chosen.

So, . But remember that

(from line equations).

Hence, ( ) is not evaluated explicitly.

We simply add to update for

E

E

E

E

a

a dy

F M

a d E

Case NE: Suppose NE chosen

Recall

...

1

21)( ( )old p pd a x b y c

3

23

2

2, )

2)

(

( ( )

new p p

p p

d F x y

a x b y c

1 1and, : ; , yNE x x y

Page 20: Rasterization: Bresenham’s Midpoint Algorithmcs4600/lectures/Wk2_Rasterization.pdf · Fall 2015 CS4600 1 Rasterization: Bresenham’s Midpoint Algorithm CS4600 Computer Graphics

Fall 2015

CS4600 20

Case NE: Suppose NE

...

badd

cybxa

cybxa

dd

oldnew

pp

pp

oldnew

)(1(

)(2(

2

1

2

3

)

)

Case NE: .badd oldnew

increment that we add if is chosen.

So, . But remember that

, and (from line equations).

Hence, ( ) is not evaluated explicitly.

We simply add to update for

NE

NE

NE

NE

a b

a dy b dx

F M

a b d NE

Page 21: Rasterization: Bresenham’s Midpoint Algorithmcs4600/lectures/Wk2_Rasterization.pdf · Fall 2015 CS4600 1 Rasterization: Bresenham’s Midpoint Algorithm CS4600 Computer Graphics

Fall 2015

CS4600 21

.for update to

i.e., , addsimply wemeans,

and , where,

NEddxdy

ba

dxbdyaba

NE

NE

NE

Case NE: .

badd oldnew

Summary• At each step of the procedure, we

must choose between moving E or NE based on the sign of the

decision variable d• Then update according to

dxdyd

dydd

NENE

EE

where,

or , where,

Page 22: Rasterization: Bresenham’s Midpoint Algorithmcs4600/lectures/Wk2_Rasterization.pdf · Fall 2015 CS4600 1 Rasterization: Bresenham’s Midpoint Algorithm CS4600 Computer Graphics

Fall 2015

CS4600 22

What is initial value of d ?

• First point is

• First midpoint is

• What is initial midpoint value?

),( 00 yx

),(2

11 00 yx

),(),(2

11

2

11 0000 yxFyxd

0 0 0 0

0 0

0 0

1 11 1)

2 2

2

(2

( , ) ( ( )

, )

b

bF

F x y a x b y c

ax by c a

x y a

What is initial value of d ?

Page 23: Rasterization: Bresenham’s Midpoint Algorithmcs4600/lectures/Wk2_Rasterization.pdf · Fall 2015 CS4600 1 Rasterization: Bresenham’s Midpoint Algorithm CS4600 Computer Graphics

Fall 2015

CS4600 23

What is initial value of d ?

0 0 0 0Note, ( (, ) 0, since , ) is on line.F x y x y

2

22

11

)(

0),( 00

dx

b

dy

ayxFHence,

What Does Factor of 2 x Do ?

• Has the same 0-set

• Changes the slope of the plane

• Rotates plane about the 0-set line

• Gets rid of the denominator

0)(2),(2 cbyaxyxF

Page 24: Rasterization: Bresenham’s Midpoint Algorithmcs4600/lectures/Wk2_Rasterization.pdf · Fall 2015 CS4600 1 Rasterization: Bresenham’s Midpoint Algorithm CS4600 Computer Graphics

Fall 2015

CS4600 24

0 0

11

2

Note, we can clear denominator

and not change line,

2 ( , ) 2( )F x y dy dx

What is initial value of d ?

2 ( , ) 2( ) 0

So, first value of

2( ) ( )

F x y ax by c

d dy dx

What is initial value of d ?

Page 25: Rasterization: Bresenham’s Midpoint Algorithmcs4600/lectures/Wk2_Rasterization.pdf · Fall 2015 CS4600 1 Rasterization: Bresenham’s Midpoint Algorithm CS4600 Computer Graphics

Fall 2015

CS4600 25

More Summary

• Initial value

• Case E:

• Case NE:

• Note, all deltas are constants

)()(2 dxdy

)(2 where, dydd EE

)}(){(2 where

,

dxdy

dd

NE

NE

More Summary

Choose

otherwise

0 if

NE

dE

Page 26: Rasterization: Bresenham’s Midpoint Algorithmcs4600/lectures/Wk2_Rasterization.pdf · Fall 2015 CS4600 1 Rasterization: Bresenham’s Midpoint Algorithm CS4600 Computer Graphics

Fall 2015

CS4600 26

Example

• Line end points:

• Deltas: dx = 4; dy = 3

)11,9(),;)8,5(), 1100 (( yxyx

Graph

13

12

11

10

9

8

7

64 5 6 7 8 9 10 11

Page 27: Rasterization: Bresenham’s Midpoint Algorithmcs4600/lectures/Wk2_Rasterization.pdf · Fall 2015 CS4600 1 Rasterization: Bresenham’s Midpoint Algorithm CS4600 Computer Graphics

Fall 2015

CS4600 27

Example (dx = 4; dy = 3)

• Initial value of

(5 8) 2( ) ( )

6 4 2 0

2

d , dy dx

d NE

otherwise

0 if

NE

dE

)()(2 dxdy d =

Graph

13

12

11

10

9

8

7

64 5 6 7 8 9 10 11

Page 28: Rasterization: Bresenham’s Midpoint Algorithmcs4600/lectures/Wk2_Rasterization.pdf · Fall 2015 CS4600 1 Rasterization: Bresenham’s Midpoint Algorithm CS4600 Computer Graphics

Fall 2015

CS4600 28

Example (dx=4; dy=3 )

• Update value of d• Last move was NE, so

2( )

2(3 4) 2

2 2 0

N E dy - dx

d

E

otherwise

0 if

NE

dE

)(2 where, dydd EE

)}(){(2 where

,

dxdy

dd

NE

NE

Graph

13

12

11

10

9

8

7

64 5 6 7 8 9 10 11

Page 29: Rasterization: Bresenham’s Midpoint Algorithmcs4600/lectures/Wk2_Rasterization.pdf · Fall 2015 CS4600 1 Rasterization: Bresenham’s Midpoint Algorithm CS4600 Computer Graphics

Fall 2015

CS4600 29

Example (dx=4; dy=3 )

• Previous move was E

2( )

2(3) 6

0 6 0

E dy

d NE

otherwise

0 if

NE

dE

)(2 where, dydd EE

)}(){(2 where

,

dxdy

dd

NE

NE

2

Graph

13

12

11

10

9

8

7

64 5 6 7 8 9 10 11

Page 30: Rasterization: Bresenham’s Midpoint Algorithmcs4600/lectures/Wk2_Rasterization.pdf · Fall 2015 CS4600 1 Rasterization: Bresenham’s Midpoint Algorithm CS4600 Computer Graphics

Fall 2015

CS4600 30

Example (dx=4; dy=3 )

• Previous move was NE

2( )

2(3 4) 2

6 2 4

N E dy - dx

d

N E

otherwise

0 if

NE

dE

)(2 where, dydd EE

)}(){(2 where

,

dxdy

dd

NE

NE

8 6

Graph

13

12

11

10

9

8

7

64 5 6 7 8 9 10 11

Page 31: Rasterization: Bresenham’s Midpoint Algorithmcs4600/lectures/Wk2_Rasterization.pdf · Fall 2015 CS4600 1 Rasterization: Bresenham’s Midpoint Algorithm CS4600 Computer Graphics

Fall 2015

CS4600 31

Graph

13

12

11

10

9

8

7

64 5 6 7 8 9 10 11

Meeting Bresenham Criteria

0 1 flip about -axism x

1 flip about m x y

0; 1 trivial casesm m Case 0:

Case 1:

Case 2:

Page 32: Rasterization: Bresenham’s Midpoint Algorithmcs4600/lectures/Wk2_Rasterization.pdf · Fall 2015 CS4600 1 Rasterization: Bresenham’s Midpoint Algorithm CS4600 Computer Graphics

Fall 2015

CS4600 32

Case 0: Trivial Situations

• Do not need Bresenham

line horizontal0 m

xym line1

Case 2: Flip about x-axis

), 00( yx

x

y

), 11( yx), 11( yx

), 00( yx

Page 33: Rasterization: Bresenham’s Midpoint Algorithmcs4600/lectures/Wk2_Rasterization.pdf · Fall 2015 CS4600 1 Rasterization: Bresenham’s Midpoint Algorithm CS4600 Computer Graphics

Fall 2015

CS4600 33

Case 2: Flip about x-axis

Flip about -axis ( ) :x y y

0 0 0 0

1 11 1

, ) ( ,

, ) , )

( );

( (

x y x y

x y x y

1Suppose, 0 ,m

How do slopes relate?

0

1 0

0

1 0

1

1

;

by definition

y y

y y

mx x

mx x

)0

1 0

(1i iSince ,

y ymyy

x x

Page 34: Rasterization: Bresenham’s Midpoint Algorithmcs4600/lectures/Wk2_Rasterization.pdf · Fall 2015 CS4600 1 Rasterization: Bresenham’s Midpoint Algorithm CS4600 Computer Graphics

Fall 2015

CS4600 34

How do slopes relate?

)0

1 0

1( y ym

x x

m m

1010 mm

i.e.,

Case 3: Flip about line y=x), 11( yx

), 00( yx

y

x

xy ), 11( yx

), 00( yx

Page 35: Rasterization: Bresenham’s Midpoint Algorithmcs4600/lectures/Wk2_Rasterization.pdf · Fall 2015 CS4600 1 Rasterization: Bresenham’s Midpoint Algorithm CS4600 Computer Graphics

Fall 2015

CS4600 35

Case 3: Flip about line y=x

,

swap and prime them ,

,

y mx B

x y

x my B

my x B

Case 3: m′=?

1,

1 and,

1 0 1

y x Bm

mm

m m

Page 36: Rasterization: Bresenham’s Midpoint Algorithmcs4600/lectures/Wk2_Rasterization.pdf · Fall 2015 CS4600 1 Rasterization: Bresenham’s Midpoint Algorithm CS4600 Computer Graphics

Fall 2015

CS4600 36

More Raster Line Issues

• Fat lines with multiple pixel width

• Symmetric lines

• How should end pt geometry look?

• Generating curves, e.g., circles, etc.

• Jaggies, staircase effect, aliasing...

Pixel Space

12

11

10

9

8

7

6

4 5 6 7 8 9 10

Page 37: Rasterization: Bresenham’s Midpoint Algorithmcs4600/lectures/Wk2_Rasterization.pdf · Fall 2015 CS4600 1 Rasterization: Bresenham’s Midpoint Algorithm CS4600 Computer Graphics

Fall 2015

CS4600 37

Example

Example

Page 38: Rasterization: Bresenham’s Midpoint Algorithmcs4600/lectures/Wk2_Rasterization.pdf · Fall 2015 CS4600 1 Rasterization: Bresenham’s Midpoint Algorithm CS4600 Computer Graphics

Fall 2015

CS4600 38

Bresenham Circles

The End Bresenham’s Algorithm


Recommended