Point in Poly?

BlitzMax Forums/BlitzMax Programming/Point in Poly?

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Below is my implementation of the Winding Number method. I ripped this out of my SA_GameMod libraries, so it used to be a method of my one of my GameMod types and it requires a simple struct-style "Point" which I will also post below.

  ' Function arguments: 
  ' _x = Mouse X position
  ' _y = Mouse Y position
  ' Points = An array of SA_GameObject_Point objects
  ' Returns:
  ' 0 if point _x, _y is outside polygon
  ' 1 if point _x, _y is inside polygon
  Function IsIn%(_x#, _y#, Points:SA_GameObject_Point[])
    Local xs% = 0

    For Local i% = 0 To Points.Length-2
    	If (( (Points[i].y <= _y) And (Points[i+1].y > _y) ) Or ( (Points[i].y > _y) And (Points[i+1].y <= _y) ))
    		Local sect# = Float((_y - Points[i].y) / (Points[i+1].y - Points[i].y))
    		If (_x < Points[i].x + sect*(Points[i+1].x - Points[i].x)) xs :+ 1
    	End If
    Next
    Return xs&1
  End Method


The Point type:
' ========================================
' SA_GameObject_Point - A "point" on the
' drawing surface or board defined by X, Y.
' Mainly used to define polygons.
' ========================================

Type SA_GameObject_Point
	Field x#, y#
	
       ' Creates and returns an SA_GameObject_Point
       ' of _x, _y
	Function Create:SA_GameObject_Point(_x#, _y#)
	  Local p:SA_GameObject_Point = new SA_GameObject_Point
		p.x = _x
		p.y = _y
		Return p
	End Function
End Type


Like I said, I ripped this from my SA_GameMod files and made a few quick edits to make it stand-alone but I haven't tested it as stand-alone. It works perfectly in my game mod, though.

SpaceAce