Sutherland–Hodgman algorithm: Difference between revisions

Content deleted Content added
mNo edit summary
Tags: Visual edit Mobile edit Mobile web edit Advanced mobile edit
Line 14:
Given a list of edges in a clip polygon, and a list of vertices in a subject polygon, the following procedure clips the subject polygon against the clip polygon.
 
List outputList = subjectPolygon;
'''for''' (Edge clipEdge in clipPolygon) '''do'''
List inputList = outputList;
outputList.clear();
'''for''' (int i = 0 ; i < inputList.count ; i += 1) '''do'''
Point current_point = inputList[i];
Point prev_point = inputList[(i + inputList.count - 1) % inputList.count];
Point Intersecting_point = ComputeIntersection(prev_point, current_point, clipEdge)
'''if''' (current_point inside clipEdge) '''then'''
'''if''' (prev_point not inside clipEdge) '''then'''
outputList.add(Intersecting_point);
'''end if'''
outputList.add(current_point);
'''else if''' (prev_point inside clipEdge) '''then'''
outputList.add(Intersecting_point);
'''end if'''
'''done'''
'''done'''
 
The vertices of the clipped polygon are to be found in ''outputList'' when the algorithm terminates. Note that a point is defined as being ''inside'' an edge if it lies on the same side of the edge as the remainder of the polygon. If the vertices of the clip polygon are consistently listed in a counter-clockwise direction, then this is equivalent to testing whether the point lies to the left of the line (left means ''inside'', while right means ''outside''), and can be implemented simply by using a [[cross product]].