Window
Viewport
Viewing in 2D - Viewport
250
45
Viewport in
Device coords
Pixels. Pixels.
The mapping of a 2D world-coordinate scene description to device coordinates is called a 2D viewing transformation or window-toviewport transformation
Clipping Window
The clipping window selects what we want to see; the viewport indicates where it is to be viewed on the output device.
To transform the world-coordinate point into the same relative position within the viewport, we require that
Clipping
Any procedure that identifies those portions of a picture that are either inside or outside of a specified region of space is referred to as clipping.
o
o
2-D Clipping
Fill-area clipping
o o
Sutherland-Hodgeman Weiler-Atherton
Point Clipping
wy2
(x, y)
wy1 wx1
wx2
Point Clipping
Assume that clip window is rectangle. Save a point P(x,y) for display if xwmin x xwmax ywmin y ywmax
Line Clipping
P4
P3 P2
P8
P6
P5 P9 Before Clipping
P10
Line Clipping
P4
P3
P8
P6
P5 After Clipping
1001
1000
1010
(above,below,right,left)
0001
0000
Window
0010
0101
0100
0110
14
Otherwise, divide line into two segments Intersection of line with window edge is determined and trivial accept/ reject test is repeated. The process is repeated until line is accepted.
1. Given a line segment with endpoint P1 = (x1, y1) and P2 = (x2, y2) 2. Compute the 4 bit code for each endpoint. If both codes are 0000 (bitwise OR of codes yields 0000) line lies completely inside the window: pass
This line must be clipped at window edge before being passed to drawing routine. 4. Examine one of the endpoints say P1 = (x1, y1), read P1s 4 bit code in order Left to Right, Bottom to Up. 5. When a set bit (1) is found, compute intersection I of
corresponding window edge with line from P1 to P2. Replace P1 with I and repeat the algorithm.
P4
P3 P2
P8
P6
P5 P9
P10
Classify Some Lines Quickly by AND of Bit Codes Representing Regions of Two Endpoints (Must Be 0) P7 1000 1001 1010 Bit 4 P1
P4 0001
P2 0101 P5 Bit 1 P3
0000 P6
0100 P9
P8 0010
Bit 3
Classify Some Lines Quickly by AND of Bit Codes Representing Regions of Two Endpoints (Must Be 0) P7 1000 1001 1010 Bit 4 P1
P4 0001
P2 0101 P5 Bit 1 P3
0000 P6
0100 P9
P8 0010
Bit 3
Classify Some Lines Quickly by AND of Bit Codes Representing Regions of Two Endpoints (Must Be 0) P7 1000 1001 1010 Bit 4
P4 0001
P3
0000 P6
P8 0010
Bit 3
0101 P5 Bit 1
0100
Compute Intersections with Window Boundary for Lines That Cant be Classified Quickly P7 1000 1001 1010
Bit 4
P4 0001
P3
0000 P6
P8 0010
Bit 3
0101 P5 Bit 1
0100
Compute Intersections with Window Boundary for Lines That Cant be Classified Quickly P7 1000 1001 1010
Bit 4
P4 0001
P3
0000 P6
P8 0010
Bit 3
0101 P5 Bit 1
0100
Compute Intersections with Window Boundary for Lines That Cant be Classified Quickly P7 1000 1001 1010
Bit 4
P4 0001
P3
0000 P6
P8 0010
P5 0101 Bit 1
Bit 3
0100
Compute Intersections with Window Boundary for Lines That Cant be Classified Quickly P7 1000 1001 1010
Bit 4
P4 0001
P3
0000 P6
P8 0010
P5 0101 Bit 1
Bit 3
0100
Compute Intersections with Window Boundary for Lines That Cant be Classified Quickly P7 1000 1001 1010
Bit 4
P4 0001
P3
0000 P6
P8 0010
P5 0101 Bit 1
Bit 3
0100
Compute Intersections with Window Boundary for Lines That Cant be Classified Quickly P7 1000 1001 1010
Bit 4
P4 0001
P3
0000 P6
P8 0010
P5 0101 Bit 1
Bit 3
0100
Compute Intersections with Window Boundary for Lines That Cant be Classified Quickly 1001 1000 1010 P
Bit 4
P4 0001
P3
0000 P6
P8 0010
P5 0101 Bit 1
Bit 3
0100
Compute Intersections with Window Boundary for Lines That Cant be Classified Quickly 1001 1000 1010 P7
Bit 4
P4 0001
P3
0000 P6
P8 0010
P5 0101 Bit 1
Bit 3
0100
Compute Intersections with Window Boundary for Lines That Cant be Classified Quickly 1001 1000 1010 P7
Bit 4
P4 0001
P3
P8 0010
0000 P6
P5 0101 Bit 1
Bit 3
0100
Compute Intersections with Window Boundary for Lines That Cant be Classified Quickly 1001 1000 1010 P7
Bit 4
P4 0001
P3
P8 0010
0000 P6
P5 0101 Bit 1
Bit 3
0100
Compute Intersections with Window Boundary for Lines That Cant be Classified Quickly 1001 1000 1010 P7
Bit 4
P4 0001
P3
P8 0010
0000 P6
P5 0101 Bit 1
Bit 3
0100
Compute Intersections with Window Boundary for Lines That Cant be Classified Quickly 1001 1000 1010 P7
Bit 4
P4 0001
P3
P8 0010
0000 P6
P5 0101 Bit 1
Bit 3
0100
Compute Intersections with Window Boundary for Lines That Cant be Classified Quickly 1001 1000 1010 P
Bit 4
P4 0001
P3
P8 0010
0000 P6
P5 0101 Bit 1
Bit 3
0100
Compute Intersections with Window Boundary for Lines That Cant be Classified Quickly 1001 1000 1010 P7
Bit 4
P4 0001
P3
P8 0010
0000 P6
P5 0101 Bit 1
Bit 3
0100
Compute Intersections with Window Boundary for Lines That Cant be Classified Quickly 1001 1000 1010 P7
Bit 4
P4 0001
P3
P8 0010
0000 P6
P5 0101 Bit 1
y = m (xL x1 ) + y1
For right window edge, Intersection point will be (xR, y)
y = m (xR x1 ) + y1
For top window edge, Intersection point will be (x, yT)
x = x1 + (yT y1 ) / m
For bottom window edge, Intersection point will be (x, yB)
x = x1 + (yB y1 ) / m
Goal: find range of u for which x and y both inside the viewing window
Mathematically:
xmin <= x1 + ux <= xmax ymin <= y1 + uy <= ymax u*(-x) <= (x1 xmin) u*(x) <= (xmax x1) u*(-y) <= (y1 ymin) u*(y) <= (ymax y1)
Rearranged
1: 2: 3: 4:
k=1,2,3,4
p1 = - x p2 = x p3 = - y p4 = y
Observations
1)
If qk < 0, line is completely outside the boundary and can be eliminated If qk > 0, the line is inside the boundary and needs further consideration
b)
2)
If pk < 0, line proceeds from outside to inside of corresponding boundary line. If pk > 0, line proceeds from inside to outside of corresponding boundary line. If pk 0, calculate value of u as u = qk / pk
3)
4)
Algorithm
Rules:
1) pk = 0: the line is parallel to boundaries
If for that same k, qk < 0, its outside Otherwise its inside rk = qk/pk u1 = max(0, rk, u1) rk = qk/pk u2 = min(1, rk, u2)
Algorithm
a)
If pk = 0 and qk < 0, eliminate the line and stop, otherwise proceed to next step. For all k such that pk < 0, calculate rk = qk / pk. Let u1 be maximum of set containing 0 and various values of r. For all k such that pk > 0, calculate rk = qk / pk. Let u2 be minimum of set containing 1 and various values of r. If u1 > u2, eliminate line. It is completely outside the window, otherwise use u1 and u2 to calculate the endpoints of clipped line.
b)
c)
d)
Example
H (8,9) y I (-1,7) G (6,6) A (11,6) F (8,4)
B (11,10)
ymax = 8
ymin = 2
xmin = 1
Conclusion
been computed.
Polygon Clipping
Before Clipping
Polygon Clipping
After Clipping
Clipping to a Boundary
Do Inside Test for Each Point in Sequence, Insert New Points When Cross Window Boundary, Remove Points Outside Window Boundary P2 Window Boundary P1 Inside
P3
P4
P5
Outside
Clipping to a Boundary
Do Inside Test for Each Point in Sequence, Insert New Points When Cross Window Boundary, Remove Points Outside Window Boundary P2 Window Boundary P1 Inside
P3
P4
P5
Outside
Clipping to a Boundary
Do Inside Test for Each Point in Sequence, Insert New Points When Cross Window Boundary, Remove Points Outside Window Boundary P2 Window Boundary P1 Inside
P3
P4
P5
Outside
Clipping to a Boundary
Do Inside Test for Each Point in Sequence, Insert New Points When Cross Window Boundary, Remove Points Outside Window Boundary P2 Window Boundary P1 Inside
P3
P4
P5
Outside
Clipping to a Boundary
Do Inside Test for Each Point in Sequence, Insert New Points When Cross Window Boundary, Remove Points Outside Window Boundary P2 Window Boundary P P1 Inside
P3
P4
P5
Outside
Clipping to a Boundary
Do Inside Test for Each Point in Sequence, Insert New Points When Cross Window Boundary, Remove Points Outside Window Boundary P2 Window Boundary P P1 Inside
P3
P4
P5
Outside
Clipping to a Boundary
Do Inside Test for Each Point in Sequence, Insert New Points When Cross Window Boundary, Remove Points Outside Window Boundary P2 Window Boundary P P1 Inside
P3
P4
P5
Outside
Clipping to a Boundary
Do Inside Test for Each Point in Sequence, Insert New Points When Cross Window Boundary, Remove Points Outside Window Boundary P2 Window Boundary P P1 P Inside Outside
P3
P4
P5
Clipping to a Boundary
Do Inside Test for Each Point in Sequence, Insert New Points When Cross Window Boundary, Remove Points Outside Window Boundary P2 Window Boundary P P1 P Inside Outside
Polygon Clipping I
NOTE!
Polygon Clipping II
Some difficulties:
Maintaining correct inside/outside Variable number of vertices Handle screen corners correctly
Example I
Example 2
Start
Left
Right
Bottom
Top
Step-1: Initialize the i/p list & o/p list such that i/p list contains set of all consecutive vertices of the polygon & the o/p list is empty. Step-2: Against each window edge L,R,B,T perform the following steps:
If V1 and V2 are inside the window then save V2 in o/p list. If V1 and V2 are outside the window then no operation is performed. If V1 is inside and V2 is outside then find the intersection point at window edge and save the intersection point in the o/p list
If V1 is outside & V2 is inside then find the intersection point at the window edge and save the intersection point V2 in the o/p list.
Step-3: When the next edge is taken the o/p list becomes the i/p list for that edge. Step-4: the o/p list contains the set of vertices which forms the clipped polygon area.
Example
Example (cont)
continue from
cached location
Example (concluded)
continue from
cached location
nothing added
finished