Encode which side of a clipping rectangle each segment endpoint lies on.

cohen-sutherland-outcodes An outcode records top, bottom, right, and left tests as bits, making trivial accept, trivial reject, and clipping cases structural.

Example

Encode which side of the viewport each endpoint lies on.

highlighted = computed this step

Step 1 — Set up

Set up the exact input values.

rect and segment({’xmin’: Fraction(1, 1), ’xmax’: Fraction(5, 1), ’ymin’: Fraction(1, 1), ’ymax’: Fraction(4, 1)}, (0, 5), (6, 2))\begin{array}{c|c}\text{rect and segment}&\text{(\{'xmin': Fraction(1, 1), 'xmax': Fraction(5, 1), 'ymin': Fraction(1, 1), 'ymax': Fraction(4, 1)\}, (0, 5), (6, 2))}\end{array}
Line clippingA segment is clipped against the rectangular viewport.AB

Step 2 — Endpoint A

Compute the highlighted value.

A outcode1001\begin{array}{c|c}\text{A outcode}&\hlmath{\text{1001}}\end{array}
Line clippingA segment is clipped against the rectangular viewport.AB

Step 3 — Endpoint B

Compute the highlighted value.

B outcode0010\begin{array}{c|c}\text{B outcode}&\hlmath{\text{0010}}\end{array}
Line clippingA segment is clipped against the rectangular viewport.AB

Step 4 — Result

State the structural verdict.

OR, AND, verdict(1011, 0000, clip)\begin{array}{c|c}\text{OR, AND, verdict}&\hlmath{\text{(1011, 0000, clip)}}\end{array}
Line clippingA segment is clipped against the rectangular viewport.AB