Java Program to Find if Two Rectangles Overlap

In the previous article, we have seen Java Program to Find Maximum Number of Squares That Can Fit in a Right-Angle Isosceles Triangle

In this article we will discuss about how to Find if Two Rectangles Overlap using Java programming language.

Java Program to Find if Two Rectangles Overlap

Before jumping into the program directly, let’s first know how can we Find if Two Rectangles Overlap

Explanation:

To Find if Two Rectangles Overlap We Have To Check For Two Conditions:

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

Let’s see different ways to Find if Two Rectangles Overlap.

Method-1: Java Program to Find if Two Rectangles Overlap By Using Static Value

Approach:

  1. Declare the value for the coordinates of top left and bottom right of both the rectangles.
  2. Then call the overlapCheck() method by passing all the coordinates value as parameter.
  3. This method checks if one rectangle lies above the top edge of the other or if one of the rectangle is on the left side/edge of the other.
  4. Then prints the result.

Program:

// JAVA Code to find if two rectangles overlap

import java.util.Scanner;

public class Main
{
    // Class to store the coordinates
    static class Coordinate
    {
        int x,y;
    }
    public static void main(String[] args)
    {
        // Declaring the variables
        Coordinate l1 = new Coordinate(),
        r1 = new Coordinate(),
        l2 = new Coordinate(),
        r2 = new Coordinate();

        // Initializing the variables with the rectangles coordinates
        l1.x=0;
        l1.y=20;
        r1.x=20;
        r1.y=0;
        l2.x=5;
        l2.y=5;
        r2.x=15;
        r2.y=0;

        if(overlapCheck(l1,r1,l2,r2))
            System.out.println("The rectangles overlap");
        else
            System.out.println("The rectangles do not overlap");
    }

    // Checks whether the rectangles overlap on each other
    public static boolean overlapCheck(Coordinate l1, Coordinate r1, Coordinate l2, Coordinate r2)
    {
        // Checks for first condition -
        // One rectangle is on left side of the other
        if(l1.x>r1.x||l2.x>r2.x)
            return false;
        // Checks for second condition -
        // One rectangle is above the other
        if(l1.y<r1.y||l2.y<r2.y)
            return false;
        
        return true;
    }
}
Output:

The rectangles overlap

Method-2: Java Program to Find  if Two Rectangles Overlap By User Input Value

Approach:

  1. Take user input value for the coordinates of top left and bottom right of both the rectangles.
  2. Then call the overlapCheck() method by passing all the coordinates value as parameter.
  3. This method checks if one rectangle lies above the top edge of the other or if one of the rectangle is on the left side/edge of the other.
  4. Then prints the result.

Program:

// JAVA Code to find if two rectangles overlap

import java.util.Scanner;

public class Main
{
    // Class to store the coordinates
    static class Coordinate
    {
        int x,y;
    }
    public static void main(String[] args)
    {
        // Declaring the variables
        Coordinate l1 = new Coordinate(),
        r1 = new Coordinate(),
        l2 = new Coordinate(),
        r2 = new Coordinate();

        Scanner sc = new Scanner(System.in);
        // Initializing the variables with the rectangles coordinates from the user
        System.out.println("Enter the top-left coordinates of first rectangle");
        l1.x=sc.nextInt();
        l1.y=sc.nextInt();
        System.out.println("Enter the top-left coordinates of second rectangle");
        l2.x=sc.nextInt();
        l2.y=sc.nextInt();
        System.out.println("Enter the bottom-right coordinates of first rectangle");
        r1.x=sc.nextInt();
        r1.y=sc.nextInt();
        System.out.println("Enter the bottom-right coordinates of second rectangle");
        r2.x=sc.nextInt();
        r2.y=sc.nextInt();

        if(overlapCheck(l1,r1,l2,r2))
            System.out.println("The rectangles overlap");
        else
            System.out.println("The rectangles do not overlap");
    }

    // Checks whether the rectangles overlap on each other
    public static boolean overlapCheck(Coordinate l1, Coordinate r1, Coordinate l2, Coordinate r2)
    {
        // Checks for first condition -
        // One rectangle is on left side of the other
        if(l1.x>r1.x||l2.x>r2.x)
            return false;
        // Checks for second condition -
        // One rectangle is above the other
        if(l1.y<r1.y||l2.y<r2.y)
            return false;
        
        return true;
    }
}
Output:

Enter the top-left coordinates of first rectangle
0 10
Enter the top-left coordinates of second rectangle
55  5
Enter the bottom-right coordinates of first rectangle
10 0
Enter the bottom-right coordinates of second rectangle
15 0
The rectangles overlap

Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.

Related Java Articles: