# Check if a point lies inside a rectangle | Set-2

• Last Updated : 09 Nov, 2021

Given coordinates of bottom-left and top-right corners of a rectangle. Check if a point (x, y) lies inside this rectangle or not.

Examples:

Attention reader! All those who say programming isn't for kids, just haven't met the right mentors yet. Join the  Demo Class for First Step to Coding Coursespecifically designed for students of class 8 to 12.

The students will get to learn more about the world of programming in these free classes which will definitely help them in making a wise career choice in the future.

Input: bottom-left: (0, 0), top-right: (10, 8), point: (1, 5)
Output: Yes

Input: bottom-left: (-1, 4), top-right:(2, 3), point:(0, 4)
Output: No

This problem is already discussed in a previous post. In this post we have discussed a new approach.

Approach: The above problem can be solved by observation. A point lies inside or not the rectangle if and only if it’s x-coordinate lies between the x-coordinate of the given bottom-right and top-left coordinates of the rectangle and y-coordinate lies between the y-coordinate of the given bottom-right and top-left coordinates.

Below is the implementation of the above approach:

## C++

 `// CPP program to Check if a``// point lies on or inside a rectangle | Set-2``#include ``using` `namespace` `std;` `// function to find if given point``// lies inside a given rectangle or not.``bool` `FindPoint(``int` `x1, ``int` `y1, ``int` `x2,``               ``int` `y2, ``int` `x, ``int` `y)``{``    ``if` `(x > x1 and x < x2 and y > y1 and y < y2)``        ``return` `true``;` `    ``return` `false``;``}` `// Driver code``int` `main()``{``    ``// bottom-left and top-right``    ``// corners of rectangle``    ``int` `x1 = 0, y1 = 0, x2 = 10, y2 = 8;` `    ``// given point``    ``int` `x = 1, y = 5;` `    ``// function call``    ``if` `(FindPoint(x1, y1, x2, y2, x, y))``        ``cout << ``"Yes"``;``    ``else``        ``cout << ``"No"``;` `    ``return` `0;``}`

## Java

 `// Java program to Check if``// a point lies on or inside``// a rectangle | Set-2``class` `GFG``{` `// function to find if given point``// lies inside a given rectangle or not.``static` `boolean` `FindPoint(``int` `x1, ``int` `y1, ``int` `x2,``                         ``int` `y2, ``int` `x, ``int` `y)``{``if` `(x > x1 && x < x2 &&``    ``y > y1 && y < y2)``    ``return` `true``;` `return` `false``;``}` `// Driver code``public` `static` `void` `main(String[] args)``{``    ` `    ``// bottom-left and top-right``    ``// corners of rectangle``    ``int` `x1 = ``0``, y1 = ``0``,``        ``x2 = ``10``, y2 = ``8``;` `    ``// given point``    ``int` `x = ``1``, y = ``5``;` `    ``// function call``    ``if` `(FindPoint(x1, y1, x2, y2, x, y))``        ``System.out.println(``"Yes"``);``    ``else``        ``System.out.println(``"No"``);``}``}` `// This code is contributed``// by ChitraNayal`

## Python3

 `# Python3 program to Check``# if a point lies on or``# inside a rectangle | Set-2` `# function to find if``# given point lies inside``# a given rectangle or not.``def` `FindPoint(x1, y1, x2,``              ``y2, x, y) :``    ``if` `(x > x1 ``and` `x < x2 ``and``        ``y > y1 ``and` `y < y2) :``        ``return` `True``    ``else` `:``        ``return` `False` `# Driver code``if` `__name__ ``=``=` `"__main__"` `:` `    ``# bottom-left and top-right``    ``# corners of rectangle.``    ``# use multiple assignment``    ``x1 , y1 , x2 , y2 ``=` `0``, ``0``, ``10``, ``8` `    ``# given point``    ``x, y ``=` `1``, ``5` `    ``# function call``    ``if` `FindPoint(x1, y1, x2,``                 ``y2, x, y) :``        ``print``(``"Yes"``)``    ``else` `:``        ``print``(``"No"``)` `# This code is contributed``# by Ankit Rai`

## C#

 `// C# program to Check if a``// point lies on or inside``// a rectangle | Set-2``using` `System;` `class` `GFG``{` `// function to find if given``// point lies inside a given``// rectangle or not.``static` `bool` `FindPoint(``int` `x1, ``int` `y1, ``int` `x2,``                      ``int` `y2, ``int` `x, ``int` `y)``{``if` `(x > x1 && x < x2 &&``    ``y > y1 && y < y2)``    ``return` `true``;` `return` `false``;``}` `// Driver code``public` `static` `void` `Main()``{``    ` `    ``// bottom-left and top-right``    ``// corners of rectangle``    ``int` `x1 = 0, y1 = 0,``        ``x2 = 10, y2 = 8;` `    ``// given point``    ``int` `x = 1, y = 5;` `    ``// function call``    ``if` `(FindPoint(x1, y1, x2, y2, x, y))``        ``Console.Write(``"Yes"``);``    ``else``        ``Console.Write(``"No"``);``}``}` `// This code is contributed``// by ChitraNayal`

## PHP

 ` ``\$x1` `and` `\$x` `< ``\$x2` `and``        ``\$y` `> ``\$y1` `and` `\$y` `< ``\$y2``)``        ``return` `true;` `    ``return` `false;``}` `// Driver code` `// bottom-left and top-right``// corners of rectangle``\$x1` `= 0; ``\$y1` `= 0;``\$x2` `= 10; ``\$y2` `= 8;` `// given point``\$x` `= 1; ``\$y` `= 5;` `// function call``if` `(FindPoint(``\$x1``, ``\$y1``, ``\$x2``,``              ``\$y2``, ``\$x``, ``\$y``))``    ``echo` `"Yes"``;``else``    ``echo` `"No"``;` `// This code is contributed``// by Akanksha Rai(Abby_akku)``?>`

## Javascript

 ``
Output
`Yes`

Time Complexity: O(1)

Auxiliary Space: O(1)

My Personal Notes arrow_drop_up