# Cohen-Sutherland Algorithm for line clipping

``` type vertex = point; {point hold real x,y} edge = array[1..2] of vertex; vertexArray = array[1..MAX] of vertex; {MAX is a declared constant} procedure SutherlandHodgmanPolygoClip ( inVertexArray : vertexArray; {Input vertex array} var outVertexArray : vertexArray; {Output vertex array} inLength : integer; {Number of entries in inVertexArray} var outLength : integer; {Number of entries in outVertexArray} clipBoundary : edge); {Edge of clip polygon} var s,p, {Start, end point of current polygon edge} i : vertex; {Intersection point with a clip boundary} j : integer; {Vertex loop counter} procedure Output( newVertex : vertex; var outLength : integer; var outVertexArray : vertexArray); {Adds newVertex to outVertexArray and then updates outLength } begin ... end; function Inside(testVertex : vertex; clipBoundary : edge):boolean; {Checks whether the vertex lies inside the clip edge or not} begin ... end; procedure Intersect(first,second:vertex; clipBoundary:edge; var intersectPt:vertex); {Clips polygon edge (first,second) against clipBoundary, outputs the new point} begin ... end; begin outLength := 0; s := inVertexArray[inLength]; {Start with the last vertex in inVertexArray} for j := 1 to inLength do begin p := inVertexArray[j]; {Now s and p correspond to the vertices in Fig. 3.48} if Inside(p,clipBoundary) then {Cases 1 and 4} if Inside(s, clipBoundary) then Output(p, outLength, outVertexArray) {Case 1} else begin Intersect(s, p, clipBoundary, i); Output(i, outLength, outVertexArray); Output(p, outLength, outVertexArray) end else {Cases 2 and 3} if Inside(s, clipBoundary) then {Cases 2} begin Intersect(s, p, clipBoundary, i); Output(i, outLength, outVertexArray) end; {No action for case 3} s := p {Advance to next pair of vertices} end {for} end; {SutherlandHodgmanPolygonClip} ```

Go back to Clipping polygons

Contact:
Haowei Hsieh
hhsieh@cc.gatech.edu
http://www.cc.gatech.edu/gvu/people/Masters/Haowei.Hsieh.html
Last Change : Feb 19,1995