Checking if two rectangles overlap each other

  • Thread starter Thread starter issacnewton
  • Start date Start date
  • Tags Tags
    Overlap
Click For Summary
The discussion revolves around a Java program designed to determine if two rectangles overlap based on their center coordinates, width, and height. The code effectively calculates the edges of the rectangles and checks for various conditions to identify if they overlap, one is inside the other, or if they match exactly. The user has tested the code with various cases and is seeking feedback on its correctness and efficiency. There is a debate on whether the problem assumes rectangles are axis-aligned, which influences the approach to solving it. Some participants suggest simplifying the code by reducing excessive indentation and avoiding repeated constants like 1e-8, advocating for better coding practices such as using functions for repeated calculations. Additionally, there is mention of a more straightforward algorithm for determining overlap that requires fewer comparisons, indicating that the current implementation may be more complex than necessary given the problem's requirements.
issacnewton
Messages
1,035
Reaction score
37
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: 1,064
  • 4.png
    4.png
    14.5 KB · Views: 799
Last edited by a moderator:
Technology news on Phys.org
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?
 
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.
 
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.
 
Jim, I think problem assumes that all the rectangles involved have edges parallel to x and y axis. Mark thanks for the input...
 
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.
 
Hello gmar, I have posted the questions in an attachment. So yes, there is requirement of printing individual results for one inside the other.
 

Similar threads

  • · Replies 14 ·
Replies
14
Views
2K
Replies
8
Views
5K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
2
Views
3K