Is there a func. to find if a point is in a rect?

BlitzMax Forums/BlitzMax Beginners Area/Is there a func. to find if a point is in a rect?

Angel: That indeed is an idea ... inline ... ;-)

Result: MingW 5 just is too optimized *hehe*


Strict
Import "inrect.c"
Extern
	Function PointInRect:Int(x:Int,y:Int,bx:Int,by:Int,bw:Int,bh:Int)
	Function DetectPointInsideRect:Int(iPointX:Int,iPointY:Int,iXPos1:Int,iYPos1:Int,iXPos2:Int,iYPos2:Int)
End Extern

Function DetectPointInsideRect1(iPointX:Int,iPointY:Int,iXPos1:Int,iYPos1:Int,iXPos2:Int,iYPos2:Int)
	Return ((((iPointX-iXPos1) ~ (iPointX-iXPos2)) & ((iPointY-iYPos1) ~ (iPointY-iYPos2))) & $80000000)
End Function 


Function PointInRect1:Int(x:Int,y:Int,bx:Int,by:Int,bw:Int,bh:Int)

	Return (x < (bx + bw)) & (y < (by + bh)) & (x > bx) & (y > by)

End Function




Local x0:Int = 100
Local y0:Int = 100
Local w0:Int = 200
Local h0:Int = 150


Local x1:Int = x0+w0
Local y1:Int = y0+h0
Local w1:Int = 100
Local h1:Int = 100

Local sx:Int = 1
Local sy:Int = 1

Local x2:Int = Rand(0,639)
Local y2:Int = Rand(0,479)

Local t1:Int = MilliSecs()
For Local i = 1 To 10000000
	x2 = Rand(0,639)
	y2 = Rand(0,479)
	PointInRect1(x2,y2,x0,y0,w0,h0)
Next

t1	= MilliSecs() - t1

Local t2:Int = MilliSecs()
For Local i = 1 To 10000000
	x2 = Rand(0,639)
	y2 = Rand(0,479)
	PointInRect(x2,y2,x0,y0,w0,h0)
Next

t2	= MilliSecs() - t2


Local t3:Int = MilliSecs()
For Local i = 1 To 10000000
	x2 = Rand(0,639)
	y2 = Rand(0,479)
	DetectPointInsideRect(x2,y2,x0,y0,x1,y1)
Next

t3	= MilliSecs() - t3

Local t4:Int = MilliSecs()
For Local i = 1 To 10000000
	x2 = Rand(0,639)
	y2 = Rand(0,479)
	DetectPointInsideRect1(x2,y2,x0,y0,x1,y1)
Next

t4	= MilliSecs() - t4

Print "Fredborg BM: " + t1
Print "Fredborg Inline: " + t1
Print "Shagwana Inline: " + t3
Print "Shagwana BM: " + t4


with inrect.c
inline int DetectPointInsideRect(int iPointX,int iPointY,int iXPos1,int iYPos1,int iXPos2,int iYPos2)
{
	return ((((iPointX-iXPos1) ^ (iPointX-iXPos2)) & ((iPointY-iYPos1) ^ (iPointY-iYPos2))) & 0x80000000);
}

inline int PointInRect(int x, int y, int bx, int by, int bw, int bh)
{
	return (x < (bx + bw)) & (y < (by + bh)) & (x > bx) & (y > by);
}


Gives the results:

Fredborg BM: 4103
Fredborg Inline: 4103
Shagwana Inline: 4070
Shagwana BM: 4081

This means around 0.0004081 ms per function call.
Don't know, how fast can a 1.5 Ghz P-M Banias be?

But I might try it again when my new system arives which has a far better math unit (Banias were the first P-M with not a that good math unit - my new system is a 2Ghz Core Duo ... 150-200% as fast ... per core)