Skip to main content

Posts

Showing posts with the label polygon

Check if Point in Polygon with javascript

function inPoly(poly,px,py) { var npoints = poly.length; // number of points in polygon var xnew,ynew,xold,yold,x1,y1,x2,y2,i; var inside=false; if (npoints/2 < 3) { // points don't describe a polygon return false; } xold=poly[npoints-2]; yold=poly[npoints-1]; for (i=0 ; i < npoints ; i=i+2) { xnew=poly[i]; ynew=poly[i+1]; if (xnew > xold) { x1=xold; x2=xnew; y1=yold; y2=ynew; } else { x1=xnew; x2=xold; y1=ynew; y2=yold; } if ((xnew < px) == (px <= xold) && ((py-y1)*(x2-x1) < (y2-y1)*(px-x1))) { inside=!inside; } xold=xnew; yold=ynew; } return inside; }