# Thread: does rectangle contain or overlap another rectangle?

1. Member
Join Date
Jun 2010
Posts
15
Rep Power
0

## 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.

2. Member
Join Date
Nov 2010
Posts
33
Rep Power
0
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.

3. Member
Join Date
Nov 2010
Posts
54
Rep Power
0
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.

4. Member
Join Date
Nov 2010
Posts
33
Rep Power
0
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"!

5. Member
Join Date
Jun 2010
Posts
15
Rep Power
0
Thanks for the help, I think i figured out both overlap and contain, this is what I have:
Java 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....
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 =)

6. Member
Join Date
Nov 2010
Posts
33
Rep Power
0
Sounds like you have defined an inner-class inside another class, so you cannot instantiate the inner-class unless you have an instance of the class that contains it.

I could be wrong, but that is what the error sounds like to me.

7. Member
Join Date
Jun 2010
Posts
15
Rep Power
0
so I could only make an object inside the class for it? I'm a little confused. If you could take a look at my other thread and let me know how it should be arranged that would be great. Getting a good grasp of OOP is a little more difficult to do on my own than I had thought it would be.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•