**by William Shoaff with lots of help**

- Contents
- Cohen-Sutherland Line Clipping
- Liang-Barsky Line Clipping
- Blinn's Line Clipping
- Polygon Clipping
- Sutherland-Hodgman Polygon Clipping
- Weiler-Atherton Polygon Clipper
- Bibliography

- 1.
- PDF version of these notes
- 2.
- Audio file of these notes (up to Blinn's algorithm)

Clipping refers to the removal of part of a scene. Internal clipping removes parts of a picture outside a given region; external clipping removes parts inside a region. We'll explore internal clipping, but external clipping can almost always be accomplished as a by-product.

There is also the question of what primitive types can we clip?
We will consider *line* clipping and *polygon* clipping.
A line clipping algorithms takes as input two endpoints of line segment
and returns one (or more) line segments.
A polygon clipper takes as input the vertices of a polygon
and returns one (or more) polygons.
There are several clipping algorithms.
We'll study the Cohen-Sutherland line clipping algorithm
to learn some basic concepts.
Develop the more efficient Liang-Barsky algorithm
and us its insights to culminate with Blinn's line clipping algorithm.
The Sutherland-Hodgman polygon clipping algorithm will then be
covered and the Weiler-Atherton algorithm, time permitting.

There are other issues in clipping that we will not have time to cover. Some of these are:

- Text character clipping
- Scissoring -- clips the primitive during scan conversion to pixels
- Bit (Pixel) block transfers (bitblts/pixblts)
- Copy a 2D array of pixels from a large canvas to a destination window
- Useful for text characters, pulldown menus, etc.

The Cohen-Sutherland algorithm clips a line to an upright rectangular window.
It is an application of *triage*, or make the simple case fast.
The algorithm extended window boundaries to define 9 regions:

top-left, top-center, top-right, center-left, center, center-right, bottom-left, bottom-center, and bottom-right.See figure 1 below. These 9 regions can be uniquely identified using a 4 bit code, often called an

- Left (first) bit is set to 1 when
*p*lies to left of window - Right (second) bit is set to 1 when
*p*lies to right of window - Bottom (third) bit is set to 1 when
*p*lies below window - Top (fourth) bit set is set to 1 when
*p*lies above window

Given a line segment with end points and , here's the basic flow of the Cohen-Sutherland algorithm:

- 1.
- Compute 4-bit outcodes LRBT
_{0}and LRBT_{1}for each end-point - 2.
- If both outcodes are 0000, the
*trivially visible*case, pass end-points to draw routine This occurs when the bitwise OR of outcodes yields 0000. - 3.
- If both outcodes have 1's in the same bit position,
the
*trivially invisible*case, clip the entire line (pass nothing to the draw routine). This occurs when the bitwise AND of outcodes is__not__0000. - 4.
- Otherwise, the
*indeterminate*case, - line may be partially visible or not visible. Analytically compute the intersection of the line with the appropriate window edges

Let's explore the indeterminate case more closely. First, one of two end-points must be outside the window, pretend it is .

- 1.
- Read
*P*_{1}'s 4-bit code in order, say left-to-right - 2.
- When a set bit (1) is found, compute intersection point
*I*of corresponding window edge with line from*p*_{0}to*p*_{1}.

As an example, pretend the right bit is set so we want to compute
the intersection with the right clipping window edge,
also, pretend we've already done the homogeneous divide, so the right edge
is *x*=1, and we need to find *y*.
The *y* value of the intersection is found by substituting
*x*=1 into the line equation (from *p*_{0} to *p*_{1})

and solving for

Other cases are handled similarly.

Now this may not complete the clipping of the line,
so we replace *p*_{0} by the intersection point *I* and repeat Cohen-Sutherland algorithm.
(Clearly we can save some state to avoid some computations)

- Define window by

- End-points and both have 4-bit codes 0000. Logical bitwise OR: line is completely visible - draw line between them.
- End-point has 4-bit code 0101 and end-point has 4-bit code 1001. Logical bitwise AND: both end-points to right of window. Therefore line is invisible.
- End-point
has 4-bit code 0101 and
end-point
has 4-bit code 0000.
- Logical bitwise OR no information.
- Logical bitwise AND no information.
- is outside.
- Reading
*p*_{4}'s 4-bit code from left-to-right, ``right'' bit is set. - Intersection with right edge is at
*I*has 4-bit code

The Cohen-Sutherland was one of, if not, the first clipping algorithm to be implemented in hardware. Yet the intersection was not computed analytically, as we have done, but by bisection (binary search) of the line segment.

The Cohen-Sutherland algorithm extends easily to 3D. Extended the 3D clipping window boundaries to define 27 regions. Assign a 6 bit code to each region, that is, for each point

- Left (first) bit set (1) point lies to left of window
- Right (second) bit set (1) point lies to right of window
- Bottom (third) bit set (1) point lies below window
- Top (fourth) bit set (1) point lies above window
- Near (fifth) bit set (1) point lies to in front of window (near)
- Far (sixth) bit set (1) point lies to behind of window (far)

To clip against a face, say

and use it to evaluate the

The Liang-Barsky is optimized for clipping to an upright rectangular
clip window
(the Cyrus-Beck algorithms is similar but clips to a more general convex polygon).
Liang-Barsky uses parametric equations, clip window edge normals, and inner
products can improve the efficiency of line clipping over Cohen-Sutherland.
Let

denote the parametric equation of the line segment from

Consider the vector
*L*(*t*) - *p*_{e} from *p*_{e} to a point on the line *L*(*t*)Figure 2 shows several of these vectors for different values of *t*.
At the intersection of *L*(*t*) and edge *e* the inner product of
and *L*(*t*)-*p*_{e} is zero, see figure 2.
In fact, we have

which if we Solve for

(Note that checks need to be made that the denominator above is not zero.)

Using the 4 edge normals for an upright rectangular clip window
and 4 points, one on each edge, we can calculate
4 parameter values where *L*(*t*) intersects each edge
Let's call these parameter values *t*_{L}, *t*_{R}, *t*_{B}, *t*_{T}
Note any of the *t*'s outside of the interval
can
be discarded, since they correspond to points before *p*_{0} (when *t* < 0)
and points after *p*_{1} (when *t*>1).
The remaining *t* values are characterized as ``potentially entering''
(PE) or ``potentially leaving'' (PL)

- The parameter
*t*_{i}is PE if when traveling along the (extended) line from*p*_{0}to*p*_{1}we move from the outside to the inside of the window*with respect to the edge*.*i* - The parameter
*t*_{i}is PL if when traveling along the (extended) line from*p*_{0}to*p*_{1}we move from the inside to the outside of the window*with respect to the edge*.*i*

The inner product of the outward pointing edge normal with *p*_{1}-*p*_{0} can be used to classify the parameter *t*_{i}as either PE or PL.

- 1.
- If

the parameter*t*_{i}is potentially entering (PE). The vectors and*p*_{1}-*p*_{0}point in opposite directions. Since is outward, the vector*p*_{1}-*p*_{0}from*p*_{0}to*p*_{1}points inward. - 2.
- If

the parameter*t*_{i}is potentially leaving (PL). The vectors and*p*_{1}-*p*_{0}point in similar directions. Since is outward, the vector*p*_{1}-*p*_{0}from*p*_{0}to*p*_{1}points outward too. - 3.
- Let
*t*_{pe}be the largest PE parameter value and*t*_{pl}the smallest PL parameter value - 4.
- The clipped line extends from
*L*(*t*_{pe}) to*L*(*t*_{pl}), where

A clipping volume can be defined as set of bounding planes.
Choosing simple planes is a good idea. We'll define the
clipping volume by:

We'll call these the

It is convenient to think of these in terms of inner products.
More specifically, the *X*=0 plane can be thought of at the column
vector:

A

To obtain the

(Recall the homogeneous divide necessary to map points after a perspective projection into real points:X=x/w,Y=y/w,Z=z/w.)

Using this notation, the six bounding planes become the six column vectors:

We'll call these the

**Nota bene:**

- 1.
- The first three elements of each column vector form an
*inward*,*unit length normal vector*to the plane in*real*space. - 2.
- When the homogeneous coordinate (
*w*) is equal to 1 and the inner product of a point with the bounding plane column vector is set to 0, the equation reduces to the real space equation. That is, for example,

or

*z*=*z*/1 =*Z*= 1.

- 3.
- Or, better yet, dividing the equation by
*w*produces the real equation, for example*w*-*z*=0 becomes 1-*z*/*w*=1-*Z*=0 or*Z*=1.

Following Blinn's terminology we'll call the inner products of a point
with the bounding planes the *boundary coordinates* of a point.
They are listed in the table below.

Boundary Number |
(Homogeneous Value) |
Real Plane |
---|---|---|

Boundary Coordinate |
||

0 | x |
X=0 |

1 | w-x |
X=1 |

2 | y |
Y=0 |

3 | w-y |
Y=1 |

4 | z |
Z=0 |

5 | w-z |
Z=1 |

The question we want to answer is:

Given two end points (P_{0}andP_{1}) of a line segment, how can we use the boundary coordinates to determine a points Cohen-Sutherland outcode?

Let's assume the 6-bit outcode is in the order left, right, bottom, top, near, far (LRBTNF) so it corresponds to the order of the boundary coordinates. A particular bit is set (to 1) if the point is in the designated region and unset (to 0) otherwise.

We'll assume *w*>0. Recall that the value of *w* that comes out of
the perspective transform is
where
is
(half of) the field of view angle. Now both
and *z*>0(provided we're looking at something in front of us) so *w*>0.
The bottom line is division by a positive quantity does not affect
the sense of an inequality!

Consider the left plane *X*=0.

- if its boundary coordinate
*x*is negative we're in the left region and L is set. - if its boundary coordinate
*x*is greater than or equal to zero, we're not in the left region and L is unset.

- if its boundary coordinate
*w*-*x*<0, then division by*w*yields 1-*X*< 0 and we're in the right region (*X*>1) and R is set. - if its boundary coordinate we're not in the right region and R is unset.

x < 0 |
Set L (to 1) |

w-x < 0 |
Set R |

y < 0 |
Set B |

w-y < 0 |
Set T |

z < 0 |
Set N |

w-z < 0 |
Set F |

For a particular bounding plane (bit in the outcode) and a pair of line segment end points there are four cases.

Bit for P_{0} |
Bit for P_{1} |
Interpretation |
---|---|---|

0 | 0 | Segment visible w.r.t. this boundary |

1 | 0 | Straddles boundary, P_{0} outside |

0 | 1 | Straddles boundary, P_{1} outside |

1 | 1 | Segment invisible w.r.t. this boundary |

Given the outcodes LRBTNF for *P*_{0} and *P*_{1} (set by considering the
boundary coordinates *x*, *w*-*x*, etc.) we can apply the trivial
accept and reject tests:

- 1.
- If
`outcode(`|*P*_{0})`outcode(`== 000000 accept line segment. (All bits in both outcodes must be 0)*P*_{1}) - 2.
- If
`outcode(`&*P*_{0})`outcode(`!= 000000 reject line segment. (Some bit is set in both outcodes for some plane)*P*_{1})

When the trivial tests fail we must calculate the intersections
and this is best done by parametric linear interpolation,
our old friend. Let

be the (directed) line segment from

and since we want the point

but these inner products are just the boundary coordinates! To be explicit,

and let's assume the line straddles the

and the inner product is

which if we set to zero and solve for

Note this value of *u* will be in the range 0 to 1 only when
boundary coordinates (*w*_{0}-*x*_{0} and *w*_{1}-*x*_{1} in the above example)
have opposite signs. This is easy to interpret in real space
(see figure 4). But you can also establish it algebraically
by considering the inequality

If

Blinn's clipping algorithm Jim Blinn presents this material better than I can. See [2] and, in particular, [1].

Polygon clipping differs from line clipping in several respects.

- 1.
- The input to the clipper is a polygon, which for simplicity we will view as a list of vertices .
- 2.
- The output from the clipper is one or more polygons.
- 3.
- The clipping process may generate vertices that do not lie on any of the edges of the orginal polygon.
- 4.
- Complex polygons (that is, non-convex) may lead to strange artifacts.

Since polygons are basic primitives, algorithms have been developed
for clipping them directly.
The Sutherland-Hodgman algorithm is a polygon clipper.
It was a basic component in James Clark's ``Geometry Engine,'' which was the precursor
to the first Silicon Graphics
machines.
This algorithm clips any *subject polygon* (convex or concave) against
any *convex clipping window*, but we will usually pretend the clipping
window is an upright rectangle.

Given a *subject* polygon
with an ordered sequence of vertices

Sutherland-Hodgman compares each subject polygon edge against a single clip window edge, saving the vertices on the in-side of the edge and the intersection points when edges are crossed. The clipper is then re-entered with this intermediate polygon and another clip window edge.

Given a clip window edge and a subject polygon edge, there are four cases to consider:

- 1.
- The subject polygon edge goes from outside clip window edge to outside clip window edge. In this case we output nothing.
- 2.
- The subject polygon edge goes from outside clip window edge to inside clip window edge. In this case we save intersection and inside vertex.
- 3.
- The subject polygon edge goes from inside clip window edge to outside clip window edge. In this case we save intersection point.
- 4.
- The subject polygon edge goes from inside clip window edge to inside clip window edge. In this case we save second inside point (the first was saved previously).

Figure 5 shows an example of the Sutherland-Hodgman clipping process. The clip window edge currently be used is solid, the others are dashed.

**First Clip Window Edge:**- Starting with the original triangle (subject
polygon), we set the intermdiate polygon to
`null`and find- 1.
- Start vertex:
*p*_{0}is outside the edge, so not saved in the intermediate polygon vertex list. - 2.
- Subject edge
*p*_{0}*p*_{1}crosses the clip edge and so the intersection*i*_{01}is saved -- intermediate list (*i*_{01}). - 3.
*p*_{1}is inside the edge, so it is saved -- intermediate list .- 4.
- Subject edge
*p*_{1}*p*_{2}does not crosses the clip edge and so no intersection is computed. - 5.
*p*_{2}is inside the edge, so it is saved -- intermediate list .- 6.
- Last edge
*p*_{2}*p*_{0}: We have output some data, so we'll continue.- (a)
- Subject edge
*p*_{2}*p*_{0}does crosses the clip edge and so the intersection*i*_{01}is saved -- intermediate list .

**Second Clip Window Edge:**- Start vertex:
*i*_{01}is inside the edge, so saved in the intermediate polygon vertex list -- intermediate list (*i*_{01}). - Subject edge
*i*_{01}*p*_{1}crosses the clip edge and so the intersection*i*_{011}is saved -- intermediate list . *p*_{1}is outside the edge, so it is not saved.- Subject edge
*p*_{1}*p*_{2}does crosses the clip edge and so the intersection*i*_{12}is saved -- intermediate list . *p*_{2}is inside the edge, so it is saved -- intermediate list .- Subject edge
*p*_{2}*i*_{20}does not cross the clip edge and so no interesection is computed. *i*_{20}is inside the edge, so it is saved -- intermediate list .- Last edge
*i*_{20}*i*_{01}: We have output some data, so we'll continue.- 1.
- Subject edge
*i*_{20}*i*_{01}does not crosses the clip edge and so no intersection is saved.

The Sutherland-Hodgman algorithm is not too difficult to code either.

The Sutherland-Hodgman algorithm depends on our ability to determine
that a point (vertex) is inside or outside of a given edge (line).
This is a fairly common decision problem in computer graphics,
yet due to the build up of floating point arithmetic errors,
it may be difficult to answer the question exactly.
We will declare that vertices that lie on the edge are *inside*.

There are several ways to answer the question: Is point
on the
*in* or *out* side of a line determined by vertices
and
.
The one we present is based on inner products,
but before we can begin, we must know what is meant by
*inside* and *outside*.
A complete description of this topic and related one
is included in the chapter on basic concepts.
Recall, that our polygon vertices are listed in counter-clockwise
when viewed from the front side, and this implies that the
inside of the polygon is on our left as we traverse its vertices.

These assumption lead to the mathematical result that the outward edge normal
from vertex *v*_{0} to *v*_{1} is given by

Now, consider the vector

from

- 1.
- positive,
*p*is on the*out*side. - 2.
- zero,
*p*is on the edge and consider*inside*. - 3.
- negative,
*p*is on the*in*side.

We can use the inside/outside decision algorithm to determine whether or not two edges cross.

The last task we must complete is developing the code to compute an intersection.

The Sutherland-Hodgman polygon clipping algorithm clips polygons against convex clipping windows. It does so by clipping the subject polygon against each clip edge producing intermediate subject polygons. Although we have not done so, the Sutherland-Hodgman algorithm easily extends to 3 dimensions.

The Sutherland-Hodgman may produce connecting lines that were not in the original polygon. When the subject polygon is concave (not convex) these connecting lines may be undesirable artifacts.

The Weiler-Atherton, which we will consider next, clips arbitrary polygons against arbitrary clipping windows, the price we pay for this generally is that Weiler-Atherton is only a 2D clipper.

- Assume the vertices of the subject polygon are listed in clockwise order (interior is on the right)
- Start at an entering intersection
- Follow the edge(s) of the polygon being clipped until an exiting intersection is encountered
- Turn right at the exiting intersection and following clip window edge until intersection is found
- Turn right and follow the subject polygon
- Continue until vertex already visited is reached
- If entire polygon has not be processed, repeat

- Consider the subject polygon with vertices and the clip polygon with vertices
- Insert the intersections in both vertex lists
- Subject list:
- Clip list:

- Starting at vertex
*a*of the clip polygon, find 1 is first entering intersection - Traversing the subject, find 2 is exiting intersection
- ``Jump'' to vertex 2 in clip polygon, follow until vertex 1 (which has be visited)
- Output clipped list

- Jump back to subject list, restarting at
*c*, find 3 is entering intersection - Traversing the subject, find 4 is exiting intersection
- Jump to vertex 4 in clip polygon, follow until vertex 5 (which is entering)
- Jump to subject, at vertex 5, find 6 is exiting
- Jump to clip, at vertex 6, find 3 is visited
- Output clipped list
- All entering intersections have been visited

**1**-
J. BLINN,
*A trip down the graphics pipeline: Line clipping*, IEEE Computer Graphics and Applications, 11 (1991), pp. 98 - 105. **2**-
height 2pt depth -1.6pt width 23pt,
*Jim Blinn's Corner: a trip down the graphics pipeline*, Morgan Kaufmann Publishers, Inc., 1996.

1-55860-387-5.

2002-03-12