fix: remove self-intersecting loops from polygon offset

Polygon offset at concave corners creates geometry that folds back
through itself. Added RemoveSelfIntersections() to Polygon that
detects non-adjacent edge crossings and removes the smaller loop
at each crossing. Applied to both collision detection and rendering.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-06 22:23:44 -05:00
parent 08b31d0797
commit 4d270ae68e
3 changed files with 128 additions and 2 deletions

View File

@@ -459,9 +459,20 @@ namespace OpenNest.Controls
if (offsetEntity == null)
continue;
offsetEntity.Offset(part.Location);
var polygon = offsetEntity.ToPolygonWithTolerance(0.01);
polygon.RemoveSelfIntersections();
polygon.Offset(part.Location);
var path = GraphicsHelper.GetGraphicsPath(offsetEntity);
if (polygon.Vertices.Count < 2)
continue;
var pts = new PointF[polygon.Vertices.Count];
for (int j = 0; j < pts.Length; j++)
pts[j] = new PointF((float)polygon.Vertices[j].X, (float)polygon.Vertices[j].Y);
var path = new GraphicsPath();
path.AddLines(pts);
path.Transform(Matrix);
g.DrawPath(offsetPen, path);
path.Dispose();