
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How To Check if a Given Point Lies Inside a Rectangle in Java?
A Rectangle is a closed two-dimensional shape that has 4 sides and 4 corners. The opposite sides are of the same length and parallel to each other. All 4 interior angles are equal, angles measure 90 degrees. It is a four-sided polygon.
Following is the diagram of the Rectangle:
Checking Points Inside Rectangle
A point is said to lie inside a rectangle if its x and y coordinates are between the x and y coordinates of the opposite corners of the rectangle. To determine whether the point lies within the rectangle, we will use the following approaches:
- 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 values are given
Then the given point x coordinate should lie inside the x coordinates of the rectangle, and the given point y coordinate should lie inside the y coordinates of the rectangle.
If four points of the rectangle are given
Then find the area of the rectangle and check that it should be the same as the four triangle areas formed by the point.
Input & Output Scenarios
Below are a few input and output scenarios that help you to understand how to check whether the point lies inside the rectangle:
Scenario 1
Suppose the bottom left corner and top right corner point values are (1, 1) and (9, 8), respectively.
Input: points = (x1 = 1, y1 = 1), (x2 = 9, y2 = 8) and (x = 2, y = 3) Output: Yes
Calculation: x > x1 and x < x2, y > y1 and y < y2
As you can see, the point x is greater than x1 but less than x2; similarly, the point y is greater than y1 but less than y2, and the point (2, 3) lies inside the rectangle.
Scenario 2
Suppose the bottom left corner and top right corner point values are (1, 1) and (9, 8), respectively.
Input: points = (x1 = 0, y1 = 0 ), (x2 = 10, y1 = 0), (x3 = 10, y3 = 10), (x4 = 0, y4 = 10) and (x = 13, y = 13) Output: No
The point (13, 13) does not lie inside the rectangle.
Example 1
The following program compares the given point (x = 2, y = 3) with (x1 = 1, x2 = 9 ) and (y1 = 1, y2 = 8), and displays whether the point lies inside the rectangle or not:
public class checkPoints { public static void main(String[] args) { int x1 = 1, y1 = 1; int x2 = 9, y2 = 8; int x = 2, y = 3; System.out.println("The given points are: "); System.out.println("(x1 = " + x1 + ", x2 = " + x2 + ")"); System.out.println("(y1 = " + y1 + ", y2 = " + y2 + ")"); System.out.println("(x = " + x + ", y = " + y + ")"); if (x > x1 && x < x2 && y > y1 && y < y2) { System.out.println("Yes!, the Yes given point lies inside rectangle"); } else{ System.out.println("No!, the Yes given point lies inside rectangle"); } } }
The above program produces the following output:
The given points are: (x1 = 1, x2 = 9) (y1 = 1, y2 = 8) (x = 2, y = 3) Yes!, the Yes given point lies inside rectangle
Example 2
This is another way to check whether a given point (x = 13, y = 13) lies inside a rectangle. We define a method checkPint() that compares the area of a rectangle with the sum of the areas of four triangles formed by the given point and the rectangle's vertices.
If the areas match, the point lies inside the rectangle; otherwise not.
public class rectangle { //method to calculate area of rectangle 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); } //method to check whether point lies inside rectangle 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){ return true; } return false; } public static void main (String[] args) { int x1 = 0, x2 = 10, x3 = 10, x4 = 0; int y1 = 0, y2 = 0, y3 = 10, y4 = 10; //given point to be checked int x = 13; int y = 13; System.out.println("The points needs to be checked: x = " + x + ", y = " + y); System.out.println("The rectangle four points are: "); System.out.println( "x1 = " + x1 + ", x2 = " + x2 + ", x3 = " + x3 + ", x4 = " + x4 ); System.out.println( "y1 = " + y1 + ", y2 = " + y2 + ", y3 = " + y3 + ", y4 = " + y4 ); boolean result = checkPoint(x1, y1, x2, y2, x3, y3, x4, y4, x, y); System.out.println("Is points the given points are lies inside the rectangle? " + result); } }
Following is the output of the above program:
The points needs to be checked: x = 13, y = 13 The rectangle four points are: x1 = 0, x2 = 10, x3 = 10, x4 = 0 y1 = 0, y2 = 0, y3 = 10, y4 = 10 Is points the given points are lies inside the rectangle? false