CSEP505: Programming Languages Lecture 7: Coercions, Type Variables Dan Grossman Spring 2006.
Line Characterizations Bresenham’s B Midpoint … 200 CS5600 1 Bresenham’s Midpoint Algorithm...
Transcript of Line Characterizations Bresenham’s B Midpoint … 200 CS5600 1 Bresenham’s Midpoint Algorithm...
Spring 200
CS5600 1
Bresenham’sMidpoint 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
kxy =
ΔΔ
kxf =′ )(
Spring 2006 CS 5600 3
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 ==
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 200
CS5600 2
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 7
Incremental Fn Eval
• Recall
• Characteristics
–Fast
–Cumulative Error
• Need to define
)()()1( ixixfixf Δ+=+
)( oxf
Spring 2006 CS 5600 8
0 0( , ) (0,0) translatex y• ≠ ⇒
Meeting Bresenham Criteria
0; 1 trivial casesm m• = = ⇒
0 1 flip about -axism x• > > − ⇒
1 flip about m x y• > ⇒ =
Spring 200
CS5600 3
Spring 2006 CS 5600 9
Case 1: Translate to Origin
x
y ), 11( yx
), 00( yx ), 00( yx ′′
), 11( yx ′′
Spring 2006 CS 5600 10
Case 0: Trivial Situations
• Do not need Bresenham
line horizontal0 ⇒=• m
xym =⇒=• line1
Spring 2006 CS 5600 11
Case 1: Translate to Origin
• Need only consider lines emanating from the origin.
0 0, ) ( yx• Move to the origin
),),
0,0(),
010111
00
((
);(
yyy
y
xxx
x
−−=′
=′
′
′
Spring 2006 CS 5600 12
Case 2: Flip about x-axis
), 00( yx
x
y
), 11( yx), 11( yx ′′
), 00( yx ′′
Spring 200
CS5600 4
Spring 2006 CS 5600 13
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• > > −
Spring 2006 CS 5600 14
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−
− −−− ′′ = =
Spring 2006 CS 5600 15
How do slopes relate?
)0
1 0
1( y ym
x xm m
−
−′ = −
′=−
1010 <′<⇒−>>∴ mm
i.e.,
Spring 2006 CS 5600 16
Case 3: Flip about line y=x), 11( yx ′′
), 00( yx ′′
y
x
xy =), 11( yx
), 00( yx
Spring 200
CS5600 5
Spring 2006 CS 5600 17
Case 3: Flip about line y=x
, swap and prime them ,
,
y mx Bx yx my B
my x B
= +↔′ ′= +′ ′= −
Spring 2006 CS 5600 18
Case 3: m′=? 1 ,
1 and,
1 0 1
y x Bm
mm
m m
⎛ ⎞′ ′= −⎜ ⎟⎝ ⎠⎛ ⎞′∴ = ⎜ ⎟⎝ ⎠
′> ⇒ < <
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
xxdxyydy
−=−=
Bxdxdyy +⎟
⎠⎞
⎜⎝⎛=
Spring 200
CS5600 6
Spring 2006 CS 5600 21
From previous
We have,
Hence,
Bxdxdyy +⎟
⎠⎞
⎜⎝⎛=
0=+− Byxdxdy
Spring 2006 CS 5600 22
Relating Explicit to Implicit Eq’s
Recall,
Or,
where,
0=+− Byxdxdy
0)()()( =+−+ Bdxydxxdy
0)()()(),( =+−+=∴ BdxydxxdyyxF
)();();( dxBcdxbdya =−==
Spring 2006 CS 5600 23
Investigate Sign of FVerify that
Look at extreme values of y
⎪⎩
⎪⎨
⎧
−
+=
0
),( yxFabove line
below line
on line
Spring 2006 CS 5600 24
The Picture
x
y
0 ),( >yxF
above line
below line
0 ),( <yxF
Spring 200
CS5600 7
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
),(211 ++ pp yx
)1,1( ++ pp yx
),( 1 pp yx +
midpointM
Spring 200
CS5600 8
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
),(211 ++ 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 200
CS5600 9
Spring 2006 CS 5600 33
How will d be used?
Recall,
Therefore,
cybxad pp ++++= )((21)1
⎪⎩
⎪⎨
⎧
⇒=⇒<⇒>
=)(arbitrary 0
line) ideal above(midpoint 0line) ideal below(midpoint 0
EENE
d
Spring 2006 CS 5600 34
Case 1: Suppose E is chosen• Recall
•
• ...
cybxad ppold ++++= )((21)1
cybxa
yxFd
pp
ppnew
++++=
++=∴)((
(
21
21
)2
),2
1: ; , yE x x y⇒ ← + ←
Spring 2006 CS 5600 35
...
add
cybxa
cybxadd
oldnew
pp
ppoldnew
+=
⎟⎠⎞⎜
⎝⎛ ++++−
⎟⎠⎞⎜
⎝⎛ ++++=−
)(1(
)(2(
21
21
)
)
Case 1: Suppose E is chosen
Spring 2006 CS 5600 36
Review of Explicit to ImplicitRecall,
Or,
where,
0=+− Byxdxdy
0)()()( =+−+ Bdxydxxdy
0)()()(),( =+−+=∴ BdxydxxdyyxF
)();();( dxBcdxbdya =−==
Spring 200
CS5600 10
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
Ea
a dyF M
a d E
Δ ≡Δ ==
Δ =
Spring 2006 CS 5600 38
Case 2: Suppose NE chosen
Recall
...
12
1)( ( )old p pd a x b y c= + + + +
32
32
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(
21
23
)
)
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
NEa b
a dy b dxF M
a b d NE
Δ ≡Δ = +
= = −
Δ = +
Spring 200
CS5600 11
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),(
21
1 00 ++ yx
),(),(21
121
1 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 200
CS5600 12
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
221
1
)(
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
221
1
)(
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
dyyxF dx
−=++
⎟⎠⎞
⎜⎝⎛−=++
)(2),(2
gives, 2by
)(),( gMultiplyin
21
1
221
1
00
00
What is initial value of d ?
Spring 200
CS5600 13
Spring 2006 CS 5600 49
2 ( , ) 2( ) 0So, 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 ,
dxdydd
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 200
CS5600 14
Spring 2006 CS 5600 53
Graph1312 11 10
9 8 7 6
4 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
⇒=>=−=
=
20246)()2(2)85(
Spring 2006 CS 5600 55
Graph1312 11 10
9 8 7 6
4 5 6 7 8 9 10 11
Spring 2006 CS 5600 56
Example (dx=4; dy=3 )
• Update value of d•
Ed
dy-dx dNE
⇒=−=−=−=
=
0222)43(2
)(2)9,6(2so , wasmoveLast
Spring 200
CS5600 15
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) = - 2d = 2 – 2 = 0 ⇒ E
Spring 2006 CS 5600 58
Graph1312 11 10
9 8 7 6
4 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) 60 6 0
d dy
d NE
== == + > ⇒
Spring 2006 CS 5600 60
Graph1312 11 10
9 8 7 6
4 5 6 7 8 9 10 11
Spring 200
CS5600 16
Spring 2006 CS 5600 61
Example (dx=4; dy=3 )
• Previous move was , so2 (8,10) 2( )
2(3 4) 26 2 4
N Ed dy - dx
d N E
== − = −= − = ⇒
Spring 2006 CS 5600 62
Graph1312 11 10
9 8 7 6
4 5 6 7 8 9 10 11
Spring 2006 CS 5600 63
Graph1312 11 10
9 8 7 6
4 5 6 7 8 9 10 11
Spring 2006 CS 5600 64
Graph1312 11 10
9 8 7 6
4 5 6 7 8 9 10 11
Spring 200
CS5600 17
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
1211109876
4 5 6 7 8 9 10
Example Example
Spring 200
CS5600 18
Spring 2006 CS 5600 69
Bresenham Circles
Spring 2006 CS 5600 70
The End Bresenham’s Algorithm
Lect
ure
Set
1