JS- Vérifiez si Point Inside A Polygon

Je veux vérifier si un point appartient à un polygone spécifique, j'ai une polygone écrite de cette façon

polygone= [ [-73.89632720118, 40.8515320489962], [-73.8964878416508, 40.8512476593594], [-73.8968799791431, 40.851375925454], [-73.8967188588015, 40.851660158514], [-73.89632720118, 40.8515320489962] ] 

Ces points d'entrée =

 [40.8515320489962,-73.89632720118,40.8512476593594,-73.8964878416508,40.851375925454,-73.8968799791431,40.851660158514,-73.8967188588015,40.8515320489962,-73.89632720118] 

Je veux vérifier si un point appartient à cette polygone ou non. Comment puis je faire ça :

Il s'agit d'algorithmes qui ne se déroulent pas: je ne sais pas pourquoi. Pt [lat, long]

  function isPointInPoly(poly, pt){ for(var c = false, i = -1, l = poly.length, j = l - 1; ++i < l; j = i) ((poly[i][1] <= pt[1] && pt[1] < poly[j][1]) || (poly[j][1] <= pt[1] && pt[1] < poly[i].y)) && (pt[0] < (poly[j][0] - poly[i][0]) * (pt[1] - poly[i][1]) / (poly[j][1] - poly[i][1]) + poly[i][0]) && (c = !c); return c; } 

Je ne veux pas utiliser de solutions tierces (comme google maps api) comme celle-ci: https://github.com/mattwilliamson/Google-Maps-Point-in-Polygon

Vous pouvez vérifier le code LIVE: http://jsfiddle.net/nvNNF/2/

Il existe un projet sur Github avec le code: https://github.com/substack/point-in-polygon (licence MIT):

 function inside(point, vs) { // ray-casting algorithm based on // http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html var x = point[0], y = point[1]; var inside = false; for (var i = 0, j = vs.length - 1; i < vs.length; j = i++) { var xi = vs[i][0], yi = vs[i][1]; var xj = vs[j][0], yj = vs[j][1]; var intersect = ((yi > y) != (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi); if (intersect) inside = !inside; } return inside; }; 

Usage:

 // array of coordinates of each vertex of the polygon var polygon = [ [ 1, 1 ], [ 1, 2 ], [ 2, 2 ], [ 2, 1 ] ]; inside([ 1.5, 1.5 ], polygon); // true 

La fonction de test est ici: https://github.com/substack/point-in-polygon/blob/master/index.js

Voici la fonction que j'ai finalement travaillé. Je l'ai obtenu en adoptant le code C vers javascript à partir d'ici (avec explication) le lien

  function checkcheck (x, y, cornersX, cornersY) { var i, j=cornersX.length-1 ; var oddNodes=false; var polyX = cornersX; var polyY = cornersY; for (i=0; i<cornersX.length; i++) { if ((polyY[i]< y && polyY[j]>=y || polyY[j]< y && polyY[i]>=y) && (polyX[i]<=x || polyX[j]<=x)) { oddNodes^=(polyX[i]+(y-polyY[i])/(polyY[j]-polyY[i])*(polyX[j]-polyX[i])<x); } j=i; } return oddNodes; } 

Où cornersX = tableau avec x ou tableau de verbes de latitude, cornersY = tableau avec y ou rangée de longitude. X, Y – latitude et longitude du point testé

Ecrivez-moi si vous avez des questions