Point inside polygon

Monkey Forums/Monkey Code/Point inside polygon

JIM(Posted 2011) [#1]
Here's a little piece of code that could be helpful to many:

Function PointInPoly:Bool(x:Float, y:Float, poly:Float[])
	Local i:Int, j:Int, c:Bool
	Local v1:Bool, v2:Bool, v3:Bool, v4:Bool, v5#, v6#, v7#
	c = False
	Local p_count% = (poly.Length() / 2)
	
	For i = 0 To p_count-1
		j = (i+1) Mod p_count
		v1 = (poly[i*2+1] <= y)
		v2 = (y < poly[j*2+1])
		v3 = (poly[j*2+1] <= y)
		v4 = (y < poly[i*2+1])
		v5 = (poly[j*2]-poly[i*2]) * (y-poly[i*2+1])
		v6 = (poly[j*2+1]-poly[i*2+1])
		If v6 = 0.0 Then v6 = 0.0001
		v7 = poly[i*2]
		If (((v1 And v2) Or (v3 And v4)) And (x < v5 / v6 + v7)) Then c = Not c
	Next
	Return c
End


It checks if a point is inside a polygon.
I did not write the entire function. It's slightly adapted, but I have no idea whatsoever where I found the reference, so credit goes to whoever wrote this initially.

Enjoy :)