Overview
Lecture 5
Line and Polygon Clipping
Clipping
Cohen-Sutherland line clipping algorithm Liang-Barsky line clipping algorithm Sutherland-Hogeman polygon clipping
Computer Graphics & Visualization
Computer Graphics & Visualization
Pedher Johansson
Department of Computing Science, Ume University
Clipping Algorithms
Line Clipping:
Cohen-Suterland (encoding)
- Oldest and most commonly used
Cohen-Sutherland Line-Clipping
1. Encode end points Bit 0 = point is left of window Bit 1 = point is right of window Bit 2 = point is below window Bit 3 = point is above window If C0 Cend 0 then P0Pend is trivially rejected If C0 Cend = 0 then P0Pend is trivially accepted Otherwise subdivide and go to step 1 with new segment.
Computer Graphics & Visualization
1001
1000
1010
Nicholl-Lee-Nicholl (encoding) (more efficient) Liang-Barsky (parametric)
- More efficient than Cohen-Sutherland
0001
0000
0010
2.
0101
0100
0110
Polygon Clipping:
Sutherland-Hodgeman (divide and conquer strategy) Weiler-Atherton (modified for concave polygons)
3.
C0 =
Bit code of P0
4.
Cend = Bit code of Pend
Pedher Johansson
Department of Computing Science, Ume University
Computer Graphics & Visualization
Pedher Johansson
Department of Computing Science, Ume University
Cohen-Sutherland Line-Clipping
Clip order: Left, Right, Bottom, Top 1) A1C1 2) B1C1 3) reject 1) 2) 3) 4) 5) A2E2 B2E2 1001 B2D2 B2C2 accept A1 0001 C1 B1 1000 C2 B2 0000 A3 A2 0101 D3
Computer Graphics & Visualization
Pedher Johansson
Department of Computing Science, Ume University
Cohen-Sutherland Line-Clipping
Will do unnecessary clipping. Not the most efficient. Clipping and testing are done in fixed order. Efficient when most of the lines to be clipped are either rejected or accepted (not so many subdivisions). Easy to program. Parametric clipping are more efficient.
6
Computer Graphics & Visualization
Pedher Johansson
Department of Computing Science, Ume University
E2 1010 D2
0010
1) 2) 3) 4)
A3D3 A3C3 A3B3 accept
C3
B3 0100
0110
Parametric form
A line segment with endpoints (x0, y0) and (xend, yend) we can describe in the parametric form x = x0 + ux y = x0 + uy where x = xend x0 y = yend y0
7
Computer Graphics & Visualization
Pedher Johansson
Department of Computing Science, Ume University
Liang-Barsky Line-Clipping
More efficient than Cohen-Sutherland A line is inside the clipping region for values of u such that: x = xend x0 xwmin x0 + ux xwmax ywmin y0 + uy ywmax y = yend y0 Can be described as u pk qk, k = 1, 2, 3, 4
8
Computer Graphics & Visualization
Pedher Johansson
Department of Computing Science, Ume University
0u1
Liang-Barsky Line-Clipping
The infinitely line intersects the clip region edges when:
p1 = x q1 = x0 xwmin p2 = x q2 = xwmax x0
Liang-Barsky Line-Clipping
When pk < 0, as u increases
- line goes from outside to inside - entering
When pk > 0,
q uk = k where p3 = y q3 = y0 ywmin pk p4 = y q4 = ywmax y0
Left boundary Right boundary Bottom boundary Top boundary
- line goes from inside to outside - exiting
When pk = 0,
- line is parallel to an edge
If there is a segment of the line inside the clip region, a sequence of infinite line intersections must go: entering, entering, exiting, exiting
10
Computer Graphics & Visualization
Pedher Johansson
Department of Computing Science, Ume University
Computer Graphics & Visualization
Pedher Johansson
Department of Computing Science, Ume University
Liang-Barsky Line-Clipping
Enter Exit Enter Enter Enter Exit Exit Exit Clip region
Liang-Barsky Line-Clipping
1. Set umin = 0 and umax = 1. 2. Calculate the u values: 3. If u < umin or u > umax ignore it. Otherwise classify the u values as entering or exiting. 4. If umin < umax then draw a line from:
( x0 + x umin, y0 + y umin ) to ( x0 + x umax, y0 + y umax )
11
Computer Graphics & Visualization
Pedher Johansson
Department of Computing Science, Ume University
12
Computer Graphics & Visualization
Pedher Johansson
Department of Computing Science, Ume University
Example Liang-Barsky
P0(-5,3)
0,10
10,10 Pend(15,9)
Liang-Barsky Line-Clipping
We have umin = 1/4 and umax = 3/4
0,0
10,0 Entering Exiting umin = 1/4 umax = 3/4
uleft = uright
q1 x0 xwmin 1 50 = = = p1 (15 (5)) 4 x
Pend - P0 = (15+5,9-3) = (20,6)
x y
xw x 10 (5) 3 q = 2 = max 0 = = p2 15 (5) 4 x
If umin < umax , there is a line segment
- compute endpoints by substituting u values
q y ywmin 30 1 ubottom = 3 = 0 = = 2 p3 y (9 3) utop = q4 ywmax y0 10 3 7 = = = 93 6 y p4
Computer Graphics & Visualization
u < 0 then ignore u > 1 then ignore
Pedher Johansson
Department of Computing Science, Ume University
Draw a line from (-5+(20)(1/4), 3+(6)(1/4)) to (-5+(20)(3/4), 3+(6)(3/4))
14
Computer Graphics & Visualization
Pedher Johansson
Department of Computing Science, Ume University
13
Example Liang-Barsky
P0(-8,2)
0,10
Pend(2,14)
10,10
Liang-Barsky Line-Clipping
We have umin = 4/5 and umax = 2/3
0,0
uleft = q1 x0 xwmin 4 80 = = = p1 x (2 (8)) 5 q2 xwmax x0 10 (8) 9 = = = p2 2 (8) 5 x
Entering
10,0
umin = 4/5
Pend - P0 = (2+8, 14-2) = (10, 12) umin > umax , there is no line segment do draw
uright =
u > 1 then ignore
ubottom = utop =
q3 y0 ywmin 20 1 = = = u < 0 then ignore 6 p3 y (14 2)
Exiting umax = 2/3
Pedher Johansson
Department of Computing Science, Ume University
15
q4 ywmax y0 10 2 2 = = = 14 2 3 y p4
Computer Graphics & Visualization
16
Computer Graphics & Visualization
Pedher Johansson
Department of Computing Science, Ume University
Nicholl-Lee-Nicholl Line Clipping
Avoids multiple clipping of an individual line by creating more regions. Only three regions need to be considered.
P0 P0 P0
Nicholl-Lee-Nicholl Line Clipping
If P0 inside and Pend outside:
L
P0
T R B
If P0 is to the left:
LT
L
If P0 is to the left and above:
P0
inside
edge region
corner region
T L LR L LB
T
P0
L
TR
Find position of Pend relative to P0.
17
Computer Graphics & Visualization
Pedher Johansson
Department of Computing Science, Ume University
LB
TB
Pedher Johansson
18
Computer Graphics & Visualization
Department of Computing Science, Ume University
Nicholl-Lee-Nicholl Line Clipping
To determine region
compare slopes, and boundaries of the NLN region.
Suterland-Hodgeman Polygon Clipping
Four test cases:
1. 2. 3. 4. First vertex inside and the second outside (in-out pair) Both vertices inside clip window First vertex outside and the second inside (out-in pair) Both vertices outside the clip window
Concave polygons may be displayed with extra lines.
19
Computer Graphics & Visualization
Pedher Johansson
Department of Computing Science, Ume University
20
Computer Graphics & Visualization
Pedher Johansson
Department of Computing Science, Ume University
Weiler-Atherton Polygon Clipping
Clips concave polygons correctly. Instead of always going around the polygon edges, we also, want to follow window boundaries. For an outside-to-inside pair of vertices, follow the polygon boundary. For an inside-to-outside pair of vertices, follow the window boundary in a clockwise direction.
21
Computer Graphics & Visualization
Pedher Johansson
Department of Computing Science, Ume University