Results 1 to 7 of 7
  1. #1
    Xycose is offline Member
    Join Date
    Jun 2010
    Posts
    15
    Rep Power
    0

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

    Default

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

    Default

    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.

  4. #4
    RichWade is offline Member
    Join Date
    Nov 2010
    Posts
    33
    Rep Power
    0

    Default

    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. #5
    Xycose is offline Member
    Join Date
    Jun 2010
    Posts
    15
    Rep Power
    0

    Default

    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. #6
    RichWade is offline Member
    Join Date
    Nov 2010
    Posts
    33
    Rep Power
    0

    Default

    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. #7
    Xycose is offline Member
    Join Date
    Jun 2010
    Posts
    15
    Rep Power
    0

    Default

    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.

Similar Threads

  1. Java Rectangle
    By java_beginner_ in forum New To Java
    Replies: 9
    Last Post: 10-31-2010, 08:15 PM
  2. Wrong with Rectangle res = new Rectangle(0,0,0,0);???
    By jiapei100 in forum AWT / Swing
    Replies: 3
    Last Post: 09-25-2010, 04:39 PM
  3. Construct a rectangle
    By cstokes91 in forum New To Java
    Replies: 1
    Last Post: 01-27-2010, 07:14 AM
  4. Rectangle and Colors
    By urbim in forum Java Applets
    Replies: 0
    Last Post: 07-11-2009, 04:03 PM
  5. non-rectangle JPanel
    By itaipee in forum AWT / Swing
    Replies: 4
    Last Post: 05-01-2009, 12:58 AM

Posting Permissions

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