Explain the Cohen-Sutherland line clipping algorithm. Describe the 4-bit…
Explain the Cohen-Sutherland line clipping algorithm. Describe the 4-bit region code (outcode) assignment and discuss the step-by-step working mechanism used to determine the visibility of a line. How does the algorithm use bitwise operators for Trivial Acceptance and Trivial Rejection?
Attempted by 37 students.
Show answer & explanation
Introduction
The Cohen-Sutherland algorithm is one of the oldest and most popular line-clipping algorithms. It speeds up the process of clipping by performing initial tests on line endpoints to determine if a line can be accepted or rejected "trivially" (immediately) without calculating intersections.
The 4-Bit Binary Codes (Outcodes)
The algorithm divides the 2D plane into 9 regions. Each region is assigned a 4-bit binary code based on its position relative to the clip window boundaries (Xmin, Xmax, Ymin, Ymax).

The bits are usually referred to as TBRL (Top, Bottom, Right, Left):
Bit 1 (Top): 1 if y > Ymax, else 0.
Bit 2 (Bottom): 1 if y < Ymin, else 0.
Bit 3 (Right): 1 if x > Xmax, else 0.
Bit 4 (Left): 1 if x < Xmin, else 0.
The codes for the regions are:
Window (Center): 0000
Top-Left: 1001 | Top-Center: 1000 | Top-Right: 1010
Mid-Left: 0001 | Mid-Right: 0010
Bottom-Left: 0101 | Bottom-Center: 0100 | Bottom-Right: 0110
Working Mechanism (Step-by-Step)
Step 1: Assign Outcodes
Calculate the 4-bit codes for both endpoints of the line segment (P1 and P2).
Step 2: Trivial Acceptance (Logical OR)
If (Code1 OR Code2) == 0000, both points are inside the window. The line is completely visible. Draw the line and exit.
Step 3: Trivial Rejection (Logical AND)
If (Code1 AND Code2) != 0000, both points are outside the window in the same half-plane (e.g., both are to the left of the window). The line is completely invisible. Discard the line and exit.
Step 4: Intersection and Clipping (Partial Visibility)
If neither of the above is true, the line is a clipping candidate.
Select an endpoint that is outside the window (its code is not 0000).
Find the intersection point of the line with the boundary corresponding to the "1" bit in the code.
Example: If the 'Top' bit is 1, calculate intersection with y = Ymax using x = x1 + (x2 - x1).(Ymax - y1)/{y2 - y1}.
Replace the outside point with the new intersection point.
Repeat Step 1 until the line is either trivially accepted or rejected.