Java Program to Check Whether a Given Point Lies Inside a Rectangle or Not

In this article we will discuss about Java Program to Check Whether a Given Point Lies Inside a Rectangle or Not.

Java Program to Check Whether a Given Point Lies Inside a Rectangle or Not

Before jumping into the program directly, let’s first know how we can check whether a point lies or not inside a rectangle.

Explanation:

Suppose ABCD is the rectangle. Where A(x1, y1), B(x2, y2), C(x3, y3) and D(x4, y4).

P is the point. Where P(x, y).

Now find the area of rectangle ABCD as = Area of triangle ABC + Area of triangle ACD. 

i.e. Area of rectangle 'Area' = [ x1(y2 – y3) + x2(y3 – y1) + x3(y1-y2)]/2 + [ x1(y4 – y3) + x4(y3 – y1) + x3(y1-y4)]/2

Now find area of 4 triangles which can be formed using point 'P'

Area of the triangle PAB as Area1.
Area of the triangle PBC as Area2.
Area of the triangle PCD as Area3.
Area of the triangle PAD as Area4.

Add all the areas formed using point 'P' i.e. Area1 + Area2 + Area3 + Area4 

If Area1 + Area2 + Area3 + Area4 = Area Then point 'P' lies inside the rectangle

Else point 'P' lies outside the rectangle.

Let’s see different ways to check whether a given point lies inside the rectangle or not.

Method-1: Java Program to Check Whether a Given Point Lies Inside a Rectangle or Not By Using Static Value

Approach:

  • Coordinates of rectangle ‘ABCD’ are declared.
  • A point ‘P’ is declared.
  • Then checkPoint() method is called.
  • Inside this method we will check the point lies inside the rectangle or not using above logic.
  • Print the result.

Program:

public class Main
{
     // Driver code 
    public static void main (String[] args) 
    { 
          
        //Rectangle Coordinate A(10,10), B(10,-10), C(-10,-10), D(-10,-10)
        //Point P(0,0)
        //Calling the checkPoint() method
        if (checkPoint(10, 10, 10, -10, -10, -10, -10, 10, 0, 0)) 
            System.out.print("Point lies inside rectangle"); 
        else
            System.out.print("Point does not lie inside rectangle"); 
    } 
    
    //Method to calculate the area of triangle
    static float area(int x1, int y1, int x2,  
                        int y2, int x3, int y3) 
    { 
        return (float)Math.abs((x1 * (y2 - y3) +  
        x2 * (y3 - y1) + x3 * (y1 - y2)) / 2.0); 
    } 
          
    //Method to check whether point P(x, y) 
    //lies inside rectangle ABCD or not.
    static boolean checkPoint(int x1, int y1, int x2, int y2, 
    int x3, int y3, int x4, int y4, int x, int y) 
    { 
          
        // Find area of rectangle ABCD 
        float Area = area(x1, y1, x2, y2, x3, y3)+  
                area(x1, y1, x4, y4, x3, y3); 
      
        // Calculate area of triangle PAB 
        float Area1 = area(x, y, x1, y1, x2, y2); 
      
        // Calculate area of triangle PBC 
        float Area2 = area(x, y, x2, y2, x3, y3); 
      
        // Calculate area of triangle PCD 
        float Area3 = area(x, y, x3, y3, x4, y4); 
      
        // Calculate area of triangle PAD 
        float Area4 = area(x, y, x1, y1, x4, y4); 
      
        // Checking if sum of Area1, Area2, Area3 and Area4  
        // is same with Area or not
        //Returns true if Area == Area1 + Area2 + Area3 + Area4
        // else returns false
        return (Area == Area1 + Area2 + Area3 + Area4); 
    } 
}
Output:

Point lies inside rectangle

Method-2: Java Program to Check Whether a Given Point Lies Inside a Rectangle or Not By User Input Value

Approach:

  • Coordinates of rectangle ‘ABCD’ are taken as user input.
  • A point ‘P’ is taken as user input.
  • Then checkPoint() method is called.
  • Inside this method we will check the point lies inside the rectangle or not using above logic.
  • Print the result.

Program:

import java.util.*;

public class Main
{
     // Driver code 
    public static void main (String[] args) 
    { 
         
        //Scanner class object created
        Scanner sc=new Scanner(System.in);
        
        //Taking Dynamic input from user
    	System.out.print("Enter cordinate of point A : ");
    	int x1=sc.nextInt();
    	int y1=sc.nextInt();
    	System.out.print("\nEnter cordinate of point B : ");
    	int x2=sc.nextInt();
    	int y2=sc.nextInt();
    	System.out.print("\nEnter cordinate of point C : ");
    	int x3=sc.nextInt();
    	int y3=sc.nextInt();
    	System.out.print("\nEnter cordinate of point D : ");
    	int x4=sc.nextInt();
    	int y4=sc.nextInt();
    	System.out.println("\nEnter cordinate of point P : ");
    	int x=sc.nextInt();
        int y=sc.nextInt();	

        //Rectangle Coordinate A(10,10), B(10,-10), C(-10,-10), D(-10,-10)
        //Point P(0,0)
        //Calling the checkPoint() method
        if (checkPoint(x1,y1,x2,y2,x3,y3,x4,y4,x,y)) 
            System.out.println("Point lies inside rectangle"); 
        else
            System.out.println("Point does not lie inside rectangle"); 
    } 
    
    //Method to calculate the area of triangle
    static float area(int x1, int y1, int x2,  
                        int y2, int x3, int y3) 
    { 
        return (float)Math.abs((x1 * (y2 - y3) +  
        x2 * (y3 - y1) + x3 * (y1 - y2)) / 2.0); 
    } 
          
    //Method to check whether point P(x, y) 
    //lies inside rectangle ABCD or not.
    static boolean checkPoint(int x1, int y1, int x2, int y2, 
    int x3, int y3, int x4, int y4, int x, int y) 
    { 
          
        // Find area of rectangle ABCD 
        float Area = area(x1, y1, x2, y2, x3, y3)+  
                area(x1, y1, x4, y4, x3, y3); 
      
        // Calculate area of triangle PAB 
        float Area1 = area(x, y, x1, y1, x2, y2); 
      
        // Calculate area of triangle PBC 
        float Area2 = area(x, y, x2, y2, x3, y3); 
      
        // Calculate area of triangle PCD 
        float Area3 = area(x, y, x3, y3, x4, y4); 
      
        // Calculate area of triangle PAD 
        float Area4 = area(x, y, x1, y1, x4, y4); 
      
        // Checking if sum of Area1, Area2, Area3 and Area4  
        // is same with Area or not
        //Returns true if Area == Area1 + Area2 + Area3 + Area4
        // else returns false
        return (Area == Area1 + Area2 + Area3 + Area4); 
    } 
}
Output:

Enter cordinate of point A : 10 10
Enter cordinate of point B : 10 -10
Enter cordinate of point C : -10 -10
Enter cordinate of point D : -10 -10
Enter cordinate of point P : 0 0
Point lies inside rectangle

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.