How Cohen-Sutherland clips a segment with outcodes

Short answer: The Cohen-Sutherland algorithm clips a line segment to an axis-aligned rectangle by assigning each endpoint a 4-bit outcode. The bits say above, below, left, and right. If both codes are zero, the segment is inside. If the bitwise AND of the codes is nonzero, both endpoints share a half-plane and the segment is outside. Otherwise an endpoint that is outside is replaced by the intersection with the corresponding boundary, and the codes are recomputed. The loop is constant time for a rectangle.


What the problem actually asks

A segment against a rectangle has a small set of outcomes: both ends inside, both ends in the same exterior half-plane, or a crossing that must be cut. The outcode packages the four half-plane tests so the two trivial outcomes are bit operations. Only the crossing case does arithmetic, and it does it one boundary at a time.

The intersection with a vertical or horizontal boundary is a linear interpolation of the endpoint coordinates. Using the wrong boundary bit, or updating only one coordinate, leaves the point outside and the loop can spin if the code is not recomputed from the new point.


What the algorithm guarantees

The algorithm returns the clipped segment inside the closed rectangle, or reports that the segment misses it. Each iteration brings at least one endpoint onto a boundary or rejects the segment, so the loop ends in a handful of steps. Extra memory is the two outcodes. The rectangle is axis-aligned. A rotated rectangle or a convex polygon needs a different clipper, such as Sutherland-Hodgman or Cyrus-Beck.

The trivial reject is the AND of the codes, not the OR. The OR is nonzero when any endpoint is outside, which includes segments that cross the rectangle and must be kept. Confusing AND with OR rejects visible segments.


The working steps

  1. Compute the outcode of an endpoint: set the left bit if x is less than xmin, the right bit if x is greater than xmax, the bottom bit if y is less than ymin, and the top bit if y is greater than ymax.
  2. If code0 OR code1 is zero, both ends are inside. Accept the segment.
  3. If code0 AND code1 is nonzero, reject the segment. Both ends are outside the same boundary.
  4. Otherwise pick an endpoint whose code is nonzero. Choose one set bit and replace that endpoint by the intersection with the corresponding edge of the rectangle.
  5. Recompute that endpoint’s code and repeat. When the loop accepts, the current endpoints are the clipped segment.

The bit tests are for an axis-aligned box. They do not clip to a triangle. The trivial reject is a shared outside half-plane. A segment that crosses a corner may take two iterations, one per boundary, and both are required. The geometric intersection is exact for that rectangle. Raster clipping of pixels is Bresenham after this geometric clip, not instead of it.


Approaches that look simpler and fail

Approach Why it fails when the trivial reject uses the OR of the outcodes
Reject when the OR is nonzero Any segment with one end outside has a nonzero OR, including segments that enter the rectangle. The reject test is the AND, which means a shared outside region.
Update the endpoint and keep the old outcode The next test sees a stale half-plane and can choose the same boundary again. Recompute the code from the new coordinates.
Clip to a rotated rectangle with the same four bits The half-planes are no longer the coordinate inequalities. The outcodes do not describe that rectangle.
Interpolate the wrong axis against a vertical edge A vertical edge fixes x and solves for y. Swapping the interpolation leaves the point off the edge and the loop does not converge.

Pixel selection after the clip is Bresenham. A general convex polygon clip is Sutherland-Hodgman. Cohen-Sutherland is the axis-aligned outcode clip.


A working example

This Python fragment clips a segment to an axis-aligned window with 4-bit outcodes. A segment is accepted when the OR of the codes is 0 and rejected when the AND is nonzero. The segment from (-1, 1) to (4, 1) against the window [0, 3] × [0, 3] becomes (0, 1)-(3, 1).

INSIDE, LEFT, RIGHT, BOTTOM, TOP = 0, 1, 2, 4, 8
xmin, xmax, ymin, ymax = 0, 3, 0, 3

def code(x, y):
    c = INSIDE
    if x < xmin:
        c |= LEFT
    elif x > xmax:
        c |= RIGHT
    if y < ymin:
        c |= BOTTOM
    elif y > ymax:
        c |= TOP
    return c

def clip(x0, y0, x1, y1):
    c0, c1 = code(x0, y0), code(x1, y1)
    while True:
        if c0 | c1 == 0:
            return (x0, y0, x1, y1)
        if c0 & c1:
            return None
        c = c0 or c1
        if c & TOP:
            x = x0 + (x1 - x0) * (ymax - y0) / (y1 - y0)
            y = ymax
        elif c & BOTTOM:
            x = x0 + (x1 - x0) * (ymin - y0) / (y1 - y0)
            y = ymin
        elif c & RIGHT:
            y = y0 + (y1 - y0) * (xmax - x0) / (x1 - x0)
            x = xmax
        else:
            y = y0 + (y1 - y0) * (xmin - x0) / (x1 - x0)
            x = xmin
        if c == c0:
            x0, y0, c0 = x, y, code(x, y)
        else:
            x1, y1, c1 = x, y, code(x, y)

clipped = clip(-1, 1, 4, 1)
assert clipped == (0, 1, 3, 1)
print(f"({clipped[0]:.0f}, {clipped[1]:.0f})-({clipped[2]:.0f}, {clipped[3]:.0f})")

Where it is used

Cohen-Sutherland clips a segment to a viewport with outcodes, which is the reject test in 2D graphics and CAD. A segment whose codes are both outside the same edge is discarded without an intersection. Liang-Barsky solves the same clip with a different test.


When you can drop this algorithm

Drop the outcodes for a box the segment is already known to lie inside. Keep them when many segments must be rejected quickly against a screen or a viewport, and use AND for the trivial reject.


Why this stays published

We publish this so a search for Cohen-Sutherland returns the four-bit outcode and the AND test that rejects a segment lying entirely to one side. Cohen-Sutherland clips a segment to an axis-aligned rectangle by outcodes, accepting when both codes are zero and rejecting when their bitwise AND is nonzero.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *