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:
parent
5205ebff6c
commit
824b41656a
@ -24,6 +24,29 @@ export function getLngLat(latLng: LatLng): [number, number] {
|
|||||||
return [latLng[1], latLng[0]];
|
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 {
|
export interface RouteLineStyle {
|
||||||
color?: string;
|
color?: string;
|
||||||
opacity?: number;
|
opacity?: number;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user