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:
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: If we observe carefully, It will be clear that for a point to be lie inside the rectangle, it should be lie inside the x-coordinate (x1, x2) of the rectangle as well as it should also lie inside the y-coordinate (y1, y2) of the rectangle.
Algorithm:
- Check if the x-coordinate of the given point (x, y) is lie inside the x-coordinate (x1, x2) of the rectangle, i.e, x > x1 and x < x2
- Check if the y-coordinate of the given point (x, y) is lie inside the y-coordinate (y1, y2) of the rectangle, i.e, y > y1 and y < y2
- If both the above condition satisfy then,
- The given point completely lie inside the rectangle.
- Otherwise, not.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
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 ;
}
int main()
{
int x1 = 0, y1 = 0, x2 = 10, y2 = 8;
int x = 1, y = 5;
if (FindPoint(x1, y1, x2, y2, x, y))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
C
#include <stdio.h>
#include <stdbool.h>
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 ;
}
int main()
{
int x1 = 0, y1 = 0, x2 = 10, y2 = 8;
int x = 1, y = 5;
if (FindPoint(x1, y1, x2, y2, x, y))
printf ( "Yes" );
else
printf ( "No" );
return 0;
}
|
Java
class GFG
{
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 ;
}
public static void main(String[] args)
{
int x1 = 0 , y1 = 0 ,
x2 = 10 , y2 = 8 ;
int x = 1 , y = 5 ;
if (FindPoint(x1, y1, x2, y2, x, y))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
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
if __name__ = = "__main__" :
x1 , y1 , x2 , y2 = 0 , 0 , 10 , 8
x, y = 1 , 5
if FindPoint(x1, y1, x2,
y2, x, y) :
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
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 ;
}
public static void Main()
{
int x1 = 0, y1 = 0,
x2 = 10, y2 = 8;
int x = 1, y = 5;
if (FindPoint(x1, y1, x2, y2, x, y))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
PHP
<?php
function FindPoint( $x1 , $y1 , $x2 ,
$y2 , $x , $y )
{
if ( $x > $x1 and $x < $x2 and
$y > $y1 and $y < $y2 )
return true;
return false;
}
$x1 = 0; $y1 = 0;
$x2 = 10; $y2 = 8;
$x = 1; $y = 5;
if (FindPoint( $x1 , $y1 , $x2 ,
$y2 , $x , $y ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function FindPoint(x1, y1, x2,
y2, x, y)
{
if (x > x1 && x < x2 && y > y1 && y < y2)
return true ;
return false ;
}
let x1 = 0, y1 = 0, x2 = 10, y2 = 8;
let x = 1, y = 5;
if (FindPoint(x1, y1, x2, y2, x, y))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!