CS 460 Computer Graphics

Clipping

OutcodesClipping must account for all of the viewport's boundaries. The Cohen-Sutherland Method is used to clip lines to a viewport. An outcode is determined by the position of an endpoint and is a 4-bit value indicating whether it is outside of the viewport to the top (MSB), bottom, right, or left (LSB) side(s).
The algorithm can be summarized as follows:

  1. Compute outcode for point P1
  2. Compute outcode for point P0
  3. Trivial accept? (both endpoints inside of viewport)
    Yes -- draw the line
  4. Trivial reject? (both endpoints outside and on the same side of one of the viewport edges)
    Yes -- do not draw and exit
  5. Is P0 inside the viewport?
    Yes -- swap P0 <--> P1
  6. Is P0 > Ymax (top) ?
    Yes -- clip at intersection and move P0 to new point
  7. Is P0 < Ymin (bottom) ?
    Yes -- clip at intersection and move P0 to new point
  8. Is P0 > Xmax (right) ?
    Yes -- clip at intersection and move P0 to new point
  9. Is P0 < Xmin (left) ?
    Yes -- clip at intersection and move P0 to new point
  10. Branch back to Step 2

The worst case will iterate four times through the algorithm.

Clipping objects other than straight lines is rather difficult and/or computationally expensive. Clipping polygons involves ownership and adjacency problems as well as dealing with gaps formed when mesh surfaces are created and rotated.

G. Hill Price