# How To Check if a Given Point Lies Inside a Rectangle in Java?

A rectangle is a closed two-dimensional shape which has 4 sides and 4 corners. The opposite sides are of the same length and parallel to each other. All the 4 interior angles are equal, angles measure 90 degrees each. It is a four-sided polygon.

We will see 2 approaches in this program.

• If bottom left corner and top right corner point values are given

• If four points of the rectangle are given

### If bottom left corner and top right corner point vales are given

Then the given point x−coordinate should lie inside the x−coordinate of the rectangle and the given point y−coordinate should lie inside the y−coordinate of the rectangle.

### If four points of the rectangle are given

Then find the area of the rectangle and check it should be the same to the four triangle areas formed by the point.

In this article we will see how we can check if a point lies inside a rectangle by using Java programming language.

## To show you some instances

### Instance-1

Suppose the bottom left corner and top right corner point values are (1, 1) and (9, 8) respectively.

Given point is (2, 3)

Then we can say the given point (2, 3) lies inside the rectangle.

## Algorithm

### Algorithm-1

• Step 1 − Get the bottom left corner (x1, y1) and top right corner (x2, y2) and given point value (x, y) either by initialization or by user input.

• Step 2 − As discussed above take an if condition and check if bottom left corner and top right corner point values are given then it should satisfy x > x1 and x < x2 and y > y1 and y < y2

• Step 3 − If the condition satisfies print the point lies inside the rectangle else not.

### Algorithm-2

• Step 1 − Get the four-point values of rectangle say A (x1, y1), B (x2, y2), C (x3, y3), D (x4, y4) and given point value P (x, y) either by initialization or by user input.

• Step 2 − As discussed above if 4 points of rectangle are given then find Area of rectangle ABCD and area of 4 triangles i.e area of triangle PAB, area of triangle PBC, area of triangle PCD, area of triangle PAD

• Step 3 − Then take an if condition and check if the area of the rectangle is the same with areas of 4 triangles or not. If same then point lies inside the rectangle else not.

## Multiple Approaches

We have provided the solution in different approaches.

• If bottom left corner and top right corner point values are given

• If four points of the rectangle are given

Let’s see the program along with its output one by one.

### Approach-1: If Bottom Left Corner and Top Right Corner Point Values are Given

In this approach the bottom left corner, top right corner point and one point value will be initialized in the program. Then by using Algorithm−1 check if the point lies inside the rectangle or not.

#### Example

public class Main {
//main method
public static void main(String[] args) {

//declared variables and initialized bottom-left corner coordinates
int x1 = 1, y1 = 1;

//declared variables and initialized top-right corner coordinates
int x2 = 9, y2 = 8;

//declared variables and initialized point value which needs to be checked
int x = 2, y = 3;

//calling the user defined method by passing all the point values as parameter
if (FindPoint(x1, y1, x2, y2, x, y))
System.out.println("Yes given point lies inside rectangle");
else
System.out.println("No given point does not lie inside rectangle");
}

//method to check given point lies inside rectangle or not
static boolean FindPoint(int x1, int y1, int x2, int y2, int x, int y) {

//if it satisfies the condition
if (x > x1 && x < x2 && y > y1 && y < y2) {
//then return true
return true;
}

//else return false
return false;
}
}


#### Output

Yes given point lies inside rectangle


### Approach-2: If Four Points of Rectangle are Given

In this approach four points of the rectangle will be initialized in the program. Then by using Algorithm−2 check if the point lies inside the rectangle or not.

#### Example

public class Main {
//main method
public static void main (String[] args) {

//declared the variables and initialized the point values
int x1=0;
int y1=0;
int x2=10;
int y2=0;
int x3=10;
int y3=10;
int x4=0;
int y4=10;

//given point to be checked
int x=13;
int y=13;

//calling the user defined method to check if point lies inside rectangle
if (checkPoint(x1,y1,x2,y2,x3,y3,x4,y4,x,y))
System.out.print("Yes given point lies inside rectangle");
else
System.out.print("No given point does not lie inside rectangle");
}

//user defined method to find area of triangle
static float triangleArea(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);
}

//user defined method to check whether point lies inside rectangle 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) {

//area of rectangle ABCD
float ABCD = (x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2))/2 + (x1*(y4-y3) + x4*(y3-y1) + x3*(y1-y4))/2;

//find area of triangle PAB
float PAB = triangleArea(x, y, x1, y1, x2, y2);

//find area of triangle PBC
float PBC = triangleArea(x, y, x2, y2, x3, y3);

//find area of triangle PCD
float PCD = triangleArea(x, y, x3, y3, x4, y4);

// find area of triangle PAD
float PAD = triangleArea(x, y, x1, y1, x4, y4);

//check if area of rectangle is
//equal to areas formed by four triangles
if(ABCD == PAB + PBC + PCD + PAD)

//then return true
return true;
else
//else return false
return false;
}
}


#### Output

No given point does not lie inside rectangle


In this article, we explored how to check if a point lies inside the rectangle in Java by using different approaches.