Skip to content

Instantly share code, notes, and snippets.

@timknip
Created December 12, 2011 14:49
Show Gist options
  • Save timknip/1467615 to your computer and use it in GitHub Desktop.
Save timknip/1467615 to your computer and use it in GitHub Desktop.
point in polygon
pointInside: function(pt) {
var i, j, a, b, xpi, ypi, xpj, ypj;
var x = pt.x;
var y = pt.y;
var c = false;
for (i = 0; i < this.points.length; i++) {
j = (i+1) % this.points.length;
a = this.points[i];
b = this.points[j];
xpi = a.x;
ypi = a.y;
xpj = b.x;
ypj = b.y;
if ((((ypi<=y) && (y<ypj)) || ((ypj<=y) && (y<ypi))) &&
(x < (xpj-xpi)*(y-ypi)/(ypj-ypi)+xpi)) {
c = !c;
}
}
return c;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment