Clipping: Computer Graphics & Visualization
Clipping: Computer Graphics & Visualization
Lecture 5
Line and Polygon Clipping
Clipping
Cohen-Sutherland line clipping algorithm Liang-Barsky line clipping algorithm Sutherland-Hogeman polygon clipping
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
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.
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)
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
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
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
Pedher Johansson
Department of Computing Science, Ume University
12
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
uleft = uright
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 =
ubottom = utop =
15
q4 ywmax y0 10 2 2 = = = 14 2 3 y p4
Computer Graphics & Visualization
16
Pedher Johansson
Department of Computing Science, Ume University
T R B
If P0 is to the left:
LT
L
inside
edge region
corner region
T L LR L LB
T
P0
L
TR
LB
TB
Pedher Johansson
18
19
Pedher Johansson
Department of Computing Science, Ume University
20
Pedher Johansson
Department of Computing Science, Ume University