From 824b41656a269925587d1a8e43b43147d22dbadc Mon Sep 17 00:00:00 2001 From: David Nguyen Date: Tue, 4 Nov 2025 11:16:08 -0500 Subject: [PATCH] Add isPointInPolygon utility function for polygon containment checks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Implements ray-casting algorithm to determine if a point lies within a polygon boundary. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: David Nguyen Co-Authored-By: Claude --- .../src/lib/adapters/map-adapter.interface.ts | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/projects/ngx-open-map-wrapper/src/lib/adapters/map-adapter.interface.ts b/projects/ngx-open-map-wrapper/src/lib/adapters/map-adapter.interface.ts index 812349d..dbf7427 100644 --- a/projects/ngx-open-map-wrapper/src/lib/adapters/map-adapter.interface.ts +++ b/projects/ngx-open-map-wrapper/src/lib/adapters/map-adapter.interface.ts @@ -24,6 +24,29 @@ export function getLngLat(latLng: LatLng): [number, number] { return [latLng[1], latLng[0]]; } +export function isPointInPolygon(polygon: LatLng[], point: LatLng): boolean { + let intersections = 0; + const x = point[0]; // lat + const y = point[1]; // lng + + for (let i = 0; i < polygon.length; i++) { + const nextIndex = (i + 1) % polygon.length; + const x1 = polygon[i][0]; // lat + const y1 = polygon[i][1]; // lng + const x2 = polygon[nextIndex][0]; // lat + const y2 = polygon[nextIndex][1]; // lng + + if ((y1 > y) !== (y2 > y)) { + const intersectionX = ((x2 - x1) * (y - y1)) / (y2 - y1) + x1; + if (x < intersectionX) { + intersections++; + } + } + } + + return intersections % 2 === 1; +} + export interface RouteLineStyle { color?: string; opacity?: number;