# does rectangle contain or overlap another rectangle?

• 11-30-2010, 10:03 PM
Xycose
does rectangle contain or overlap another rectangle?
I have an assignment to that takes in userinput(4 doubles: x y coordinates for the center of the rectangle, and width + height of the rectangle) to create 2 rectangles and two of the things we have to do with the info is check if one rectangle contains the other, and if they overlap with eachother.

I can't seem to think of the logic for how to tell if they contain and overlap... I have the rest of the program written but am having troubles with this. Can anyone point me in the right direction for how to go about this? Thanks.
• 11-30-2010, 11:55 PM
OK, so you have got x and y coords for the center of each rectangle as well as width and height.

First, determine the x and y coords of each corner of each rectangle.
upper_left_x = x - (width / 2);
upper_left_y = y - (height / 2);
(and so on until you have x and y for all four corners).

Now you can determine if they overlap. For example, if the left X coord for the first triangle is greater than the left X coord for the second triangle and less than the right X coord for the second triangle then they overlap.

That's the general idea - you should be able to figure out the rest.
• 12-01-2010, 12:02 AM
couling
Hmm... the hard thing here is telling you something helpful without telling you the answer. It's the same in 2D as it is in 1D, just you need to satisfy the condition twice.
Edit: and you can do it simpler tham Rich's method.
• 12-01-2010, 12:07 AM
Yes, my example was just one approach, I'm sure there is a simpler method (I didn't think too hard about it). You sound like my wife - she always wants me to "satisfy the condition twice"!
• 12-01-2010, 12:17 AM
Xycose
Thanks for the help, I think i figured out both overlap and contain, this is what I have:
Code:

```Boolean contains(Rectangle r1, Rectangle r2)                 {                         Boolean contains = false;                         if ((r1.x + - r2.x) < (r1.width + r2.width)/2                                         && (r1.y + - r2.y) < (r1.height + r2.height)/2                                         || (r2.x + - r1.x) < (r2.width + r1.width)/2                                         && (r2.y + - r1.y) < (r2.height + r1.height)/2);                         {                                 contains = true;                         }                         return contains;                        }                 Boolean overlaps(Rectangle r1, Rectangle r2)                 {                         Boolean overlaps = false;                         if ((r1.x + .5 * r1.width > r2.x + .5 * r2.width)                                         && (r1.y + .5 * r1.height > r2.y + .5 * r2.height)                                         || (r2.x + .5 * r2.width > r1.x + .5 * r1.width)                                         && (r2.y + .5 * r2.height > r1.y + .5 * r1.height) )                         {                                 overlaps = true;                         }                         return overlaps;```
I "Think" that is correct and will work, but now that I have that out of the way, I finished up the rest and can't get the program to run.. I can create the first rectangle object just fine but when I try to make a second one Eclipse gives me this error....
Quote:

No enclosing instance of type Exercise9_12 is accessible. Must qualify the allocation with an enclosing
instance of type Exercise9_12 (e.g. x.new A() where x is an instance of Exercise9_12).
I'll make a new thread on it. Thanks guys =)
• 12-01-2010, 12:21 AM