# A box intersect function

This topic is 4585 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm not entirely sure how I would make this. I want to find out if two given boxes intersect. So the function when called would look like this: box(box1x1, box1y1, box1x2, box1y2, box2x1, box2y1, box2x2, box2y2) How would I find out if these two areas crossed each other? It's been confusing me for a while.

##### Share on other sites
Just take a pencil and paper and start drawing situations...I think you will find really fast the conditions for intersection....

such as for box1x1 > box2x1 && box1x1 < box2x2
that would be determine if the x1 of box1 lies in the x-range of box2...

##### Share on other sites
I think you could use something like this

BOOL RectInRect(LPRECT a, LPRECT b){  return (a->right < b->left ||          a->left > b->right ||          a->top > b->bottom ||          a->bottom < b->top) ? FALSE : TRUE;}

##### Share on other sites
Edit: Ignore this post, it's wrong due to my misreading of the above post.

Quote:
 Original post by BrocketinoI think you could use something like this*** Source Snippet Removed ***

That won't work. Even something as simple like this will return true with that code:

|--------||        ||   A    ||        ||--------||--------||        ||   B    ||        ||--------|

Since a->bottom < b->top (using Windows top-left coordintes)

You have to check if ANY of the corners of A is inside B. And vice versa.

Since this will fail if you only check if A corners are inside B:
|-------------------|| A                 ||   |---------|     ||   |   B     |     ||   |---------|     ||-------------------|

Edit: first image was wrong

[Edited by - crudbreeder on August 1, 2005 2:20:57 AM]

##### Share on other sites
Just check if you can find a axis where there isn't a intersection. Read about SAT (Separating Axis Theorem)

##### Share on other sites
Lol, thanks guys but I've just figured that out myself without checking here.

I got this:

bool box_intersect(int b1x1, int b1y1, int b1x2, int b1y2, int b2x1, int b2y1, int b2x2, int b2y2){
return (
(between(b1x1,b2x1, b2x2) && between(b1y1, b2y1, b2y2))
|| (between(b1x2, b2x1, b2x2) && between(b1y2, b2y1, b2y2))
|| (between(b2x1,b1x1,b1x2) && between(b2y1,b1y1, b1y2))
|| (between(b2x2, b1x1, b1x2) && between(b2y2, b1y1, b1y2))
);
}

Between checks whether or not the first num is between the second and third one. Would with work?

##### Share on other sites
The following works?

BOOL RectInRect(LPRECT a, LPRECT b){  return (a->right < b->left ||          a->left > b->right ||          a->top > b->bottom ||          a->bottom < b->top) ? FALSE : TRUE;}RECT rcTest = { 50, 50, 250, 250 };LRESULT CALLBACK WindowProc( ... ){  switch(uMsg)   {    case WM_LBUTTONDOWN:    {      RECT rcCursor = {       LOWORD(lParam) - 10,       HIWORD(lParam) - 10,       LOWORD(lParam) + 10,       HIWORD(lParam) + 10 };      if(RectInRect(&rcCursor,&rcTest))      {         MessageBox(hwnd,"RectInRect",NULL,MB_OK);      }      return 0;    } break;  }}

##### Share on other sites
Quote:
 Original post by BrocketinoThe following works?*** Source Snippet Removed ***
Yes that does work. It's a simple concatenation of the early out cases, resulting in an overall complete and optimal test. crudbreeder obviously didn't notice that you had FALSE : TRUE, not TRUE : FALSE.

The same kind a technique can be applied to Miles Lombardi's problem, however it will be much simpler if he knows that b1y1 is always less than b1y2 for example. If so, then it can be almost identical to your code:
return !(b1x2 < b2x1 || b1x1 > b2x2 || b1y1 > b2y2 || b1y2 < b2y1);
Much simpler than all those between calls.

##### Share on other sites
Quote:
Original post by iMalc
Quote:
 Original post by BrocketinoThe following works?
Yes that does work. It's a simple concatenation of the early out cases, resulting in an overall complete and optimal test. crudbreeder obviously didn't notice that you had FALSE : TRUE, not TRUE : FALSE.

Yes, sorry about that. I only looked at the if () part and not the entire code, my bad.

##### Share on other sites
Quote:
Original post by crudbreeder
Quote:
Original post by iMalc
Quote:
 Original post by BrocketinoThe following works?
Yes that does work. It's a simple concatenation of the early out cases, resulting in an overall complete and optimal test. crudbreeder obviously didn't notice that you had FALSE : TRUE, not TRUE : FALSE.

Yes, sorry about that. I only looked at the if () part and not the entire code, my bad.
That's okay, I nearly made the same mistake myself![smile]

##### Share on other sites
Quote:
Original post by iMalc
Quote:
 Original post by BrocketinoThe following works?*** Source Snippet Removed ***
Yes that does work. It's a simple concatenation of the early out cases, resulting in an overall complete and optimal test. crudbreeder obviously didn't notice that you had FALSE : TRUE, not TRUE : FALSE.

The same kind a technique can be applied to Miles Lombardi's problem, however it will be much simpler if he knows that b1y1 is always less than b1y2 for example. If so, then it can be almost identical to your code:
return !(b1x2 < b2x1 || b1x1 > b2x2 || b1y1 > b2y2 || b1y2 < b2y1);
Much simpler than all those between calls.

So what you just said would work if I knew that it was smaller? Okay I know how to do that, I'll try it.

##### Share on other sites
Quote:
Original post by Miles Lombardi
Quote:
Original post by iMalc
Quote:
 Original post by BrocketinoThe following works?*** Source Snippet Removed ***
Yes that does work. It's a simple concatenation of the early out cases, resulting in an overall complete and optimal test. crudbreeder obviously didn't notice that you had FALSE : TRUE, not TRUE : FALSE.

The same kind a technique can be applied to Miles Lombardi's problem, however it will be much simpler if he knows that b1y1 is always less than b1y2 for example. If so, then it can be almost identical to your code:
return !(b1x2 < b2x1 || b1x1 > b2x2 || b1y1 > b2y2 || b1y2 < b2y1);
Much simpler than all those between calls.

So what you just said would work if I knew that it was smaller? Okay I know how to do that, I'll try it.

What do u mean with 'would work if I knew that it was smaller', the rectangles could be of any size.