Using the Cohen–Sutherland line-clipping algorithm, determine whether each of…
2010
Using the Cohen–Sutherland line-clipping algorithm, determine whether each of the following line segments is displayed unchanged, clipped, or rejected for the rectangular window with lower-left corner (20, 40) and upper-right corner (200, 200): (i) (10, 15) to (15, 30); (ii) (150, 100) to (250, 100). If a segment is clipped, give the visible endpoints.
Show answer & explanation
Concept
The Cohen–Sutherland algorithm assigns a four-bit outcode to each endpoint. The bits record whether the point lies to the left, right, below, or above the rectangular clipping window.
If both outcodes are zero, the segment is accepted unchanged. If their bitwise AND is non-zero, the segment is rejected; otherwise, each outside endpoint is intersected with the relevant window boundary until the segment is accepted or rejected.
Application
Use the convention LEFT = 0001, RIGHT = 0010, BOTTOM = 0100, and TOP = 1000. The window bounds are xmin = 20, xmax = 200, ymin = 40, and ymax = 200.
For the segment from (10, 15) to (15, 30), both endpoints are left of xmin and below ymin, so both outcodes are 0101 (LEFT | BOTTOM).
Their bitwise AND is 0101, which is non-zero. Therefore this segment is trivially rejected and is not displayed.
For the segment from (150, 100) to (250, 100), the first endpoint is inside the window, so its outcode is 0000. The second endpoint is to the right of xmax, so its outcode is 0010 (RIGHT).
The bitwise AND is zero, so the segment is neither trivially accepted nor trivially rejected. Clip the outside endpoint against the right boundary x = 200.
The segment is horizontal at y = 100, so it meets x = 200 at (200, 100). The remaining endpoints (150, 100) and (200, 100) are inside or on the window boundary.
Cross-check
The first segment has x-values only from 10 to 15 and y-values only from 15 to 30; it cannot reach a window that begins at x = 20 and y = 40.
For the second segment, y = 100 lies within [40, 200], and the overlap of its x-range [150, 250] with the window x-range [20, 200] is [150, 200].
Result
The segment from (10, 15) to (15, 30) is rejected totally. The segment from (150, 100) to (250, 100) is clipped and displayed from (150, 100) to (200, 100); neither segment is displayed unchanged.