From dd7383467b311405dd88afec937445677bc20f26 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Sat, 7 Mar 2026 18:13:12 -0500 Subject: [PATCH] feat: add Polygon.ContainsPoint using ray casting Co-Authored-By: Claude Opus 4.6 --- OpenNest.Core/Geometry/Polygon.cs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/OpenNest.Core/Geometry/Polygon.cs b/OpenNest.Core/Geometry/Polygon.cs index c6b8d4b..8aa8ac2 100644 --- a/OpenNest.Core/Geometry/Polygon.cs +++ b/OpenNest.Core/Geometry/Polygon.cs @@ -608,5 +608,29 @@ namespace OpenNest.Geometry var hull = ConvexHull.Compute(Vertices); return RotatingCalipers.MinimumBoundingRectangle(hull, startAngle, endAngle); } + + public bool ContainsPoint(Vector pt) + { + var n = IsClosed() ? Vertices.Count - 1 : Vertices.Count; + + if (n < 3) + return false; + + var inside = false; + + for (int i = 0, j = n - 1; i < n; j = i++) + { + var vi = Vertices[i]; + var vj = Vertices[j]; + + if ((vi.Y > pt.Y) != (vj.Y > pt.Y) && + pt.X < (vj.X - vi.X) * (pt.Y - vi.Y) / (vj.Y - vi.Y) + vi.X) + { + inside = !inside; + } + } + + return inside; + } } }