Sutherland-Hodgman Implementation

type
vertex=array[1..2] of real;
boundary=array[1..2] of vertex;
vertex_array[1..max] of vertex;

This algorithm works by moving around the polygon from Vn to V1 to Vn taking into account the relationship between successive vertices and the clip boundary. For each vertex pair, either zero, one, or two vertices are added to the output list of vertices.

Four possible test cases need to be examined. For each of the following cases, we will assume the polygon edge to be clipped is from vertex S to P.

The Sutherland-Hodgman clipping algorithm has a couple of assumptions:
1) accepts an array in_v of polygon vertices and creates an array out_v
2) procedure OUTPUT(out_v,v,out_length) places vertex v into out_v and updates the number of vertices into out_v
3) function INTERSECT(S,P,clip_boundary) returns the intersection of segment(SP) with clip_boundary
4) function INSIDE(point,clip_boundary) returns true if the point is inside the clip_boundary where inside is defined to be to the right of the clip boundary. More specific, inside is the to right when looking from the first point of the clip boundary to the second.

Remember, function INSIDE uses the cross product of two vectors formed using the point and the clip boundary. If the cross product is along the negative z, the point is to the right and inside; otherwise, the point is to the left and outside.

In general, the cross product of two vectors vec(V) and vec(W) in the xy-plane is just a vector of magnitude along the z-axis, so if vec(V)=[Vx Vy] and vec(W)=[Wx Wy], then vec(V) x vec(W) = [0 0 VxWy-VyWx].

procedure clip_poly(
    in_v  : vertexarray;       {input vertex array}
var out_v : vertexarray;       {output vertex array}
    in_length : integer;       {length of in_v}
var out_length : integer;      {length of out_v}
    clip_boundary : boundary); {clip boundary}

var
i,p,s : vertex;
j : integer;

begin
  out_length := 0;
  s := in_v[in_length];
  for j := 1 to in_length do
    begin
      p := in_v[j];
      if(INSIDE(p,clip_boundary)) then
        if(INSIDE(s,clip_boundary)) then
          OUTPUT(out_v,p,outlength)
        else
          begin
            i := INTERSECT(s,p,clip_boundary);
            OUTPUT(out_v,i,out_length);
            OUTPUT(out_v,p,out_length);
          end
      else
        if(INSIDE(s,clip_boundary) then
          begin
            i := INTERSECT(s,p,clip_boundary);
            OUTPUT(out_v,i,out_length)
          end
      s := p;
    end
end;
Algorithm from Fundamentals of Interactive Computer Graphics

Text Clipping

Remember, characters can be generated in one of two ways:
1) bit mapped character generator
2) vector character generator

There are three strategies for clipping characters:
1) When dealing with a string, either all or none of the string is printed depending on whether all characters of the string are visible.
2)When dealing with a character, either entire character is displayed or not displayed.
3) Only the visible portion of the character is displayed


©1995 Douglas J. Ryan
Douglas J. Ryan/ryand@tardis.pacificu.edu