Checking if two rectangles overlap each other

  • Thread starter issacnewton
  • Start date
  • Tags
    Overlap
In summary: You could use the well known algorithm for overlap which is to check if the rectangles are parallel to the axes.
  • #1
issacnewton
1,003
31
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:
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
 

Attachments

  • 3.png
    3.png
    15.1 KB · Views: 993
  • 4.png
    4.png
    14.5 KB · Views: 730
Last edited by a moderator:
Technology news on Phys.org
  • #2
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?
 
  • #3
jim mcnamara said:
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?
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.
 
  • #4
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.
 
  • #5
Jim, I think problem assumes that all the rectangles involved have edges parallel to x and y axis. Mark thanks for the input...
 
  • #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.
 
  • #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.
 

Related to Checking if two rectangles overlap each other

1. How can I determine if two rectangles overlap each other?

To determine if two rectangles overlap, you can check if any of the four corners of one rectangle lie within the boundaries of the other rectangle. If this is true, then the two rectangles overlap.

2. What is the formula for checking if two rectangles overlap?

The formula for checking if two rectangles overlap is to compare the x and y coordinates of the top left and bottom right corners of each rectangle. If any of the x or y values for one rectangle fall within the range of the other rectangle's x or y values, then the two rectangles overlap.

3. Can rectangles overlap if they share a side?

Yes, rectangles can overlap if they share a side. As long as any part of one rectangle lies within the boundaries of the other rectangle, they are considered to be overlapping.

4. How do I handle rectangles with negative coordinates?

To handle rectangles with negative coordinates, you can shift the coordinates to make them all positive. For example, if a rectangle has coordinates (-2, -2) for the top left corner and (2, 2) for the bottom right corner, you can shift the coordinates by adding 2 to both the x and y values. This would change the coordinates to (0, 0) and (4, 4) respectively, making it easier to check for overlap.

5. Are there any special cases I should consider when checking for overlap?

One special case to consider is when one rectangle is entirely contained within the other. In this case, the rectangles may not overlap, but one rectangle is still considered to be inside the other. Another case to consider is when the rectangles share a side or corner, but do not overlap. In this case, they are considered to be adjacent, but not overlapping.

Similar threads

  • Programming and Computer Science
Replies
14
Views
2K
Replies
8
Views
5K
  • Programming and Computer Science
Replies
1
Views
2K
Back
Top