Content deleted Content added
Cooperised (talk | contribs) →Pseudo Code: Completely rewritten for consistency with the algorithm description (see talk page) |
Added book publication date |
||
(93 intermediate revisions by 64 users not shown) | |||
Line 1:
{{Short description|Algorithm for clipping polygons}}
The '''Sutherland
==Description==
The algorithm begins with an input [[List (computing)|list]] of all vertices in the subject polygon. Next, one side of the clip polygon is extended infinitely in both directions, and the path of the subject polygon is traversed. Vertices from the input list are inserted into an output list if they lie on the visible side of the extended clip polygon line, and new vertices are added to the output list where the subject polygon path crosses the extended clip polygon line.
This process is repeated iteratively for each clip polygon side, using the output list from one stage as the input list for the next. Once all sides of the clip polygon have been processed, the final generated list of vertices defines a new single polygon that is entirely visible. Note that if the subject polygon was [[concave polygon|concave]] at vertices outside the clipping polygon, the new polygon may have coincident (i.e., overlapping) edges
[[image:Sutherland-
The [[Weiler
==
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 inputList = outputList
for (Point E in inputList) do▼
'''for''' (int i = 0; i < inputList.count; i += 1) '''do'''
if (E inside clipEdge) then▼
if (S not inside clipEdge) then▼
Point prev_point = inputList[(i − 1) % inputList.count];
outputList.add(ComputeIntersection(S,E,clipEdge));▼
end if▼
Point Intersecting_point = ComputeIntersection(prev_point, current_point, clipEdge)
outputList.add(E);▼
'''if''' (current_point inside
done▼
▲ '''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
''ComputeIntersection'' is a
==Implementations==
A Python implementation of the Sutherland-Hodgman can be found [https://github.com/mdabdk/sutherland-hodgman here].
==See also==
*[[Weiler–Atherton clipping algorithm]]
*[[Vatti clipping algorithm]]
On the subject of clipping:
*[[Rasterisation#Clipping|Clipping (in rasterisation)]]
*[[Line clipping|Line clipping algorithms]]
== References==
* Mel Slater, Anthony Steed, Yiorgos Chrysanthou: ''Computer Graphics and Virtual Environments: From Realism to Real-Time.'' Addison Wesley, 2002. {{ISBN|0-201-62420-6}}.
* [[Ivan Sutherland]], Gary W. Hodgman: ''Reentrant Polygon Clipping.'' [[Communications of the ACM]], vol. 17, pp. 32–42, 1974
==External links==
* [http://www.cs.drexel.edu/~david/Classes/CS430/Lectures/L-05_Polygons.6.pdf Polygon clipping and filling] Describes the algorithm using images that are easy to understand.
* [https://rosettacode.org/wiki/Sutherland-Hodgman_polygon_clipping Rosetta Code example]
{{DEFAULTSORT:Sutherland-Hodgman algorithm}}
▲[[Category:Clipping (computer graphics)]]
[[Category:
|