Upload
dotuyen
View
221
Download
0
Embed Size (px)
Citation preview
1
1
CS 430Computer Graphics I
Line Drawing and ClippingWeek 1, Lecture 2
David Breen, William Regli and Maxim Peysakhov
Geometric and Intelligent Computing Laboratory
Department of Computer Science
Drexel Universityhttp://gicl.mcs.drexel.edu
2
Outline
• Line drawing
• Digital differential analyzer
• Bresenham’s algorithm
• Cohen-Sutherland line clipping• Parametric line clipping
3
Scan-Conversion Algorithms
• Scan-Conversion:Computing pixelcoordinates forideal line on2D raster grid
• Pixels bestvisualized ascircles/dots– Why? Monitor
hardware
1994 Foley/VanDam/Finer/Huges/Phillips ICG
4
Digital Differential Analyzer(DDA)
• If slope is less then 1 _x= 1 (otherwize _y = 1)
• Compute corresponding_y (_x)
• xk+1 = xk + _x• yk+1 = yk + _y• Issues:
– Avoiding floatingpoint ops,multiplications, andreal numbers
1994 Foley/VanDam/Finer/Huges/Phillips ICG
xk+1
_xxk
yk+1
yk
_y
5
Bresenham’s Algorithm
• 1965 @ IBM
• Basic Idea:– Only integer
arithmetic
– Incremental
• Consider the implicitequation for a line:
1994 Foley/VanDam/Finer/Huges/Phillips ICG
6
Bresenham’s Algorithm
Given:
implicit line equation:
Let:where r and q are points on the line
Then:
Observe that all of these are integers
and: for points above the line for points below the line
Now…..Pics/Math courtesy of Dave Mount @ UMD-CP
2
7
Bresenham’s Algorithm
• Suppose we justfinished– (assume 0<slope<1)
other cases symmetric
• Which pixel next?– E or NE
Pics/Math courtesy of Dave Mount @ UMD-CP
8
Bresenham’s Algorithm
Assume:
• = exact y value at
• y midway between E and NE:
Observe:
If , then pick E
Else pick NE
If ,it doesn’t matter
Pics/Math courtesy of Dave Mount @ UMD-CP
9
Bresenham’s Algorithm
• Create “modified” implicit function (2x)
• Create a decision variable D to select,where D is the value of f at the midpoint:
Pics/Math courtesy of Dave Mount @ UMD-CP
10
Bresenham’s Algorithm
• If then m is below the line– NE is the closest pixel
• If then m is above the line– E is the closest pixel
• Note: because we multiplied by 2x, D isnow an integer---which is very good news
• How do we make this incremental??
11
Case I: When E is next
• What increment for computing an new D?• Next midpoint is:
• Hence, increment by:Pics/Math courtesy of Dave Mount @ UMD-CP
12
Case II: When NE is next
• What increment for computing an new D?• Next midpoint is:
• Hence, increment by:Pics/Math courtesy of Dave Mount @ UMD-CP
3
13
How to get an initial value for D?
• Suppose we start at:
• Initial midpoint is:
Then:
Pics/Math courtesy of Dave Mount @ UMD-CP
14
The Algorithm
Assumptions:
Pre-computed:Pics/Math courtesy of Dave Mount @ UMD-CP
15
Bresenham’s Algorithm:Example
16
Bresenham’s Algorithm:Example
17
Bresenham’s Algorithm:Example
18
Bresenham’s Algorithm:Example
4
19
Bresenham’s Algorithm:Example
20
Bresenham’s Algorithm:Example
21
Bresenham’s Algorithm:Example
22
Bresenham’s Algorithm:Example
23
Bresenham’s Algorithm:Example
24
Some issues withBresenham’s Algorithms
• Pixel ‘density’ variesbased on slope– straight lines look
darker, more pixels perCM
• Endpoint order– Line from P1 to P2
should match P2 to P1
– Always choose E whenhitting M, regardless ofdirection
1994 Foley/VanDam/Finer/Huges/Phillips ICG
5
25
Some issues withBresenham’s Algorithms
• How to handle the line when it hits the clipwindow?
• Vertical intersections– Could change line slope
– Need to change init cond.
• Horizontal intersections– Again, changes in the
boundary conditions
– Can’t just intersectthe line w/ the box
1994 Foley/VanDam/Finer/Huges/Phillips ICG
26
Scissoring Clipping
Pics/Math courtesy of Dave Mount @ UMD-CP
Performed during scan conversion of theline (circle, polygon)
Compute the next point (x,y)If xmin £ x £ xmax and ymin £ y £ ymax
Then output the point.
Else do nothing
• Issues with scissoring:– Too slow
– Does more work then necessary
– We are not going to use it.
27
The Cohen-Sutherland LineClipping Algorithm
• How to clip lines to fit inwindows?– easy to tell if whole line
falls w/in window
– harder to tell what partfalls inside
• Consider a straight line
• And window:
Pics/Math courtesy of Dave Mount @ UMD-CP
28
Cohen-Sutherland
Basic Idea:
• First, do easy test– completely inside or
outside the box?
• If no, we need amore complex test
• Note: we will alsoneed to figure outhow scan line meetsthe box
Pics/Math courtesy of Dave Mount @ UMD-CP
29
Cohen-Sutherland
• The Easy Test:
• Compute 4-bit codebased on endpointsP1 and P2
Pics/Math courtesy of Dave Mount @ UMD-CP
30
Cohen-Sutherland
• Line is completelyvisible iff both codevalues of endpointsare 0, i.e.
• If line segments arecompletely outsidethe window, then
Pics/Math courtesy of Dave Mount @ UMD-CP
6
31
Cohen-Sutherland
Otherwise,we clip the lines:
• We know that thereis a bit flip, w.o.l.g.assume its (x0 ,x1 )
• Which bit? Try `em all!– suppose its bit 4
– Then x0 < WL and we know that x1 WL
– We need to find the point: (xc ,yc )≥
Pics/Math courtesy of Dave Mount @ UMD-CP
32
Cohen-Sutherland
• Clearly:
• Using similar triangles
• Solving for yc gives
Pics/Math courtesy of Dave Mount @ UMD-CP
33
Cohen-Sutherland
• Replace (x0 ,y0 ) with (xc ,yc )
• Re-compute values
• Continue until allpoints are inside theclip window
QED
Pics/Math courtesy of Dave Mount @ UMD-CP
34
Parametric Line Clipping
• Developed by Cyrus and Beck in 1978• Used to clip 2D/3D lines against convex
polygon/polyhedron• Liang and Barsky (1984) algorithm efficient
in clipping upright 2D/3D clipping regions• First we will follow original Cyrus-Beck
development to introduce parametric clipping• Then we will reduce Cyrus-Beck to more
efficient Liang-Barsky case
35
The Cyrus-Beck Technique
• Cohen-Sutherland algorithm computes (x,y)intersections of the line and clipping edge
• Cyrus-Beck finds a value of parameter t forintersections of the line and clipping edges
• Simple comparisons used to find actualintersection points
• Liang-Barsky optimizes it by examining tvalues as they generated to reject some linesegments immediately
36
Finding the Intersection Points
Line P(t) = P0 +t(P1-P0)Point on the edge PEi
Make sure1. D ≠ 0, or P1 ≠ P0
2. Ni• D ≠ 0 lines are not parallel
1994 Foley/VanDam/Finer/Huges/Phillips ICG
DN
]P[PNt
)-P (PLet D
]-Pt[PN ]-P[PN
])-P-Pt(P[PN
][P(t)-PN
i
Eii
iEii
Eii
Eii
•-
-•=
=
=•+•
=+•
=•
0
01
010
010
0
0
0
7
37
Finding the Line Segment
• Classify point as potentially entering (PE) or leaving (PL)
• PE if crosses edge into inside half plane => angle P0 P1and Ni greater 90° => Ni• D < 0
• PL otherwise.
• Find Te = max(te)
• Find Tl = min(tl)
• Discard if Te > Tl
• If Te < 0 Te = 0
• If Tl > 1 Tl = 1
• Use Te, Tl to compute intersection coordinates (xe,ye), (xl,yl)
1994 Foley/VanDam/Finer/Huges/Phillips ICG