Class PolygonClipper
- Namespace
- SixLabors.PolygonClipper
- Assembly
- SixLabors.PolygonClipper.dll
Performs boolean operations on polygons.
public class PolygonClipper
- Inheritance
-
PolygonClipper
- Inherited Members
Remarks
This implementation follows the algorithm described in "A Simple Algorithm for Boolean Operations on Polygons" by Francisco Martínez, Carlos Ogayar, Juan R. Jiménez, and Antonio J. Rueda. It supports intersection, union, difference, and symmetric difference (XOR).
It uses a sweep-line with an event queue to process segment intersections and robustly handles special cases, including overlapping edges and trivial non-overlapping inputs.
The static boolean methods are the recommended entry points. They route work through an internal thread-local pool of reusable clipper instances and automatically reset temporary state between calls.
Instance members are not thread-safe for concurrent use.
The high-level workflow has three stages:
- Preprocessing: Handles trivial operations and prepares segments for processing.
- Sweeping: Processes events using a priority queue, handling segment insertions and removals.
- Connecting edges: Constructs the resulting polygon by connecting valid segments.
Constructors
PolygonClipper(Polygon, Polygon, BooleanOperation)
Initializes a new instance of the PolygonClipper class.
public PolygonClipper(Polygon subject, Polygon clip, BooleanOperation operation)
Parameters
subjectPolygonThe polygon used as the left-hand operand.
clipPolygonThe polygon used as the right-hand operand.
operationBooleanOperationThe boolean operation to execute.
Remarks
This constructor is intended for advanced/manual execution flows. For typical usage, prefer the static methods to take advantage of internal pooling and automatic lifecycle management.
Methods
Difference(Polygon, Polygon)
Computes the difference of two polygons (subject minus clip).
public static Polygon Difference(Polygon subject, Polygon clip)
Parameters
subjectPolygonThe polygon used as the left-hand operand.
clipPolygonThe polygon used as the right-hand operand.
Returns
- Polygon
A polygon containing regions from
subjectnot covered byclip.
Remarks
Preferred entry point. Uses internal thread-local reusable instances.
Intersection(Polygon, Polygon)
Computes the intersection of two polygons.
public static Polygon Intersection(Polygon subject, Polygon clip)
Parameters
subjectPolygonThe polygon used as the left-hand operand.
clipPolygonThe polygon used as the right-hand operand.
Returns
- Polygon
A polygon containing regions common to both inputs.
Remarks
Preferred entry point. Uses internal thread-local reusable instances.
Normalize(Polygon)
Normalizes a polygon by resolving self-intersections and overlaps.
public static Polygon Normalize(Polygon polygon)
Parameters
polygonPolygonThe polygon to process.
Returns
- Polygon
A new normalized polygon. Output contours are implicitly closed (no duplicated terminal closing vertex is appended).
Remarks
Preferred entry point. Uses internal thread-local reusable builders.
Run()
Executes the configured boolean operation for the current subject and clipping polygons.
public Polygon Run()
Returns
- Polygon
The operation result.
Remarks
Instance execution is not thread-safe for concurrent use.
Union(Polygon, Polygon)
Computes the union of two polygons.
public static Polygon Union(Polygon subject, Polygon clip)
Parameters
subjectPolygonThe polygon used as the left-hand operand.
clipPolygonThe polygon used as the right-hand operand.
Returns
- Polygon
A polygon containing regions from either input.
Remarks
Preferred entry point. Uses internal thread-local reusable instances.
Xor(Polygon, Polygon)
Computes the symmetric difference (XOR) of two polygons.
public static Polygon Xor(Polygon subject, Polygon clip)
Parameters
subjectPolygonThe polygon used as the left-hand operand.
clipPolygonThe polygon used as the right-hand operand.
Returns
- Polygon
A polygon containing regions that belong to exactly one input.
Remarks
Preferred entry point. Uses internal thread-local reusable instances.