Theory of Computation & Computer Graphics — Two-Dimensional Transformation, NEC licence examination syllabus (Nepal Engineering Council).
Cohen-Sutherland Line Clipping: deciding what's actually visible
Why draw a line that's completely off-screen? This algorithm figures that out FAST, using pure bit tricks.
Clipping decides which parts of a line fall inside a rectangular viewing window, discarding what's outside. Cohen-Sutherland assigns each endpoint a 4-bit region code based on its position relative to the window.
Region code bits (TBRL — Top, Bottom, Right, Left):
Bit 1 (Top): y > y_max
Bit 2 (Bottom): y < y_min
Bit 3 (Right): x > x_max
Bit 4 (Left): x < x_min
Code 0000 = point is INSIDE the window
The three quick decision rules:
1. Both codes = 0000 → line is COMPLETELY INSIDE, draw it as-is
2. Bitwise AND of both codes ≠ 0000 → line is COMPLETELY OUTSIDE,
discard it entirely (both points are outside on the SAME side)
3. Otherwise → line PARTIALLY crosses the boundary — compute the
actual intersection point with the boundary, replace the
outside endpoint, and re-test
The genius here: rules 1 and 2 let you instantly discard or accept a huge number of lines using only a bitwise AND — no complicated math needed. Only lines that fail BOTH quick tests need the more expensive intersection-point calculation.
💡 Practice assigning region codes to several points around a window, then working through a full clip of at least one line that needs an actual intersection calculation — this full walkthrough is exactly how it's graded.
A full worked clip
Take the window (0,0) to (10,10) and the line from P₀(−2, 3) to P₁(14, 9).
Step 1 — assign region codes (TBRL):
P₀(−2, 3): y=3 not above 10, not below 0 → top/bottom bits 0
x=−2 is < x_min=0 → LEFT bit set
code = 0001
P₁(14, 9): y=9 inside → top/bottom bits 0
x=14 is > x_max=10 → RIGHT bit set
code = 0010
Step 2 — apply the quick tests:
Both zero? 0001, 0010 → no, not trivially accepted
Bitwise AND ≠ 0000? 0001 AND 0010 = 0000 → no, not trivially rejected
So the line must be clipped.
Step 3 — clip against each violated boundary.
dx = 14 − (−2) = 16 dy = 9 − 3 = 6
P₀ is LEFT of the window, so intersect with x = 0:
t = (0 − (−2)) / 16 = 2/16 = 0.125
y = 3 + 0.125 × 6 = 3.75
New P₀ = (0, 3.75), code now 0000 ✓
P₁ is RIGHT of the window, so intersect with x = 10:
t = (10 − (−2)) / 16 = 12/16 = 0.75
y = 3 + 0.75 × 6 = 7.5
New P₁ = (10, 7.5), code now 0000 ✓
Both codes are now 0000 → accept and draw (0, 3.75) to (10, 7.5).
Note the loop structure: after replacing an endpoint you recompute its code and re-test, because a point moved onto one boundary may still lie outside another. A line crossing a corner region needs two rounds before it resolves.
💡 Why the bitwise AND rejects: a non-zero AND means both endpoints have the same bit set, so both are outside on the same side — beyond the top edge, say. No line with both ends above the window can cross it, so it is discarded without any arithmetic. That single test eliminates most off-screen lines in a real scene.
Syllabus points
Region codes; algorithm steps (numerical)
Create a free account to tick topics off, take notes as you read, watch the video lessons and get a day-by-day study plan built around your exam date.