Results 1 to 7 of 7
 11302010, 09:03 PM #1Member
 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.
 11302010, 10:55 PM #2Member
 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.
 11302010, 11:02 PM #3Member
 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.Signature 
Please use [CODE] tags and indent correctly. It really helps when reading your code.
 11302010, 11:07 PM #4Member
 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"!
 11302010, 11:17 PM #5Member
 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;
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).
 11302010, 11:21 PM #6Member
 Join Date
 Nov 2010
 Posts
 33
 Rep Power
 0
Sounds like you have defined an innerclass inside another class, so you cannot instantiate the innerclass 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.
 11302010, 11:29 PM #7Member
 Join Date
 Jun 2010
 Posts
 15
 Rep Power
 0
Similar Threads

Java Rectangle
By java_beginner_ in forum New To JavaReplies: 9Last Post: 10312010, 07:15 PM 
Wrong with Rectangle res = new Rectangle(0,0,0,0);???
By jiapei100 in forum AWT / SwingReplies: 3Last Post: 09252010, 03:39 PM 
Construct a rectangle
By cstokes91 in forum New To JavaReplies: 1Last Post: 01272010, 06:14 AM 
Rectangle and Colors
By urbim in forum Java AppletsReplies: 0Last Post: 07112009, 03:03 PM 
nonrectangle JPanel
By itaipee in forum AWT / SwingReplies: 4Last Post: 04302009, 11:58 PM
Bookmarks