Guest User

Leaflet Polygon Intersection algorithm

a guest
Sep 12th, 2021
34
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //modified from https://stackoverflow.com/questions/31790344/determine-if-a-point-reside-inside-a-leaflet-polygon
  2.  
  3.     function isPointInsideMarker(point, poly) {
  4.         var polyPoints = poly.getLatLngs()[0];
  5.         var x = point.lat, y = point.lng;
  6.  
  7.         var inside = false;
  8.         for (var i = 0, j = polyPoints.length - 1; i < polyPoints.length; j = i++) {
  9.             var xi = polyPoints[i].lat, yi = polyPoints[i].lng;
  10.             var xj = polyPoints[j].lat, yj = polyPoints[j].lng;
  11.  
  12.             var intersect = ((yi > y) != (yj > y))
  13.                 && (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
  14.             if (intersect) inside = !inside;
  15.         }
  16.  
  17.         return inside;
  18.     }
  19.  
RAW Paste Data