Skip to content

Commit a5a8774

Browse files
committed
Rough fix for issue96
1 parent ffdb3a5 commit a5a8774

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

src/PolygonClipper/PolygonClipper.cs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,27 @@ public PolygonClipper(Polygon subject, Polygon clip, BooleanOperation operation)
5959
public static Polygon Intersection(Polygon subject, Polygon clip)
6060
{
6161
PolygonClipper clipper = new(subject, clip, BooleanOperation.Intersection);
62-
return clipper.Run();
62+
Polygon a = clipper.Run();
63+
64+
Polygon polygons = new();
65+
66+
67+
for (int i = 0; i < a.ContourCount; i++) {
68+
Contour contour = a[i];
69+
if (contour.IsExternal) {
70+
// The exterior ring goes first
71+
polygons.Push(contour);
72+
// Followed by holes if any
73+
for (int j = 0; j < contour.HoleCount; j++) {
74+
int holeId = contour.GetHoleIndex(j);
75+
polygons.Push(a[holeId]);
76+
}
77+
// polygons.Push(contour);
78+
}
79+
}
80+
81+
82+
return polygons;
6383
}
6484

6585
/// <summary>
@@ -140,7 +160,12 @@ public Polygon Run()
140160
for (int i = 0; i < clipping.ContourCount; i++)
141161
{
142162
Contour contour = clipping[i];
143-
contourId++;
163+
bool exterior = operation != BooleanOperation.Difference;
164+
if (exterior)
165+
{
166+
contourId++;
167+
}
168+
144169
for (int j = 0; j < contour.VertexCount - 1; j++)
145170
{
146171
ProcessSegment(contourId, contour.Segment(j), PolygonType.Clipping, eventQueue, ref min, ref max);
@@ -243,7 +268,7 @@ public Polygon Run()
243268
/// <param name="operation">The boolean operation being performed.</param>
244269
/// <param name="result">The resulting polygon if the operation is trivial.</param>
245270
/// <returns>
246-
/// <see langword="true"/> if the operation results in a trivial case due to zero contours;
271+
/// <see langword="true"/> if the operation results in a trivial case due to zero contours;
247272
/// otherwise, <see langword="false"/>.
248273
/// </returns>
249274
private static bool TryTrivialOperationForEmptyPolygons(

0 commit comments

Comments
 (0)