Created
April 11, 2021 03:29
-
-
Save joehonton/8e20db8cf52d6e545656ee4d59d1c650 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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