-1
import java.util.Scanner;

public class RectangleTester {

    public static void main(String[] args) {
        Rectangle rectangle1 = new Rectangle(0, 0, 0, 0);
        Rectangle rectangle2 = new Rectangle(0, 0, 0, 0);

        Scanner input = new Scanner(System.in);
        System.out.println("Rectangle 1:");
        System.out.println("Enter the x coordinate ");
        int r1x1 = input.nextInt();
        System.out.println("Enter the y coordinate");
        int r1y1 = input.nextInt();
        System.out.println("Enter the width");
        int width1 = input.nextInt();
        System.out.println("Enter the height");
        int height1 = input.nextInt();
        rectangle1.setLeft(r1x1);
        rectangle1.setBottom(r1y1);
        rectangle1.setWidth(width1);
        rectangle1.setHeight(height1);
        
        System.out.println("\n*****************\nRectangle 2:");
        System.out.println("Enter the x coordinate ");
        int r2x1 = input.nextInt();
        System.out.println("Enter the y coordinate");
        int r2y1 = input.nextInt();
        System.out.println("Enter the width");
        int width2 = input.nextInt();
        System.out.println("Enter the height");
        int height2 = input.nextInt();
        rectangle2.setLeft(r2x1);
        rectangle2.setBottom(r2y1);
        rectangle2.setWidth(width2);
        rectangle2.setHeight(height2);
    
        System.out.println("\n****************\n"+"Rectangle 1: \n"+rectangle1.toString()+"\nArea is " + rectangle1.area(rectangle1) + "\nPerimeter is " + rectangle1.perimeter(rectangle1));
        System.out.println("*****************"+"\nRectangle 2: \n"+rectangle2.toString()+"\nArea is " + rectangle2.area(rectangle2) + "\nPerimeter is " + rectangle2.perimeter(rectangle2));
        
        int r1y2=r1y1+height1;
        int r1x2=r1x1+width1;
        int r2y2=r2y1+height2;
        int r2x2=r2x1+width1;
        
        int r3x1=Math.max(r1x1, r2x1);
        int r3x2=Math.min(r1x2, r2x2);
        int r3y1=Math.max(r1y1, r2y1);
        int r3y2=Math.min(r1y2, r2y2);
            
    }

    public static boolean intersection() {      
    }
}

I am trying write a method called intersection that takes two rectangles parameters and returns the rectangle that is formed when/if they overlap, if they don't overlap however, the method should return a rectangle where all fields are 0

Two rectangles do not overlap if one of the following conditions is true.

  1. One rectangle is above top edge of the other rectangle.
  2. One rectangle is on left side of left edge of the other rectangle.

If the rectangles only touch, but do not overlap, then the width or height should be zero, but all other parameters should be properly calculated and stored.

I could really use some help on how to structure this cause the logic is confusing to me!

Dada
  • 6,313
  • 7
  • 24
  • 43

2 Answers2

0

You can see method java.awt.Rectangle#intersection such method does what you described

p.s. Realization from java.awt.Rectangle#intersection

    int tx1 = this.x;
    int ty1 = this.y;
    int rx1 = r.x;
    int ry1 = r.y;
    long tx2 = tx1; tx2 += this.width;
    long ty2 = ty1; ty2 += this.height;
    long rx2 = rx1; rx2 += r.width;
    long ry2 = ry1; ry2 += r.height;
    if (tx1 < rx1) tx1 = rx1;
    if (ty1 < ry1) ty1 = ry1;
    if (tx2 > rx2) tx2 = rx2;
    if (ty2 > ry2) ty2 = ry2;
    tx2 -= tx1;
    ty2 -= ty1;
    // tx2,ty2 will never overflow (they will never be
    // larger than the smallest of the two source w,h)
    // they might underflow, though...
    if (tx2 < Integer.MIN_VALUE) tx2 = Integer.MIN_VALUE;
    if (ty2 < Integer.MIN_VALUE) ty2 = Integer.MIN_VALUE;
    return new Rectangle(tx1, ty1, (int) tx2, (int) ty2);
Cookie
  • 193
  • 7
0

This question is on LeetCode. https://leetcode.com/problems/rectangle-overlap/

Consider a rectangle represented by its lower left corner and upper right corner.

1st rectangle: x1,y1    x2,y2
2nd rectangle: x3,y3    x4,y4

The rectangles overlap if:

  • x3 or x4 falls between x1 and x2
  • x1 or x2 falls between x3 and x4

Thus, if you take the x-coordinates and sort them, for them to overlap, the first two and last two should be from different rectangles.

Same logic applies to the y-coordinates.

Java doesn't have a tuple type, but you can use Map.Entry class with the key as the x (or y) value, and the value as 1 or 2, indicating 1st or 2nd rectangle.

Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219