Professional Documents
Culture Documents
2D Clipping PDF
2D Clipping PDF
Clipping Points
Consider a clip window which is rectangular in shape. P(x, y) is a point to be displayed.
xwmax, ywmax
xwmin, ywmin
Point P (x; y) lies inside the rectangular clip window if and only if the following
inequalities hold:
xwmin x xwmax
ywmin y ywmax
If any of these inequalities are not satisfied, point P is outside (not visible) the window.
2D Clipping
The Cohen-Sutherland algorithm consists of the following parts:
1. Determine the outcodes of the endpoints.
2. Based on the outcodes, determine the visibility of the line
3. If the line is wholly visible, draw the line.
4. If the line is partially visible, determine the intersection points of the line with the
window boundaries and draw the line.
Endpoint Outcodes:
The clipping window divides the XY plane into 9 regions as shown below. Each region is
assigned a 4-bit code. The endpoints of a line can only lie in these regions. A 4-bit code is
assigned to each endpoint of a line segment. The code is determined according to which of
the following nine regions of the plane the endpoint lies in.
1001
1000
1010
0001
0000
0010
0101
0100
0110
To remember the outcodes, remember this mnemonic: TBRL, where T -> Top,
B -> Bottom, R -> Right, and L -> Left.
If the endpoint of a line segment is to the right of the window and above the window, its
outcode is 1010. Similarly, determine the outcodes of the other regions.
Case 1 Trivial Acceptance:
If both the endpoint codes are 0000, then both the end points of the line lie inside
the window and the line is visible. It is trivially accepted.
Case 2 Trivial Rejection:
If the logical AND of the codes of the endpoints is not 0000, the line is not visible.
It is trivially rejected.
Case 3 Clipping Candidate:
If the logical AND of the endpoint codes is 000, the line is a candidate for clipping.
Intersection Points:
Once it is determined in which category the line falls, we can determine the intersection
points of the line with the window boundaries.
The equation of a line passing through P1(x1, y1) and P2(x2, y2) is given by:
y y1 = m (x x1)
Page 2 of 6
OR y y2 = m (x x12)
2D Clipping
This can be rearranged as
y = m (x x1)
where,
Slope
m =
+ y1
y2 y1
x2 x1
The intersections of the line with the window edges are given by:
If slope m :
y = m (xL x1) + y1
y = m (xR x1) + y1
If slope m 0:
y y
x = x1 + T 1
m
y B y1
If m = , then the line is parallel to the left and the right edges and we only have to check
the top and bottom edges for intersection with the clipping boundary.
If m = 0, then the line is parallel to the top and the bottom edges and we need to only
check for intersection with the right and left window boundaries.
This algorithm terminates after at most four passes through the loop, since at each iteration
we retain only the position of the segment that has remained after testing at previous
window boundaries, and there are only four such boundaries. Thus, after at most four
iterations, trivial acceptance or rejection is assured.
Consider the line segment AD in the following figure:
D
1001
1000
1010
A
0001
0101
0000
0100
G
0010
0110
Page 3 of 6
2D Clipping
Logical AND of these two outcodes is (0000 AND 1001) = 0000. Hence, the line AD is a
candidate for clipping.
Choose D as the outside point. The code for D shows that it crosses the top edge(since T
bit is 1) and the left edge (since L bit is also 1). We first use the top edge to clip AD to AB
and we calculate point Bs outcode as 0000. (Remember, a point on the window boundary
is treated as inside the window, so its outcode is 0000). In the next iteration, we apply the
trivial acceptance/rejection tests to AB, and the line is trivially accepted or rejected.
Line EH requires multiple iterations.
(i)
(ii)
(iii)
(iv)
The point E has endpoint code 0100, so the algorithm chooses it as the outside
point. The endpoint code shows that the first edge against which the line is cut is
the bottom edge. So EI is clipped to FI.
In the second iteration, FI cannot be trivially accepted or rejected. The code for F is
0000 (since it is on the window boundary). So the algorithm chooses point I as the
outside point and its outcode is 1010. The first edge clipped against is the top edge,
yielding the segment FH.
The endpoint code of H is 0010. the clipping is now against the right edge to
segment FG.
FG is trivially accepted in the final iteration.
Cohen-Sutherland Algorithm:
1. Read the two end points of the line P1 (x1, y1), and P2 (x2, y2).
2. Read the co-ordinates of the two corners of the rectangular window (xL, yB) and
(xR, yT)
3. Assign the outcodes for each point P1 and P2:
a. Initialize the outcode to 0000 for each point P1and P2.
b. For each point:
i. Set bit 1 (L bit) to 1 if x < xL (point to the left)
ii. Set bit 2 (R bit) to 1 if x > xR (point to the right)
iii. Set bit 3 (B bit) to 1 if y < yB (point below the bottom)
iv. Set bit 4 (T bit) to 1 if y > yT (point above the top)
4. Check for visibility of line P1P2:
a. If the outcodes of both P1 and P2 are zero, then the line is completely visible.
Hence, draw the line and go to step 9.
b. If the outcodes for endpoints are not zero and outcode [{P1) AND {P2)] is also
not equal to zero, then the line is completely invisible. So reject the line and go
to step 9.
c. Else, line is partially visible (candidate for clipping) (i.e., processing continues
at step 5}.
5. Compute the intersecting edge of the clipping window by inspecting the outcodes of
the two end points:
a. If the outcodes for both P1 and P2 are non-zero, then find the intersection points
P/1 and P/2 with the boundary edges of the clipping window.
Page 4 of 6
2D Clipping
b. If the outcode for any one end point is non-zero, then find the intersection point
P/1 or P/2 with the boundary edge of the clipping window.
6. Divide the line segments considering intersection points.
7. Reject the line segment if any one end point of it appears outside the clipping window.
8. Draw the remaining line segments.
9. Stop.
P1
c
Pm1
Pm2
P2
The endpoint codes of the line are 0001 and 0100. Logical AND of codes is 0001 and
0100 is 0000. Hence, the line is a candidate for clipping.
Mukesh N. Tekwani (mukeshtekwani@hotmail.com)
Page 5 of 6
2D Clipping
Subdivide the line at point Pm1. The two halves of the line are now P1Pm1 and Pm1 P2. Both
these segments are again candidates for clipping.
Consider Pm1P2 . This is further subdivided at Pm2. The segment Pm1 Pm2 is totally visible
(since their outcodes are both 0000). Segment Pm2P2 is partially visible. Although line
Pm2P2 can be drawn, it will result in short segments being drawn. Instead, we save the
point Pm2 as the current farthest visible point from P1.
Now subdivide Pm2P2. each time a visible midpoint is found, it is treated as the farthest
visible point from P1 until the intersection with the bottom edge of the clipping window is
determined. This intersection is then declared the farthest visible point from P1.
The segment P1Pm1 is then examined in the same way. The visible portion of the line P1P2
is then drawn between the two intersections.
Midpoint Subdivision Algorithm:
1. Read the two endpoint coordinates P1 (x1, y1) and P2 (x2, y2).
2. Read the co-ordinates of the two corners of the rectangular window (xL, yB) and
(xR, yT)
3. Assign the outcodes for each point P1 and P2:
a. Initialize the outcode to 0000 for each point P1and P2.
b. For each point:
i. Set bit 1 (L bit) to 1 if x < xL (point to the left)
ii. Set bit 2 (R bit) to 1 if x > xR (point to the right)
iii. Set bit 3 (B bit) to 1 if y < yB (point below the bottom)
iv. Set bit 4 (T bit) to 1 if y > yT (point above the top)
4. Check for visibility of line P1P2:
a. If the outcodes of both P1 and P2 are zero, then the line is completely visible.
Hence, draw the line and go to step 6.
b. If the outcodes for endpoints are not zero and outcode [{P1) AND {P2)] is also
not equal to zero, then the line is completely invisible. So reject the line and go
to step 6.
c. Else, line is partially visible (candidate for clipping) (i.e., processing continues
at step 5}.
5. Divide the partially visible line in equal parts and repeat steps 3 to 5 for both
subdivided segments until you get completely visible and completely invisible line
segments.
6. Stop.
Page 6 of 6