Add isPointInPolygon utility function for polygon containment checks

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 <david.nguyen@goutezplanb.com>
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
David Nguyen 2025-11-04 11:16:08 -05:00
parent 5205ebff6c
commit 824b41656a
Signed by: david.nguyen
GPG Key ID: D5FB5A5715829326

View File

@ -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;