PolyPartitionSharp 0.1.0
dotnet add package PolyPartitionSharp --version 0.1.0
NuGet\Install-Package PolyPartitionSharp -Version 0.1.0
<PackageReference Include="PolyPartitionSharp" Version="0.1.0" />
<PackageVersion Include="PolyPartitionSharp" Version="0.1.0" />
<PackageReference Include="PolyPartitionSharp" />
paket add PolyPartitionSharp --version 0.1.0
#r "nuget: PolyPartitionSharp, 0.1.0"
#:package PolyPartitionSharp@0.1.0
#addin nuget:?package=PolyPartitionSharp&version=0.1.0
#tool nuget:?package=PolyPartitionSharp&version=0.1.0
PolyPartitionSharp
PolyPartitionSharp is a pure C# port of polypartition by Ivan Fratric: polygon triangulation and convex partitioning for 2D polygons, with or without holes. The algorithms are ported line for line, so the results match the upstream library (the upstream test fixtures are part of the test suite).
Targets netstandard2.0, netstandard2.1, net8.0, and net10.0.
Installation
dotnet add package PolyPartitionSharp
Usage
using System.Numerics;
using PolyPartitionSharp;
// Non-hole polygons must be counter-clockwise, holes clockwise.
var outer = new Polygon([new Vector2(0, 0), new Vector2(4, 0), new Vector2(4, 4), new Vector2(0, 4)]);
var hole = new Polygon([new Vector2(1, 1), new Vector2(1, 3), new Vector2(3, 3), new Vector2(3, 1)], isHole: true);
if (PolyPartition.TryTriangulateEC([outer, hole], out List<Polygon> triangles))
{
foreach (Polygon triangle in triangles)
{
Console.WriteLine(string.Join(", ", triangle.Select(p => p.Position)));
}
}
Use Polygon.SetOrientation(PolygonOrientation.CounterClockwise) to fix the winding of an input.
Each PolygonPoint carries a user Id that is copied into the output, so output vertices can be
mapped back to the input.
Index buffer
Use vertex buffer indices as point ids, then read the ids back from the triangles. The Vector2
constructor gives every point id 0, so build the polygons from PolygonPoints instead.
Vector2[] vertices = [new(0, 0), new(4, 0), new(4, 4), new(0, 4), new(1, 1), new(1, 3), new(3, 3), new(3, 1)];
var outer = new Polygon(Enumerable.Range(0, 4).Select(i => new PolygonPoint(vertices[i], i)));
var hole = new Polygon(Enumerable.Range(4, 4).Select(i => new PolygonPoint(vertices[i], i)), isHole: true);
if (PolyPartition.TryTriangulateEC([outer, hole], out List<Polygon> triangles))
{
int[] indices = triangles.SelectMany(t => t.Select(p => p.Id)).ToArray();
}
Input requirements
- Every polygon is simple: no self-intersections, no zero-width spikes.
- No repeated closing vertex (GeoJSON and SVG repeat the first point at the end; drop it).
- No consecutive duplicate vertices.
- Finite coordinates (no NaN or Infinity).
- Non-hole polygons counter-clockwise, holes clockwise, and every hole strictly inside its outer polygon (not touching its edges or vertices).
Other input is not rejected up front. Depending on the algorithm it returns false, or true with
zero-area or clockwise triangles, non-convex parts, or NaN areas; see the
per-algorithm table.
Algorithms
| Method | Upstream | Holes | Time | Notes |
|---|---|---|---|---|
TryTriangulateEC |
Triangulate_EC |
yes (list overload) | O(n^2) | Ear clipping |
TryTriangulateOPT |
Triangulate_OPT |
no | O(n^3) | Minimal total edge length |
TryTriangulateMONO |
Triangulate_MONO |
yes (list overload) | O(n log n) | Monotone partition, then triangulation |
TryMonotonePartition |
MonotonePartition |
yes | O(n log n) | Monotone pieces |
TryConvexPartitionHM |
ConvexPartition_HM |
yes (list overload) | O(n^2) | Hertel-Mehlhorn, at most 4x optimal |
TryConvexPartitionOPT |
ConvexPartition_OPT |
no | O(n^3) | Optimal number of convex parts (Keil-Snoeyink); rejects bare triangles, as upstream |
TryRemoveHoles |
RemoveHoles |
yes | O(h n^2) | Merges holes into their outer polygon |
Every method returns false where upstream returns 0. Vertex positions are System.Numerics.Vector2;
all geometry is computed in double precision, as upstream does.
With many holes, TryTriangulateMONO is much faster than TryTriangulateEC (about 160x at 400 holes,
since EC and HM remove holes in O(h n^2)), but see limitation B below.
Known limitations (inherited from upstream)
These are upstream behaviors, kept so results stay identical to polypartition. In each case the method
returns true with wrong output:
- A.
TryConvexPartitionOPTcan return non-convex parts (about 5% of random simple polygons; areas still add up). Workaround: check every part for convexity before using it, e.g. as a collider. - B.
TryMonotonePartitionwith holes can return pieces that are neither simple nor monotone (about 7% of random polygons with holes;TryTriangulateMONOthen returnsfalse). Workaround: preferTryTriangulateECfor polygons with holes, or validate the MONO pieces. - C. MONO accepts a counter-clockwise hole and fills it:
TryTriangulateMONOandTryMonotonePartitiontriangulate the hole as solid (EC and HM returnfalse). Workaround: callSetOrientation(PolygonOrientation.Clockwise)on every hole before MONO.
Repro inputs and numbers: dogfooding round 1.
License
MIT. The original polypartition is Copyright (c) 2011-2021 Ivan Fratric and contributors, under the MIT license; see LICENSE.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- System.Memory (>= 4.6.3)
- System.Numerics.Vectors (>= 4.6.1)
-
.NETStandard 2.1
- No dependencies.
-
net10.0
- No dependencies.
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.1.0 | 130 | 11/1/2026 |