May 24, 2020, 10:48:11 PM

### Author Topic: Can anybody provide a RectsOverlap function for AGK?  (Read 935 times)

#### Amon.

• Full Member
• Posts: 170
• What? There's no ceiling outside?
##### Can anybody provide a RectsOverlap function for AGK?
« on: April 01, 2019, 08:49:27 AM »
Can anybody provide a RectsOverlap function for AGK?
Windows 10 Pro - 16GB DDR4 RAM - GEFORCE RTX 2080 TI 11GB - AMD RYZEN 7 3800X

#### Derron

• Hero Member
• Posts: 2968
##### Re: Can anybody provide a RectsOverlap function for AGK?
« Reply #1 on: April 01, 2019, 08:54:43 AM »
Isn't this the same basic algorithm in most languages?

point in rect:
return ( otherX >= x And otherX < x+w And otherY >= y And otherY < y+h )

Now just do that for "top left" and "bottom right" of an rectangle - if one of both returns true then they intersect/overlap.

bye
Ron

#### Amon.

• Full Member
• Posts: 170
• What? There's no ceiling outside?
##### Re: Can anybody provide a RectsOverlap function for AGK?
« Reply #2 on: April 01, 2019, 09:00:24 AM »
Yes it is the same principle in nearly all programming languages but I need an AGK version. I notice this forum has blitz code archives. I'm going to hunt down a blitz one and see if I can convert it.
Thanks though.
Windows 10 Pro - 16GB DDR4 RAM - GEFORCE RTX 2080 TI 11GB - AMD RYZEN 7 3800X

#### Derron

• Hero Member
• Posts: 2968
##### Re: Can anybody provide a RectsOverlap function for AGK?
« Reply #3 on: April 01, 2019, 09:09:57 AM »
What do you want to look for in the archives? All you need is already written in my previous post: a line of code to check if a point (x,y) is in a rectangle and the instruction to do that for the top left and the bottom right point of a rectangle.
Of course you could use other approaches like "abs(x2-x1) < width" but they call a function "abs" which does some stuff too + a potential function call overhead.

Seems you want it to be chewed already :-)

bye
Ron

#### Amon.

• Full Member
• Posts: 170
• What? There's no ceiling outside?
##### Re: Can anybody provide a RectsOverlap function for AGK?
« Reply #4 on: April 01, 2019, 09:14:10 AM »
How does this look, Derron?

Code: [Select]
`function CheckCollide( x1 as float, y1 as float, x2 as float, y2 as float, width as float, height as float )    local r as integer    If abs( x1 - x2 ) < width and abs( y1 - y2 ) < height        r = 1        return    else        r = 0        return    endifendfunction r`
Windows 10 Pro - 16GB DDR4 RAM - GEFORCE RTX 2080 TI 11GB - AMD RYZEN 7 3800X

#### Derron

• Hero Member
• Posts: 2968
##### Re: Can anybody provide a RectsOverlap function for AGK?
« Reply #5 on: April 01, 2019, 09:22:58 AM »
It only checks if a point is within a rectangle.

You need to do that twice (as said) for top left (x,y) and bottom right (x+w, y+h) of your first rectangle.

bye
Ron

#### therevills

• Hero Member
• Posts: 607
##### Re: Can anybody provide a RectsOverlap function for AGK?
« Reply #6 on: April 01, 2019, 09:33:42 AM »
I've just quickly knocked this up:

RectOverlap
Code: [Select]
`function RectsOverlap(x1, y1, w1, h1, x2, y2, w2, h2) value = true If x1 >= (x2 + w2) Or (x1 + w1) <= x2 Then value = false If y1 >= (y2 + h2) Or (y1 + h1) <= y2 Then value = false endfunction value`
And PointInRect
Code: [Select]
`function PointInRect(x, y, w, h, otherX, otherY)endfunction ( otherX >= x And otherX < x+w And otherY >= y And otherY < y+h )`
Example:

Code: [Select]
`// show all errorsSetErrorMode(2)// set window propertiesSetWindowTitle("Test")SetWindowSize(1024, 768, 0)SetWindowAllowResize(1) // allow the user to resize the window// set display propertiesSetVirtualResolution(1024, 768) // doesn't have to match the windowSetOrientationAllowed(1, 1, 1, 1) // allow both portrait and landscape on mobile devicesSetSyncRate(30, 0) // 30fps instead of 60 to save batterySetScissor(0, 0, 0, 0) // use the maximum available screen space, no black bordersUseNewDefaultFonts(1)global false = 0global true = 1red = MakeColor( 255, 0, 0 )green = MakeColor( 0, 255, 0 )blue = MakeColor( 0, 0, 255 )box_x = Random(0, 1000)box_y = Random(0, 700)box_w = Random(10, 100)box_h = Random(10, 100)mouse_w = 50mouse_h = 50do Print(ScreenFPS()) mx = GetRawMouseX() my = GetRawMouseY() Print(mx) Print(my) if (RectsOverlap(mx, my, mouse_w, mouse_h, box_x, box_y, box_w, box_h)) color = red else color = green endif DrawBox(mx, my, mx + mouse_w, my + mouse_h, blue, blue, blue, blue, true) DrawBox(box_x, box_y, box_x + box_w, box_y + box_h, color, color, color, color, true) Sync()loopfunction RectsOverlap(x1, y1, w1, h1, x2, y2, w2, h2) value = true If x1 >= (x2 + w2) Or (x1 + w1) <= x2 Then value = false If y1 >= (y2 + h2) Or (y1 + h1) <= y2 Then value = false endfunction valuefunction PointInRect(x, y, w, h, otherX, otherY)endfunction ( otherX >= x And otherX < x+w And otherY >= y And otherY < y+h )`

#### MikeHart

• Hero Member
• Posts: 651
##### Re: Can anybody provide a RectsOverlap function for AGK?
« Reply #7 on: April 01, 2019, 10:32:50 AM »
Amon, is the buildin command GetSpriteCollision not suitable for you? you can even define collision shapes if my memory doesn't mislead me here.

#### Amon.

• Full Member
• Posts: 170
• What? There's no ceiling outside?
##### Re: Can anybody provide a RectsOverlap function for AGK?
« Reply #8 on: April 01, 2019, 11:05:26 AM »
Thanks, therevills. You are da dude.

@MikeHart
Like you said, I need to RTFM.
Windows 10 Pro - 16GB DDR4 RAM - GEFORCE RTX 2080 TI 11GB - AMD RYZEN 7 3800X

#### MikeHart

• Hero Member
• Posts: 651
##### Re: Can anybody provide a RectsOverlap function for AGK?
« Reply #9 on: April 01, 2019, 12:33:51 PM »
Thanks, therevills. You are da dude.

@MikeHart
Like you said, I need to RTFM.

Btw. that command will also help when the sprites are rotated.

#### Steve Elliott

• Hero Member
• Posts: 2433
##### Re: Can anybody provide a RectsOverlap function for AGK?
« Reply #10 on: April 01, 2019, 02:36:43 PM »
Windows 10, 64-bit, 16Gb RAM, CPU Intel i5, 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb).
MacOS Catalina, 64-bit, 8Gb RAM, CPU Intel i5, 2.3 Ghz, Intel Iris Plus Graphics 640 1536 MB.
Linux Mint 19.3, 64-bit, 16Gb RAM, CPU Intel i5, 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb).
Raspberry pi 3 and 4.

#### DaiHard

• Jr. Member
• Posts: 34
##### Re: Can anybody provide a RectsOverlap function for AGK?
« Reply #11 on: April 02, 2019, 09:14:50 AM »
Just out of interest, why won't Ron's method fail if a tall thin rectangle crosses a wide thin one (think of a fence post in the middle of a fence railing? All corners of both rectangles lie outside the other?

If TR and BL of the test rectangle both lie to the left/right/top bottom of the object, you can be confident, but otherwise you need to look more closely?

Best wishes,

D

#### Derron

• Hero Member
• Posts: 2968
##### Re: Can anybody provide a RectsOverlap function for AGK?
« Reply #12 on: April 02, 2019, 09:34:29 AM »
It does a "line check". Will try to draw something during lunch break - maybe it is explanable visually.

I might have mixed up "contains" with "intersects". My algorithm was for "rect a contains rect b".
So I stand corrected: algorithm in my first reply is NOT the one you need for "RectsOverlap".

My (BlitzMax) code for "intersects" is:
Code: BlitzMax
1.         'returns if the rect overlaps with the given one
2.         Method Intersects:int(rect:TRectangle) {_exposeToLua}
3.                 'checking if topleft or bottomright of the rect
4.                 'is contained in our rect via "containsXY" also returns true
5.                 'for rects next to each other:
6.                 'rectA = 0,0 - 10,10
7.                 'rectB = 10,10 - 20,10
8.                 '-> rectA contains point "10,10"
9.                 'return ( containsXY( rect.GetX(), rect.GetY() ) ..
10.                 '         OR containsXY( rect.GetX() + rect.GetW(),  rect.GetY() + rect.GetH() ) ..
11.                 '       )
12.
13.                 'to avoid this, we use "exclusive" ranges (> instead of >=)
14.                 Return ( GetX() < rect.GetX2() AND GetY() < rect.GetY2() ) AND ..
15.                        ( GetX2() > rect.GetX() AND GetY2() > rect.GetY() )
16.         End Method
17.

bye
Ron