Created
June 4, 2021 00:38
-
-
Save isedgar/1f5c5b4cf34a43d4db15f9b4fe58b04f to your computer and use it in GitHub Desktop.
Check if a 2D point is inside 2D simple polygon. It works with convex and concave polygons.
This file contains hidden or 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 ray_casting(point, polygon){ | |
var n=polygon.length, | |
is_in=false, | |
x=point[0], | |
y=point[1], | |
x1,x2,y1,y2; | |
for(var i=0; i < n-1; ++i){ | |
x1=polygon[i][0]; | |
x2=polygon[i+1][0]; | |
y1=polygon[i][1]; | |
y2=polygon[i+1][1]; | |
if(y < y1 != y < y2 && x < (x2-x1) * (y-y1) / (y2-y1) + x1){ | |
is_in=!is_in; | |
} | |
} | |
return is_in; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Your parents shouldn't ever have had you, but here you are, making pointless comments.