Bresenham’s Midpoint Algorithm CS5600 Computer Graphics Rich Riesenfeld Spring 2006 Lecture Set 1
Dec 30, 2015
Bresenham’s Midpoint Algorithm
CS5600 Computer Graphics
Rich RiesenfeldSpring 2006
Lect
ure
Set
1
Spring 2006 CS 5600 2
Line Characterizations
• Explicit:
• Implicit:
• Constant slope:
• Constant derivative:
Bmxy
0),( cbyaxyxF
kx
y
kxf )(
Spring 2006 CS 5600 4
Discrete Lines
• Lines vs. Line Segments
• What is a discrete line segment?
–This is a relatively recent problem
–How to generate a discrete line?
Spring 2006 CS 5600 5
“Good” Discrete Line - 1
• No gaps in adjacent pixels
• Pixels close to ideal line
• Consistent choices; same pixels
in same situations
Spring 2006 CS 5600 6
“Good” Discrete Line - 2
• Smooth looking
• Even brightness in all orientations
• Same line for as for
• Double pixels stacked up?
PP 10 PP 01
Spring 2006 CS 5600 19
• Line segment in first octant with
0 < m < 1
• Let us proceed
Restricted Form
Spring 2006 CS 5600 20
Two Line Equations
• Explicit:
• Implicit:
Define:
Hence,
Bmxy 0),( cbyaxyxF
01
01
xxdx
yydy
Bxdx
dyy
Spring 2006 CS 5600 21
From previous
We have,
Hence,
Bxdx
dyy
0 Byxdx
dy
Spring 2006 CS 5600 22
Relating Explicit to Implicit Eq’s
Recall,
Or,
where,
0 Byxdx
dy
0)()()( Bdxydxxdy
0)()()(),( BdxydxxdyyxF
)();();( dxBcdxbdya
Spring 2006 CS 5600 23
Investigate Sign of FVerify that
Look at extreme values of y
0
),( yxF
above line
below line
on line
Spring 2006 CS 5600 24
The Picture
x
y
0 ),( yxF
above line
below line
0 ),( yxF
Spring 2006 CS 5600 25
Key to Bresenham Algorithm
“Reasonable assumptions” have reduced the problem to making a binary choice at each pixel:
(Previous)
NE (next)
E (next)
Spring 2006 CS 5600 26
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
Spring 2006 CS 5600 27
The Picture
ideal linemidpoint
E
NE
previous
Q
1 pxx
pxx
pyy
1 pyy
M
Spring 2006 CS 5600 28
The Picture (again)
ideal line
E
NE
previous
Q
),( pp yx
),(2
11 pp yx
)1,1( pp yx
),( 1 pp yx
midpointM
Spring 2006 CS 5600 29
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
Spring 2006 CS 5600 30
The Picture (again)
ideal line
midpoint
E
NE
previous
Q
),( pp yx
),(2
11 pp yx
)1,1( pp yx
),( 1 pp yx
Spring 2006 CS 5600 31
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
Spring 2006 CS 5600 32
M = Midpoint = :
),(211 pp yx
• Want to evaluate at M
• Will use an incr decision var d
• Let, ),(211 pp yxFd
cybxad pp )((21)1
Spring 2006 CS 5600 33
How will d be used?
Recall,
Therefore,
cybxad pp )((2
1)1
)(arbitrary 0
line) ideal above(midpoint 0
line) ideal below(midpoint 0
E
E
NE
d
Spring 2006 CS 5600 34
Case 1: Suppose E is chosen
• Recall
•
• ...
cybxad ppold )((2
1)1
cybxa
yxFd
pp
ppnew
)((
(
2
12
1
)2
),2
1: ; , yE x x y
Spring 2006 CS 5600 35
...
add
cybxa
cybxadd
oldnew
pp
ppoldnew
)(1(
)(2(
2
1
2
1
)
)
Case 1: Suppose E is chosen
Spring 2006 CS 5600 36
Review of Explicit to Implicit
Recall,
Or,
where,
0 Byxdx
dy
0)()()( Bdxydxxdy
0)()()(),( BdxydxxdyyxF
)();();( dxBcdxbdya
Spring 2006 CS 5600 37
Case 1:
.
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
Spring 2006 CS 5600 38
Case 2: 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
Spring 2006 CS 5600 39
Case 2: Suppose NE
...
badd
cybxa
cybxa
dd
oldnew
pp
pp
oldnew
)(1(
)(2(
2
1
2
3
)
)
Spring 2006 CS 5600 40
Case 2: .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
Spring 2006 CS 5600 41
.for update to
i.e., , addsimply wemeans,
and , where,
NEddxdy
ba
dxbdyaba
NE
NE
NE
Case 2: .badd oldnew
Spring 2006 CS 5600 42
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,
Spring 2006 CS 5600 43
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
Spring 2006 CS 5600 44
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 ?
Spring 2006 CS 5600 45
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,
Spring 2006 CS 5600 46
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,
Spring 2006 CS 5600 47
What Does “2 x ” Do ?
• Has the same 0-set
• Changes the slope of the plane
• Rotates plane about the 0-set line
0)(2),(2 cbyaxyxF
Spring 2006 CS 5600 48
dxdyyxF
dyyxFdx
)(2),(2
gives, 2by
)(),( gMultiplyin
2
11
22
11
00
00
What is initial value of d ?
Spring 2006 CS 5600 49
2 ( , ) 2( ) 0
So, first value of
2( ) ( )
F x y ax by c
d dy dx
What is initial value of d ?
Spring 2006 CS 5600 50
More Summary
• Initial value
• Case 1:
• Case 2:
)()(2 dxdy
)(2 where, dydd EE
)}(){(2 where
,
dxdy
dd
NE
NE
Spring 2006 CS 5600 51
More Summary
Choose
otherwise
0 if
NE
dE
Spring 2006 CS 5600 52
Example
• Line end points:
• Deltas:
)11,9(),;)8,5(), 1100 (( yxyx
3;4 dydx
Spring 2006 CS 5600 53
Graph
13
12
11
10
9
8
7
64 5 6 7 8 9 10 11
Spring 2006 CS 5600 54
Example ( dx = 4; dy = 3 )
• Initial value of 3;4 dydx
NEd
dx-y ,d
2
0246
)()2(2)85(
Spring 2006 CS 5600 55
Graph
13
12
11
10
9
8
7
64 5 6 7 8 9 10 11
Spring 2006 CS 5600 56
Example ( dx = 4; dy = 3 )
• Update value of d•
Ed
dy-dx d
NE
022
2)43(2
)(2)9,6(2
so , wasmoveLast
Spring 2006 CS 5600 57
Example ( dx = 4; dy = 3 ) -2
• Update value of d
• Last move was NE, so
2d(6,9) = 2d(y - dy)
= 2(4 - 3) = - 2
d = 2 – 2 = 0 E
Spring 2006 CS 5600 58
Graph
13
12
11
10
9
8
7
64 5 6 7 8 9 10 11
Spring 2006 CS 5600 59
Example ( dx = 4; dy = 3 )
• Previous move was E
(7,9) 2( )
2(3) 6
0 6 0
d dy
d NE
Spring 2006 CS 5600 60
Graph
13
12
11
10
9
8
7
64 5 6 7 8 9 10 11
Spring 2006 CS 5600 61
Example ( dx = 4; dy = 3 )
• Previous move was , so
2 (8,10) 2( )
2(3 4) 2
6 2 4
NE
d dy - dx
d NE
Spring 2006 CS 5600 62
Graph
13
12
11
10
9
8
7
64 5 6 7 8 9 10 11
Spring 2006 CS 5600 63
Graph
13
12
11
10
9
8
7
64 5 6 7 8 9 10 11
Spring 2006 CS 5600 64
Graph
13
12
11
10
9
8
7
64 5 6 7 8 9 10 11
Spring 2006 CS 5600 65
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...
Spring 2006 CS 5600 66
Pixel Space
12
11
10
9
8
7
6
4 5 6 7 8 9 10
Example
Example
Spring 2006 CS 5600 69
Bresenham Circles
Spring 2006 CS 5600 70
The End Bresenham’s Algorithm
Lect
ure
Set
1