Area of overlapping rectangles java – Java Program to Find Total Area Two Rectangles Overlap

In the previous article, we have seen Java Program to Check if Line Passes Through the Origin

Area of overlapping rectangles java: In this article we will discuss about Java Program to Find Total Area of Two Overlapping Rectangles.

Java Program to Find Total Area Two Rectangles Overlap

Area of two rectangles: Before going into the program directly, let’s first know hoe we can get the total area of two overlapping rectangle.

Explanation:

We can add the area of both the rectangles first, then we can subtract the area of intersecting part as it is present twice.

Total Area of Two Overlapping Rectangle =
(Area of First rectangle + Area of Second rectangle) - Area of Intersecting part

Let’s see different ways to see how to find total area of two overlapping rectangles.

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

Approach:

  • Declare theĀ  points.
  • Then call the totalArea() method where we will get the total area of two overlapping rectangle.
  • Inside method we will find area of first rectangle and area of second rectangle and intersecting part.
  • Then subtract the area of intersecting part from the addition of both area of first rectangle and second rectangle.
  • Then Print the result.

Program:

// JAVA Code to Java Program 
//to Find Total Area of Two Overlapping Rectangles 

public class Main
{ 
    //totaArea() method to find total area of two overlapping rectangles
    public static int  totalArea(int l1x,int l1y,int l2x,int l2y,int r1x,int r1y,int r2x,int r2y) 
    { 
        // Area of first Rectangle 
    	int firstArea = Math.abs(l1x - r1x) * Math.abs(l1y - r1y); 
      
    	// Area of second Rectangle 
    	int secondArea = Math.abs(l2x - r2x) * Math.abs(l2y - r2y); 
  
    // Length of intersecting part 
    int intersectingArea = (Math.min(r1x, r2x) - Math.max(l1x, l2x)) *  
                           (Math.min(r1y, r2y) - Math.max(l1y, l2y)); 

  
    //returning the totallength of overlapping rectangles
    return (firstArea + secondArea - intersectingArea); 
    } 
      
    /* Driver program to test above function */
    public static void main(String[] args)  
    { 
        //Points are declared
    int l1x=2;
    int l1y=1;
    int l2x=3;
    int l2y=2;
    int r1x=5;
    int r1y=5;
    int r2x=5;
    int r2y=7;
    //Calling totaArea() method 
    System.out.println("Total Area: " +totalArea(l1x,l1y,l2x,l2y,r1x,r1y,r2x,r2y)); 
    } 
}
Output:

Total Area: 16

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

Approach:

  • Take input of points.
  • Then call the totalArea() method where we will get the total area of two overlapping rectangle.
  • Inside method we will find area of first rectangle and area of second rectangle and intersecting part.
  • Then subtract the area of intersecting part from the addition of both area of first rectangle and second rectangle.
  • Then Print the result.

Program:

import java.util.*;

// JAVA Code to Java Program 
//to Find Total Area of Two Overlapping Rectangles 

public class Main
{ 
    //totaArea() method to find total area of two overlapping rectangles
    public static int  totalArea(int l1x,int l1y,int l2x,int l2y,int r1x,int r1y,int r2x,int r2y) 
    { 
        // Area of first Rectangle 
    	int firstArea = Math.abs(l1x - r1x) * Math.abs(l1y - r1y); 
      
    	// Area of second Rectangle 
    	int secondArea = Math.abs(l2x - r2x) * Math.abs(l2y - r2y); 
  
    // Length of intersecting part 
    int intersectingArea = (Math.min(r1x, r2x) - Math.max(l1x, l2x)) *  
                           (Math.min(r1y, r2y) - Math.max(l1y, l2y)); 

  
    //returning the totallength of overlapping rectangles
    return (firstArea + secondArea - intersectingArea); 
    } 
      
    /* Driver program to test above function */
    public static void main(String[] args)  
    {
        //Scanner class object created
    	Scanner sc=new Scanner(System.in);
    	//Takinginput of points
    	System.out.println("Enter x,y index of L1 : ");
    	int l1x=sc.nextInt();
    	int l1y=sc.nextInt();
    	System.out.println("Enter x,y index of L2 : ");
    	int l2x=sc.nextInt();
    	int l2y=sc.nextInt();
    	System.out.println("Enter x,y index of R1 : ");
    	int r1x=sc.nextInt();
    	int r1y=sc.nextInt();
    	System.out.println("Enter x,y index of R2 : ");
    	int r2x=sc.nextInt();
    	int r2y=sc.nextInt();
    
    	//Calling totaArea() method 
    	System.out.println("Total Area: " +totalArea(l1x,l1y,l2x,l2y,r1x,r1y,r2x,r2y)); 
    } 
}
Output:

Enter x,y index of L1 : 2 1
Enter x,y index of L2 : 3 2
Enter x,y index of R1 : 5 5
Enter x,y index of R2 : 5 7
Total Area: 16

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

Related Java Programs: