Help with Functions PtInRect and OverlapRect
Monkey Forums/Monkey Programming/Help with Functions PtInRect and OverlapRect
| ||
Well... My functions PTInRect and OverlapRect is not working as expected! Anybody seens something wrong? Thanks! Class TRect Field Left:Int Field Top:Int Field Right:Int Field Bottom:Int Field Width:Int Field Height:Int Method New(aLeft:Int,aTop:Int,aRight:Int,aBottom:Int) Left=aLeft Right=aRight Top=aTop Bottom=aBottom Width=Right-Left Height=Bottom-Top End Function Bounds:TRect(aLeft:int,aTop:int,aWidth:int,aHeight:int) return New TRect(aLeft,aTop,aLeft+aWidth,aTop+aHeight) End End Function PTInRect:Bool(x1:Int,y1:Int,aRect:TRect) Return (y1>=aRect.Top) And (y1<aRect.Bottom) And (x1>=aRect.Left) And (x1<aRect.Right) End Function OverlapRect:Bool(Rect1:TRect, Rect2: TRect) Local result : Bool result=PTInRect(Rect1.Left,Rect1.Top,Rect2) if Not result then result=PTInRect(Rect1.Right,Rect1.Top,Rect2) if Not result then result=PTInRect(Rect1.Left,Rect1.Bottom,Rect2) if Not result then result=PTInRect(Rect1.Right,Rect1.Bottom,Rect2) Return result End |
| ||
I found the problem! But There is a more efficient way of doing this with monkey?Function OverlapRect:Bool(Rect1:TRect, Rect2: TRect) Local result : Bool result=false if Not result then result=PTInRect(Rect1.Left,Rect1.Top,Rect2) if Not result then result=PTInRect(Rect1.Right,Rect1.Top,Rect2) if Not result then result=PTInRect(Rect1.Left,Rect1.Bottom,Rect2) if Not result then result=PTInRect(Rect1.Right,Rect1.Bottom,Rect2) if Not result then result=PTInRect(Rect2.Left,Rect2.Top,Rect1) if Not result then result=PTInRect(Rect2.Right,Rect2.Top,Rect1) if Not result then result=PTInRect(Rect2.Left,Rect2.Bottom,Rect1) if Not result then result=PTInRect(Rect2.Right,Rect2.Bottom,Rect1) return result End |
| ||
You'll have to do some timings if you want to check performance but it seems to me that you'd be better off avoiding all those point checks and just test the rect positions. Test if they're totally to the left or right or top or bottom of each other. If any of those are true then they aren't overlapping, if non of them are then they must be. Something like: Function OverlapRect:Bool(Rect1:TRect, Rect2: TRect) If( Rect1.Left > Rect2.Right Or Rect2.Left > Rect1.Right ) Return False End If( Rect1.Top > Rect2.Bottom Or Rect2.Top > Rect1.Bottom ) Return False End Return True End |
| ||
You should always try to prove things false as quickly as possible for repetitive checks.Function RectOverlap:Bool(x1#,y1#,x2#,y2#,x3#,y3#,x4#,y4#) if x2 < x3 Then return false if x1 > x4 Then return false if y2 < y3 Then return false if y1 > y4 Then return false return true End Function Where x1,y1,x2,y2 are the coordinates for the first rect and x3,y3,x4,y4 are the coordinates for the second rect. Function PointInRect:Bool(px#,py#,x1#,y1#,x2#,y2#) if px < x1 Then return false if px > x2 Then return false if py < y1 Then return false if py > y2 Then return false return true End Function so to check two of your TRects you would do Function CheckRects:Bool(Rect1:TRect,Rect2:TRect) return RectOverlap(Rect1.Left,Rect1.Top,Rect1.Right,Rect1.Bottom,Rect2.Left,Rect2.Top,Rect2.Right,Rect2.Bottom) End (well that's what I would do) |
| ||
Thanks Raz and Muddy! Muddy, I think there are still a small bug with your OverlapRect function! Rect1=(-1,188,5,220) Rect2=(0,0,320,480) ReckRects(Rect1,Rect2) I think it should return True in this case, but it is returning false! |
| ||
Strange... I have made a Delphi version of your function and debuged it with the same values and it returns me true! I will try to debug it someway in Monkey to see what is happening. Let me ask you something else! What does # do in the functions parameters? I haven't found anything in Monkey Docs. |
| ||
# = :Float |
| ||
It was a bug with my engine!!! Thanks for all the help! |