RectsOverlap

BlitzMax Forums/BlitzMax Beginners Area/RectsOverlap

matt!(Posted 2004) [#1]
Do I have to program my own RectsOverlap function or am I missing the new equivalent?


BlitzSupport(Posted 2004) [#2]
Good point. We do need a native one -- Birdie rolled this one early in the beta, so I know I've overlooked the need for it since then!

Function OverLap (x0, y0, x1, y1, x2, y2, x3, y3)
	If x0 > x3 Or x1 < x2 Then Return False
 	If y0 > y3 Or y1 < y2 Then Return False
 	Return True
End Function



ImaginaryHuman(Posted 2004) [#3]
Or you could do..

Function OverLap (x0,y0,x1,y1,wid,hig)


matt!(Posted 2004) [#4]
Thanks James, Birdie is closest to retaining compatability with the old one so that wins it for me.


matt!(Posted 2004) [#5]
I'm coding my own exact replacement for the old syntax.


matt!(Posted 2004) [#6]
Hope this helps. A slight modification of Birdie's function.

'legacy function
Function RectsOverlap (x0, y0, w0, h0, x2, y2, w2, h2)
	If x0 > (x2 + w2) Or (x0 + w0) < x2 Then Return False
	If y0 > (y2 + h2) Or (y0 + h0) < y2 Then Return False
	Return True
End Function



Jim Teeuwen(Posted 2004) [#7]
Shagwana did a nice n fast one. I've adapted it to use in a Bmax TRectangle type.

Type TRectangle

	Method Intersect:Int( rect:TRectangle )
		'// Thanks to Shagwana for this
		return ( ( ( ( (rect.X + rect.W) - X) ^ ( rect.X - (X + W) ) ) And ( ( rect.Y - (Y + H) ) ^ ( (rect.Y + rect.H) - Y ) ) ) And $80000000 )
	End Method
End Type


This one is fast, but might give weird results with negative coordinates.


matt!(Posted 2004) [#8]
I don't know how to use that last one?


Jim Teeuwen(Posted 2004) [#9]
Here's a non-OOP'ed version then.

Function RectsIntersect:Int( srcX%, srcY%, srcW%, srcH%, dstX%, dstY%, dstW%, dstH% )
	return ( ( ( ( (dstX + dstW) - srcX) ^ ( dstX - (srcX + srcW) ) ) And ( ( dstY - (srcY + srcH) ) ^ ( (dstY + dstH) - srcY ) ) ) And $80000000 )
End Function



matt!(Posted 2004) [#10]
With the OOP version, where would I put the code?


Todd(Posted 2004) [#11]
In a subclass of TRectangle. Then you'd use your subclass everywhere instead of the standard TRectangle type.

Type MyTRectangle Extends TRectangle
   ' Intersection Method Goes Here
End Type


(Edit): Never mind that. I thought that TRectangle was a built-in type. Defiance's first example is correct.


Warren(Posted 2004) [#12]
Or change the "Method" into "Function" and just call:

TRectangle.Intersect


matt!(Posted 2004) [#13]
Is it possible somebody could post a short program? I'm confused.

Thanks,


dmaz(Posted 2004) [#14]
I don't think you want to place it in the type just because you can. I really don't see why you would want a function like this inside an extended type. If you extend TRectangle with MyTRectangle then one or both of your types need to be a MyTRectangle. If I defined TRectangle myself I might put it in but in this case, I would just do

function Intersect:Int( r1:TRectangle, r2:TRectangle )
'// Thanks to Shagwana for this
return ( ( ( ( (r2.X + r2.W) - r1.X) ^ ( r2.X - (r1.X + r1.W) ) ) And ( ( r2.Y - (r1.Y + r1.H) ) ^ ( (r2.Y + r2.H) - r1.Y ) ) ) And $80000000 )
End Method

this function just compares base information that's why I wouldn't put it in an extended type.


Shagwana(Posted 2004) [#15]
Yep thats one of my babys!.

Original work is here. It does work but only if you obey certain rules when working with it (see link).


dan_upright(Posted 2004) [#16]
just thought i should point out to anyone using defiance's version up there, that you should have ~ instead of ^ as xor and you should be using & instead of "and"