A box intersect function

Started by
10 comments, last by Brocketino 18 years, 9 months ago
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.
Advertisement
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...
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;}
Edit: Ignore this post, it's wrong due to my misreading of the above post.



Quote:Original post by Brocketino
I 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]
Just check if you can find a axis where there isn't a intersection. Read about SAT (Separating Axis Theorem)
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?
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;  }}


Quote:Original post by Brocketino
The 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.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Quote:Original post by iMalc
Quote:Original post by Brocketino
The 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.

Quote:Original post by crudbreeder
Quote:Original post by iMalc
Quote:Original post by Brocketino
The 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]
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement