function inpoly(px,py,edges) i = 0 for k=1:size(edges)[1] p1x = edges[k,1] p1y = edges[k,2] p2x = edges[k,3] p2y = edges[k,4] if (p1y < py) & (p2y < py) continue end
if (p1y >= py) & (p2y >= py) continue end d = p2y - p1y s = (py-p1y)*(p2x-p1x) if d != 0 sx = (s / d)+p1x if sx >= px i = i +1 end end end return mod(i,2) end tests a single point (px,py) against a closed polygon in edges = [x1 y1 x2 y2; x2 y2 x3 y3; x3 y3 x1 y1], the first point repeated as last point.