Maths genius needed...

Blitz3D Forums/Blitz3D Programming/Maths genius needed...

I pinched a function from the code archives to help define a shape made with points. If I were to make another point (not part of the shape), how could I tell if it was inside or outside of the shape?

Graphics3D 640,480,0,2
SetBuffer BackBuffer()
SeedRnd MilliSecs()
HidePointer

Global Camera	= CreateCamera()
Global Light	= CreateLight()
Global Plane	= CreatePlane()
Global PlaneTex	= CreateTexture (64,64)

Color 0,0,100
Rect 0,0,64,64
Color 0,0,150
Rect 0,0,32,32
Rect 32,32,32,32
CopyRect 0,0,64,64,0,0,BackBuffer(),TextureBuffer(PlaneTex)

ScaleTexture PlaneTex,5,5
EntityTexture Plane,PlaneTex
MoveEntity Plane,0,0,40
RotateEntity Plane,-90,0,0
EntityPickMode Plane,2

Type Cube

	Field X#,Y#,Z#,Mesh

End Type

Local CE.Cube,CE2.Cube

While Not KeyDown (1)

	If MouseHit (1)

		CameraPick (Camera,MouseX(),MouseY())
		
		CE.Cube = New Cube
		CE\Mesh	= CreateCube()
		CE\X	= PickedX()
		CE\Y	= PickedY()
		CE\Z	= PickedZ()
		
		PositionEntity CE\Mesh,CE\X,CE\Y,CE\Z
	
	EndIf
	
	UpdateWorld
	RenderWorld

	Color 0,255,0

	For CE.Cube = Each Cube
	
		If CE = Last Cube
		
			CE2 = First Cube
		
		Else
		
			CE2 = After CE
		
		End If
		
		Color 0,255,0
		
		ProjectLine (Camera,CE2,CE)
	
	Next
	
	Color 255,0,0

	Line MouseX()-10,MouseY(),MouseX()+10,MouseY()
	Line MouseX(),MouseY()-10,MouseX(),MouseY()+10
	
	Flip
	
Wend
End

Function ProjectLine (Camera,CE.Cube,CE2.Cube)

Local X1#,Y1#,X2#,Y2#

CameraProject Camera,CE\X,CE\Y,CE\Z

X1 = ProjectedX()
Y1 = ProjectedY()

CameraProject Camera,CE2\X,CE2\Y,CE2\Z

X2 = ProjectedX()
Y2 = ProjectedY()

Line X1,Y1,X2,Y2
	
End Function


Here's some code to check if a point is inside a polygon. This is based on BlitzMax question.


; This is adapted from a BlitzMax programming thread about determining if a point
; is inside a polygon, not necessarily convex. Thread began on May 3, 2007.
; I was interested in what happened to points on edges. The result was this test.
; The algorithm works even for this self-intersecting quadrilateral.
; Note that some edgepoints are 'inside' while others are not.

Graphics 600,500,0, 2

SetBuffer FrontBuffer()
HidePointer 

Global xp#[100], yp#[100]
Global npol

AddPoint 100, 100
AddPoint 400, 400
AddPoint 400, 100
AddPoint 100, 400

DrawPoly
Delay 1500

Color 80, 80, 0

For y = 100 To 400
	For x = 100 To 400
		If CheckPoint( x, y ) Then Plot x,y
	Next
Next

WaitKey

Function AddPoint( x#, y# )
	xp[npol] = x
	yp[npol] = y
	npol = npol + 1
End Function

Function DrawPoly()
	For n = 1 To npol-1
		Line xp[n-1], yp[n-1],  xp[n], yp[n]
	Next
	Line xp[npol-1], yp[npol-1],  xp[0], yp[0]
End Function

Function CheckPoint(x#, y#)
	Local i, j, c
	j=npol-1
	For i=0 To npol-1
		Local ypi#= yp[i]
		Local xpi#= xp[i]
		Local ypj#= yp[j]
		Local xpj#= xp[j]
		If ( (ypi<=y) And (y<ypj) ) Or ( (ypj<=y) And (y<ypi) )
			If x < (xpj - xpi) * (y-ypi) / (ypj - ypi) + xpi Then c = Not c
		End If
		j = i
	Next
	Return c		
End Function


That's great!! Thank you very much :)