Original Post
Hey guys, Disapointed that the search feature is broken, but I have a simple problem. I need an efficient way to determine if two rectangles intersect. Here is the current version that I am working at:
bool Rect::DoesIntersect( const Rect& other ) const
{
//see which rectangle is 'bigger' - this is done as the bigger
//rect can never lie completely inside the smaller
unsigned int this_area = Width() * Height();
unsigned int other_area = other.Width() * other.Height();
const Rect* a;
const Rect* b;
if( this_area > other_area )
{
a = this;
b = &other
}
else
{
a = &other
b = this;
}
//rect 'a' can never lie completely 'inside' rect 'b'
//check top-left point
if( b->top >= a->top && b->top <= a->bottom )
if( b->left >= a->left && b->left <= a->right )
return true;
//check top-right point
if( b->top >= a->top && b->top <= a->bottom )
if( b->right >= a->left && b->right <= a->right )
return true;
//check bottom-left point
if( b->bottom >= a->top && b->bottom <= a->bottom )
if( b->left >= a->left && b->left <= a->right )
return true;
//check bottom-right point
if( b->bottom >= a->top && b->bottom <= a->bottom )
if( b->right >= a->left && b->right <= a->right )
return true;
return false;
}
Any tips or suggestions on cutting down all of those comparisons would be greatly appreciated!