Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Checking if two rectangles overlap each other

  1. Apr 1, 2014 #1
    Hi

    I have uploaded the problem in the two files 3 and 4. Basically I have to see if the two rectangles overlap each other given the x, y coordinates of the centre and the width and the height of them.
    Here is my code in Java.
    Code (Text):
    import java.util.Scanner;

    public class problem3_28 {

      public static void main(String[] args) {
     
        Scanner input = new Scanner(System.in);
                   
        System.out.print("Enter r1's center x-, y-coordinates, width, and height: ");
                   
        // get input from the standard input
                   
        double x1 = input.nextDouble(), y1 = input.nextDouble(), w1 = input.nextDouble();
                   
        double h1 = input.nextDouble() ;
                   
        System.out.print("Enter r2's center x-, y-coordinates, width, and height: ");
                   
        // get input from the standard input
        double x2 = input.nextDouble(), y2 = input.nextDouble(), w2 = input.nextDouble();
                   
        double h2 = input.nextDouble() ;
                   
        double left1, right1, up1, down1 , left2, right2, up2, down2 ;
                   
        //define up, down, left and right edges of the two rectangles
                   
        left1 = x1 - w1/2.0 ;  right1 = x1 + w1/2.0 ; up1 = y1 + h1/2.0 ; down1 = y1 - h1/2.0 ;
                   
        left2 = x2 - w2/2.0 ; right2 = x2 + w2/2.0 ; up2 = y2 + h2/2.0 ; down2 = y2 - h2/2.0 ;
                   
        // check if the two rectangles overlap each other or one is inside the other
                   
        if ( Math.abs(x1 - x2) < 1e-8 && Math.abs(y1 - y2) < 1e-8 && Math.abs(w1 - w2) < 1e-8 && Math.abs(h1 - h2) < 1e-8)
            System.out.println("Two rectangles match exactly");
        else if( right1 >= right2 && left2 >= left1 && up1 >= up2 && down2 >= down1)
            System.out.println("r2 is inside r1");
        else if( right2 >= right1 && left1 >= left2 && up2 >= up1 && down1 >= down2)
            System.out.println("r1 is inside r2");
        else if ( down2 >= up1 || down1 >= up2 || right2 <= left1 || right1 <= left2)
            System.out.println("r2 does not overlap r1");
        else
            System.out.println("r2 overlaps r1");
      }
    }
    I have run this with some test cases given and it works fine. I also ran some additional test runs for other cases. So do you think its ok ?

    Thanks
     

    Attached Files:

    • 3.png
      3.png
      File size:
      20 KB
      Views:
      265
    • 4.png
      4.png
      File size:
      21 KB
      Views:
      210
    Last edited by a moderator: Apr 1, 2014
  2. jcsd
  3. Apr 1, 2014 #2

    jim mcnamara

    User Avatar

    Staff: Mentor

    The algorithms I have seen for tasks like this do not assume the rectangles to be oriented with 2 sides parallel to the x-axis and 2 sides parallel to the y-axis. By 'test all cases' is this by any chance what the problem writer means?
     
  4. Apr 1, 2014 #3

    Mark44

    Staff: Mentor

    In the figures in the attached image, the rectangles are oriented with sides parallel to the axes. I don't know if that is something that should be assumed to always be true, though.
     
  5. Apr 1, 2014 #4

    Mark44

    Staff: Mentor

    isaacnewton,
    I altered your code a bit by removing a lot of TAB characters. By having so much indentation, many lines were not completely visible. Now, most of the lines can be seen without having to scroll to the right. Using TABs to indent is not good if it makes the lines too long. I don't know if that's something you can control in your IDE. With some IDE's you can have a TAB get turned into 3 or so spaces.
     
  6. Apr 3, 2014 #5
    Jim, I think problem assumes that all the rectangles involved have edges parallel to x and y axis. Mark thanks for the input....
     
  7. Apr 17, 2014 #6
    I don't like your magic 1e-8 everywhere. Anytime you have to copy and paste like that you should be thinking about automation. Perhaps write a function, or overload the operator, I'm not familar with Java.

    > Basically I have to see if the two rectangles overlap each other given the x, y coordinates of the centre and the width and the height of them

    I see you printing individual results for X1 inside X2 and a lot of other cases, not just overlap. Is that a requirement of the question you have not told us? If all you need is a simple yes or no, then there is a very simple and well known algorithm for overlap where the rectangles are parallel with the axes. It's not that hard to derive and you could do it only four comparisons.
     
  8. Apr 17, 2014 #7
    Hello gmar, I have posted the questions in an attachment. So yes, there is requirement of printing individual results for one inside the other.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook