Results 1 to 7 of 7
- 11-30-2010, 09:03 PM #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.
- 11-30-2010, 10:55 PM #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.
- 11-30-2010, 11:02 PM #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.----Signature ----
Please use [CODE] tags and indent correctly. It really helps when reading your code.
- 11-30-2010, 11:07 PM #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"!
- 11-30-2010, 11:17 PM #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:
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....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'll make a new thread on it. Thanks guys =)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).
- 11-30-2010, 11:21 PM #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.
- 11-30-2010, 11:29 PM #7
Member
- Join Date
- Jun 2010
- Posts
- 15
- Rep Power
- 0
Similar Threads
-
Java Rectangle
By java_beginner_ in forum New To JavaReplies: 9Last Post: 10-31-2010, 07:15 PM -
Wrong with Rectangle res = new Rectangle(0,0,0,0);???
By jiapei100 in forum AWT / SwingReplies: 3Last Post: 09-25-2010, 03:39 PM -
Construct a rectangle
By cstokes91 in forum New To JavaReplies: 1Last Post: 01-27-2010, 06:14 AM -
Rectangle and Colors
By urbim in forum Java AppletsReplies: 0Last Post: 07-11-2009, 03:03 PM -
non-rectangle JPanel
By itaipee in forum AWT / SwingReplies: 4Last Post: 04-30-2009, 11:58 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks