0

I need some help regarding an assignment that I've received through school. I've been stuck on a specific part of the assignment, detecting whether a rectangle is overlapping another. I tried to create an If-statement for the cases when the two rectangles aren't overlapping, but the teacher said that it would be easier doing the opposite and gave me a picture of seven scenarios that I should create If-statements for. I'm not looking for an complete answer, just maybe a solution to one of the cases so I get an idea of how I should think, or maybe just a tip. Thanks!

here is the picture,

enter image description here

The program language is Java. The rectangles have the dimensions: dx(width), dy(height). They each have coordinates: x, y. Which are located on the top left of each rectangle.

  • 1
    "The rectangles have the dimensions: dx(width), dy(height). They each have coordinates: x, y. Which are located on the top left of each rectangle." is a reasonable hint to stare at. – clwhisk Feb 02 '22 at 22:30
  • Oh, that wasn't a hint, just thought it would be helpful information. –  Feb 02 '22 at 22:35
  • I just don't quite understand how I'm gonna formulate it into a If-statement. –  Feb 02 '22 at 22:37
  • 2
    **7** scenarios? I count **8** , and still not complete (better test each coordinate alone - only if both coordinates overlap, the rectangle will overlap) ((6 different scenarios for each coordinate)) – user16320675 Feb 02 '22 at 22:44
  • 1
    With the given info, do you see how to calculate each corner of a rectangle? – Andrew S Feb 02 '22 at 22:57
  • @AndrewS So you calculate the corners X and Y then I guess? –  Feb 02 '22 at 23:06
  • 1
    Yes - calculate each corner of each rectangle (step 1). Then as @user16320675 suggested, use those corners to determine if one overlaps the other. Expand the drawing, and do the math manually on paper. Then translate to your java program. – Andrew S Feb 02 '22 at 23:10
  • Yeah, I saw a couple of posts about that class, but We're not allowed to use pre-created classes. –  Feb 02 '22 at 23:17
  • The Rectangle class is not the problem. The lecture is about coding the overlap detection, so you must not use the Rectangle's method. – Queeg Feb 02 '22 at 23:24
  • Are they regarded as overlapping if only a corner touches, like {(0,0),(1,1)} vs {(1,1),(2,2)}? – phatfingers Feb 03 '22 at 00:33
  • Does this answer your question? [Determine if two rectangles overlap each other?](https://stackoverflow.com/questions/306316/determine-if-two-rectangles-overlap-each-other) – Yolomep Feb 03 '22 at 03:29
  • @Yolomep Yeah, actually it might. I figured it out already tho thanks to Hiran Chaudhuri but thanks anyways! –  Feb 03 '22 at 12:14

1 Answers1

0

Try to solve it for one dimension first, then the other and combine the results.

Line A would overlap line B if start of A is before end of B while at the same time end of A is after start of B.

So if these two criterions match for the horizontal coordinates, perform the same check for the vertical coordinates. Only when you have overlap both horizontally and vertically the rectangles have a common area.

In code:

boolean isOverlap(java.awt.Rectangle A, java.awt.Rectangle B) {
    if ((A.x <= B.x+B.width) && (A.x+A.width >= B.x)) {
        // overlap horizontally, now check vertical
        if ((A.y <= B.y+B.height) && (A.y+A.height >= B.y)) {
            // overlap also vertically
            return true;
        }
    }
    return false;
}
Queeg
  • 7,748
  • 1
  • 16
  • 42
  • Oh I see, I've come up with this If-statement from what you wrote. `if (room2.posX < r.bposX && room2.bposX > r.posX) { System.out.print("is overlapping (x)"); return true; } if (room2.posY < r.bposY && room2.bposY > r.posY) { System.out.print("is overlapping (y)"); return true; } else { System.out.print("isn't overlapping"); return false; }` But It says that they're overlapping even if one of the rectangles is at (0,0) and the other is at (0,20) bus it detects the X-axis overlapping. –  Feb 02 '22 at 23:03
  • sorry I don't know how to tab without sending message. –  Feb 02 '22 at 23:04
  • Looking at your code you return true when one of the axes overlap. You have to return true if both of them overlap. – Queeg Feb 02 '22 at 23:13
  • I do, I return true if x overlaps and if y overlaps, and false if none of those are overlapping. –  Feb 02 '22 at 23:16
  • For me your code reads: If horizontal overlap then return true. If vertical overlap then return true. If still here, return false. This returns true is one of them overlaps but not necessarily both. – Queeg Feb 02 '22 at 23:22
  • I tried putting them together like this: `if ((room2.posX < r.bposX && room2.bposX > r.posX)&&(room2.posY < r.bposY && room2.bposY > r.posY)) { ... }` Is it wrong, now it only seems to overlap. –  Feb 02 '22 at 23:29
  • All the given examples show an overlap - I could always find an area common to blue and red. So I'd not worry if you always get true with the above szenarios. If this is wrong please explain where. – Queeg Feb 02 '22 at 23:31
  • Oh sorry didn't see you added code. –  Feb 02 '22 at 23:39
  • Fixed mine! had missed some parentheses. Thanks fro the help. I've always struggled understanding this overlapping thing. This helped a lot! –  Feb 02 '22 at 23:47