Polygon Pick

BlitzMax Forums/BlitzMax Programming/Polygon Pick

banky(Posted 2005) [#1]
Hi all

I have a polygon in 2D with (5, 6, 7...) points, and I need a function that tells me if a point (the mouse cursor) is inside this poly.

Any ideas?

Thanks in advance,
Banky


taxlerendiosk(Posted 2005) [#2]
If you look at the collision stuff in brl.max2d, you may be able to work out how to use the system to collide arbitrary quads rather than just rectangles. Might have to add your own functions into the library though, unfortunately.


TartanTangerine (was Indiepath)(Posted 2005) [#3]
Here is one I made in Blitz3D which we use extensively in GEOM. It's Pretty Simple to convert to BMAX.

x & Y are the mouse co-ords

Function InsidePoly(p.Polygon,x#,y#)
	Local i, j
	Local bInPoly = 0
	Local xt1#,xt2#,yt1#,yt2#
	j = p\VertexCount - 1 
	For i = 0 To p\VertexCount - 1 
		xt1 = p\VertexX[j]
		xt2 = p\VertexX[i]
		yt1 = p\VertexY[j]
		yt2 = p\VertexY[i]
		If x < ( (xt1 - xt2) * (y - yt2) / (yt1 - yt2) + xt2 ) And ((yt2 <= y And y < yt1) Or (yt1 <= y And y < yt2)) Then  bInPoly = 1 - bInPoly
		j = i
	Next

	Return bInPoly

End Function



altitudems(Posted 2005) [#4]
I don't know if this helps but here is a polygon collision routine I made a while back:
http://www.altitude-studios.com/blitzmax/Polygon%20Collision%20&%20Response.zip


banky(Posted 2005) [#5]
Thank you, guys

Indiepath, your function works perfect!!!
Thank you very much, it's just what I need!


TartanTangerine (was Indiepath)(Posted 2005) [#6]
Nice one :D

If you want to make sure it's really fast do a quick distance check to see if you are near the poly, if not then ignore the insidepoly function.