Polygon Pick
BlitzMax Forums/BlitzMax Programming/Polygon Pick
| ||
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 |
| ||
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. |
| ||
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 |
| ||
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 |
| ||
Thank you, guys Indiepath, your function works perfect!!! Thank you very much, it's just what I need! |
| ||
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. |