Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Overlapping rectangles

This thread is locked; no one can reply to it. rss feed Print
Overlapping rectangles
Rick
Member #3,572
June 2003
avatar

There was a simple way to find the overlapping rectangle after you found that 2 rectangles have collided using min max functions but I can't remember it. Anyone know what that is?

========================================================
Actually I think I'm a tad ugly, but some women disagree, mostly Asians for some reason.

Kris Asick
Member #1,424
July 2001

if (rect1.x1 > rect2.x1) finalrect.x1 = rect1.x1; else finalrect.x1 = rect2.x1;
if (rect1.x2 < rect2.x2) finalrect.x2 = rect1.x2; else finalrect.x2 = rect2.x2;
if (rect1.y1 > rect2.y1) finalrect.y1 = rect1.y1; else finalrect.y1 = rect2.y1;
if (rect1.y2 < rect2.y2) finalrect.y2 = rect1.y2; else finalrect.y2 = rect2.y2;

That's a guess BTW, that I came up with moments after reading your post... I could be totally wrong about that. ::)

--- Kris Asick (Gemini)
--- http://www.pixelships.com

--- Kris Asick (Gemini)
--- http://www.pixelships.com

HardTranceFan
Member #7,317
June 2006
avatar

[edit]
Doh, Same as Kris's. I should learn to read before I write :)
[/edit]

Something like this?

//
// rect[n].<Left|Right|Top|Bottom>
//   n = rectangle number - 0 for first rectangle, 1 for second rectangle or 2 for overlap
//   Left = left edge
//   Right = right edge
//   Top = top edge
//   Bottom = bottom edge
// 
rect[2].Left =   max(rect[0].Left, rect[1].Left)
rect[2].Right =  min(rect[0].Right, rect[1].Right)
rect[2].Top =    max(rect[0].Top, rect[1].Top)
rect[2].Bottom = min(rect[0].Bottom, rect[1].Bottom)

[edit]
Is there any optimisation to :

finalrect.y2 = (rect1.y2 < rect2.y2) ? rect1.y2 : rect2.y2;

as an alternative to :

if (rect1.y2 < rect2.y2) finalrect.y2 = rect1.y2; else finalrect.y2 = rect2.y2;

[/edit]

--
"Shame your mind don't shine like your possessions do" - Faithless (I want more part 1)

Go to: