Checking if two rectangles overlap each other

  • Thread starter Thread starter issacnewton
  • Start date Start date
  • Tags Tags
    Overlap
Click For Summary

Discussion Overview

The discussion revolves around determining whether two rectangles overlap based on their center coordinates, width, and height. Participants are examining a Java implementation of an algorithm designed for this purpose, considering both the correctness of the code and the assumptions underlying the problem.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant shares a Java code snippet to check for rectangle overlap and asks for feedback on its correctness.
  • Another participant questions whether the problem assumes rectangles are oriented with sides parallel to the axes, suggesting that algorithms for non-axis-aligned rectangles exist.
  • A participant comments on code formatting, suggesting that excessive indentation can hinder readability and offers a potential solution related to IDE settings.
  • One participant expresses concern about the use of a specific tolerance value (1e-8) in the code, suggesting that it might be better to automate this aspect or use a function for clarity.
  • Another participant confirms that the problem requires printing individual results for cases where one rectangle is inside another, indicating that this is part of the problem's requirements.

Areas of Agreement / Disagreement

Participants do not reach a consensus on whether the rectangles must be axis-aligned, and there are differing opinions on the implementation details and requirements of the problem.

Contextual Notes

Some assumptions about rectangle orientation and the necessity of specific output formats remain unresolved, and the discussion reflects varying interpretations of the problem 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,077
  • 4.png
    4.png
    14.5 KB · Views: 813
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
6K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
2
Views
3K