RectsOverlap

BlitzMax Forums/BlitzMax Beginners Area/RectsOverlap

Do I have to program my own RectsOverlap function or am I missing the new equivalent?

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


Or you could do..

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

Thanks James, Birdie is closest to retaining compatability with the old one so that wins it for me.

I'm coding my own exact replacement for the old syntax.

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


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.

I don't know how to use that last one?

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


With the OOP version, where would I put the code?

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.

Or change the "Method" into "Function" and just call:

TRectangle.Intersect

Is it possible somebody could post a short program? I'm confused.

Thanks,

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.

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).

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"