# Java Program to Check if Point Lies in Rectangle

In the previous article, we have discussed about Java Program to Find Line Angle from Two Point

In this article we are going to see how to check if point lies in rectangle by using Java programming language.

## Java Program to Check if Point Lies in Rectangle

Before Jumping into the program directly let’s see how to check if point lies in rectangle.

Explanation:

Suppose the coordinates of bottom-left and top-right corners of a rectangle are given Q(x1,y1) & R(x2,y2)

Now, we have to check if a point P having coordinate (x, y) lies inside this rectangle or not.

We can check this by observation,

Point P is said to lie inside a rectangle if, the x coordinate of P lies in between the x-coordinate of the given bottom-right and top-left coordinates Q(x1), R(x2) of the rectangle and y-coordinate of P lies in between the y-coordinate of the given bottom-right and top-left coordinates Q(y1), R(y2) of the rectangle.

Example:

X1 = 0, y1 = 0

x2 = 10, y2 = 8

x = 1, y = 5

Point P lies inside the rectangle.

Let’s see different ways to to check if point lies in rectangle.

### Method-1: Java Program to Check if Point Lies in Rectangle By Using Static Input Value

Approach:

• Declare an int variable say ‘x1’ and assign the value to it, which holds the x coordinate of point Q
• Declare an int variable say ‘y1’ and assign the value to it, which holds the y coordinate of point Q
• Declare an int variable say ‘x2’ and assign the value to it, which holds the x coordinate of point R
• Declare an int variable say ‘y2’ and assign the value to it, which holds the y coordinate of point R
• Declare an int variable say ‘x’ and assign the value to it, which holds the x coordinate of point P
• Declare an int variable say ‘y’ and assign the value to it, which holds the y coordinate of point P
• Check the condition using the formula (x > x1 && x < x2) && (y > y1 && y < y2)
• Print the result.

Program:

public class Main
{
public static void main(String[] args)
{
int x1 = 0;
int y1 = 0;
int x2 = 10;
int y2 = 8;
int x = 1;
int y = 5;

if ( (x > x1 && x < x2) && (y > y1 && y < y2))
System.out.println("points P lies inside the rectangle");
else
System.out.println("points P doesn’t lie inside the rectangle");
}
}

Output:

points P lies inside the rectangle

### Method-2: Java Program to Check if Point Lies in Rectangle By Using User Input Value

Approach:

• Declare an int variable say ‘x1’ which holds the x coordinate of point Q
• Declare an int variable say ‘y1’ which holds the y coordinate of point Q
• Declare an int variable say ‘x2’ which holds the x coordinate of point R
• Declare an int variable say ‘y2’ which holds the y coordinate of point R
• Declare an int variable say ‘x’ which holds the x coordinate of point P
• Declare an int variable say ‘y’ which holds the y coordinate of point P
• Then we will take the value of “x1”, “y1”, “x2”, “y2”, “x”, “y” as user input using scanner class.
• Check the condition using the formula (x > x1 && x < x2) && (y > y1 && y < y2)
• Print the result.

Program:

import java.util.*;
public class Main
{
public static void main(String[] args)
{
// Create a Scanner object
Scanner s = new Scanner(System.in);
System.out.println("Enter the x coordinate of 1st point Q");
double x1 = s.nextDouble();
System.out.println("Enter the y coordinate of 1st point Q");
double y1 = s.nextDouble();
System.out.println("Enter the x coordinate of 2nd point R");
double x2 = s.nextDouble();
System.out.println("Enter the y coordinate of 2nd point R");
double y2 = s.nextDouble();
System.out.println("Enter the x coordinate of point P");
double px = s.nextDouble();
System.out.println("Enter the y coordinate of point P");
double py = s.nextDouble();
if ( (px > x1 &&  px < x2) && (py > y1 && py < y2))
System.out.println("points P lies inside the rectangle");
else
System.out.println("points P doesn’t lie inside the rectangle");
}
}

Output:

Enter the x coordinate of 1st point Q
1
Enter the y coordinate of 1st point Q
2
Enter the x coordinate of 2nd point R
3
Enter the y coordinate of 2nd point R
4
Enter the x coordinate of point P
5
Enter the y coordinate of point P
6
points P doesn’t lie inside the 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.

Related Java Programs: