Skip to content

Instantly share code, notes, and snippets.

@joehonton
Created April 11, 2021 03:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joehonton/8e20db8cf52d6e545656ee4d59d1c650 to your computer and use it in GitHub Desktop.
Save joehonton/8e20db8cf52d6e545656ee4d59d1c650 to your computer and use it in GitHub Desktop.
function isInsidePolygon(polygon, mouseX, mouseY) {
const c = false;
for (let i=1, j=0; i < polygon.length; i++, j++) {
const ix = polygon[i].x;
const iy = polygon[i].y;
const jx = polygon[j].x;
const jy = polygon[j].y;
const iySide = (iy > mouseY);
const jySide = (jy > mouseY);
if (iySide != jySide) {
const intersectX = (jx-ix) * (mouseY-iy) / (jy-iy) + ix;
if (mouseX < intersectX)
c = !c;
}
}
return c;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment