Check whether a given point lies inside a rectangle or not
Given four points of a rectangle, and one more point P. Write a function to check whether P lies within the given rectangle or not.
Examples:
Input : R = [(10, 10), (10, -10),
(-10, -10), (-10, 10)]
P = (0, 0)
Output : yes
Illustration :
Input : R = [(10, 10), (10, -10),
(-10, -10), (-10, 10)],
P = (20, 20)
Output : no
Illustration :
Prerequisite: Check whether a given point lies inside a triangle or not
Approach : Let the coordinates of four corners be A(x1, y1), B(x2, y2), C(x3, y3) and D(x4, y4). And coordinates of the given point P be (x, y)
1) Calculate area of the given rectangle, i.e., area of the rectangle ABCD as area of triangle ABC + area of triangle ACD.
Area A = [ x1(y2 – y3) + x2(y3 – y1) + x3(y1-y2)]/2 + [ x1(y4 – y3) + x4(y3 – y1) + x3(y1-y4)]/2
2) Calculate area of the triangle PAB as A1.
3) Calculate area of the triangle PBC as A2.
4) Calculate area of the triangle PCD as A3.
5) Calculate area of the triangle PAD as A4.
6) If P lies inside the triangle, then A1 + A2 + A3 + A4 must be equal to A.
C++
#include <bits/stdc++.h>
using namespace std;
float area( int x1, int y1, int x2, int y2,
int x3, int y3)
{
return abs ((x1 * (y2 - y3) + x2 * (y3 - y1) +
x3 * (y1 - y2)) / 2.0);
}
bool check( int x1, int y1, int x2, int y2, int x3,
int y3, int x4, int y4, int x, int y)
{
float A = area(x1, y1, x2, y2, x3, y3) +
area(x1, y1, x4, y4, x3, y3);
float A1 = area(x, y, x1, y1, x2, y2);
float A2 = area(x, y, x2, y2, x3, y3);
float A3 = area(x, y, x3, y3, x4, y4);
float A4 = area(x, y, x1, y1, x4, y4);
return (A == A1 + A2 + A3 + A4);
}
int main()
{
if (check(0, 10, 10, 0, 0, -10, -10, 0, 10, 15))
cout << "yes" ;
else
cout << "no" ;
return 0;
}
|
Java
class GFG
{
static float area( 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 );
}
static boolean check( int x1, int y1, int x2, int y2,
int x3, int y3, int x4, int y4, int x, int y)
{
float A = area(x1, y1, x2, y2, x3, y3)+
area(x1, y1, x4, y4, x3, y3);
float A1 = area(x, y, x1, y1, x2, y2);
float A2 = area(x, y, x2, y2, x3, y3);
float A3 = area(x, y, x3, y3, x4, y4);
float A4 = area(x, y, x1, y1, x4, y4);
return (A == A1 + A2 + A3 + A4);
}
public static void main (String[] args)
{
if (check( 0 , 10 , 10 , 0 , 0 , - 10 , - 10 , 0 , 10 , 15 ))
System.out.print( "yes" );
else
System.out.print( "no" );
}
}
|
Python3
def area(x1, y1, x2, y2, x3, y3):
return abs ((x1 * (y2 - y3) +
x2 * (y3 - y1) +
x3 * (y1 - y2)) / 2.0 )
def check(x1, y1, x2, y2, x3,
y3, x4, y4, x, y):
A = (area(x1, y1, x2, y2, x3, y3) +
area(x1, y1, x4, y4, x3, y3))
A1 = area(x, y, x1, y1, x2, y2)
A2 = area(x, y, x2, y2, x3, y3)
A3 = area(x, y, x3, y3, x4, y4)
A4 = area(x, y, x1, y1, x4, y4);
return (A = = A1 + A2 + A3 + A4)
if __name__ = = '__main__' :
if (check( 0 , 10 , 10 , 0 , 0 , - 10 ,
- 10 , 0 , 10 , 15 )):
print ( "yes" )
else :
print ( "no" )
|
C#
using System;
class GFG {
static float area( 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);
}
static bool check( int x1, int y1, int x2,
int y2, int x3, int y3,
int x4, int y4, int x, int y)
{
float A = area(x1, y1, x2, y2, x3, y3) +
area(x1, y1, x4, y4, x3, y3);
float A1 = area(x, y, x1, y1, x2, y2);
float A2 = area(x, y, x2, y2, x3, y3);
float A3 = area(x, y, x3, y3, x4, y4);
float A4 = area(x, y, x1, y1, x4, y4);
return (A == A1 + A2 + A3 + A4);
}
public static void Main ()
{
if (check(0, 10, 10, 0, 0, -10, -10, 0, 10, 15))
Console.Write( "yes" );
else
Console.Write( "no" );
}
}
|
Javascript
<script>
function area(x1, y1, x2, y2,
x3, y3)
{
return Math.abs((x1 * (y2 - y3) + x2 * (y3 - y1) +
x3 * (y1 - y2)) / 2.0);
}
function check(x1, y1, x2, y2, x3,
y3, x4, y4, x, y)
{
let A = area(x1, y1, x2, y2, x3, y3) +
area(x1, y1, x4, y4, x3, y3);
let A1 = area(x, y, x1, y1, x2, y2);
let A2 = area(x, y, x2, y2, x3, y3);
let A3 = area(x, y, x3, y3, x4, y4);
let A4 = area(x, y, x1, y1, x4, y4);
return (A == A1 + A2 + A3 + A4);
}
if (check(0, 10, 10, 0, 0, -10, -10, 0, 10, 15))
document.write( "yes" );
else
document.write( "no" );
</script>
|
PHP
<?php
function area( $x1 , $y1 , $x2 ,
$y2 , $x3 , $y3 )
{
return abs (( $x1 * ( $y2 - $y3 ) +
$x2 * ( $y3 - $y1 ) +
$x3 * ( $y1 - $y2 )) / 2.0);
}
function check( $x1 , $y1 , $x2 , $y2 , $x3 ,
$y3 , $x4 , $y4 , $x , $y )
{
$A = area( $x1 , $y1 , $x2 , $y2 , $x3 , $y3 ) +
area( $x1 , $y1 , $x4 , $y4 , $x3 , $y3 );
$A1 = area( $x , $y , $x1 , $y1 , $x2 , $y2 );
$A2 = area( $x , $y , $x2 , $y2 , $x3 , $y3 );
$A3 = area( $x , $y , $x3 , $y3 , $x4 , $y4 );
$A4 = area( $x , $y , $x1 , $y1 , $x4 , $y4 );
return ( $A == $A1 + $A2 + $A3 + $A4 );
}
if (check(0, 10, 10, 0, 0, -10,
-10, 0, 10, 15))
echo "yes" ;
else
echo "no" ;
?>
|
Output:
no
Time complexity: O(1)
Auxiliary Space: O(1)
This article is contributed by Shivam Pradhan (anuj_charm).
Approach#2: Using the Point-in-Polygon Algorithm
One way to check if a point lies inside a rectangle is to use the point-in-polygon algorithm. In this case, we can consider the rectangle as a polygon with four vertices, and check if the point P lies inside this polygon.
Algorithm
1. Define a function point_in_rect(rect, p) that takes the rectangle vertices rect and point p as input.
2. Use the ray casting algorithm to determine if the point p lies inside the rectangle polygon:
a. Initialize a count variable to zero
b. For each edge of the polygon, check if it intersects the ray extending from the point p to infinity in the positive x direction
c. If the edge intersects the ray, increment the count
d. If the count is odd, the point lies inside the polygon; otherwise, it lies outside
C++
#include <iostream>
#include <vector>
using namespace std;
bool point_in_rect(vector<pair< int , int > >& rect,
pair< int , int >& p)
{
int n = rect.size();
bool inside = false ;
int j = n - 1;
for ( int i = 0; i < n; i++) {
if ((rect[i].second > p.second)
!= (rect[j].second > p.second)) {
if (p.first
< (rect[j].first - rect[i].first)
* (p.second - rect[i].second)
/ (rect[j].second
- rect[i].second)
+ rect[i].first) {
inside = !inside;
}
}
j = i;
}
return inside;
}
int main()
{
vector<pair< int , int > > R = {
{ 10, 10 }, { 10, -10 }, { -10, -10 }, { -10, 10 }
};
pair< int , int > P = { 0, 0 };
cout << point_in_rect(R, P) << endl;
R = {
{ 10, 10 }, { 10, -10 }, { -10, -10 }, { -10, 10 }
};
P = { 20, 20 };
cout << point_in_rect(R, P) << endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
static boolean pointInRect( int [][] rect, int [] p)
{
int n = rect.length;
boolean inside = false ;
int j = n - 1 ;
for ( int i = 0 ; i < n; i++) {
if ((rect[i][ 1 ] > p[ 1 ]) != (rect[j][ 1 ] > p[ 1 ])
&& (p[ 0 ]
< (rect[j][ 0 ] - rect[i][ 0 ])
* (p[ 1 ] - rect[i][ 1 ])
/ (rect[j][ 1 ] - rect[i][ 1 ])
+ rect[i][ 0 ])) {
inside = !inside;
}
j = i;
}
return inside;
}
public static void main(String[] args)
{
int [][] R = { { 10 , 10 },
{ 10 , - 10 },
{ - 10 , - 10 },
{ - 10 , 10 } };
int [] P = { 0 , 0 };
System.out.println(pointInRect(R, P));
int [] P2 = { 20 , 20 };
System.out.println(pointInRect(R2, P2));
}
}
|
Python3
def point_in_rect(rect, p):
n = len (rect)
inside = False
j = n - 1
for i in range (n):
if ((rect[i][ 1 ] > p[ 1 ]) ! = (rect[j][ 1 ] > p[ 1 ])) and \
(p[ 0 ] < (rect[j][ 0 ] - rect[i][ 0 ]) * (p[ 1 ] - rect[i][ 1 ]) / (rect[j][ 1 ] - rect[i][ 1 ]) + rect[i][ 0 ]):
inside = not inside
j = i
return inside
R = [( 10 , 10 ), ( 10 , - 10 ), ( - 10 , - 10 ), ( - 10 , 10 )]
P = ( 0 , 0 )
print (point_in_rect(R, P))
R = [( 10 , 10 ), ( 10 , - 10 ), ( - 10 , - 10 ), ( - 10 , 10 )]
P = ( 20 , 20 )
print (point_in_rect(R, P))
|
C#
using System;
public class GFG {
public static bool PointInRect( int [][] rect, int [] p)
{
int n = rect.Length;
bool inside = false ;
int j = n - 1;
for ( int i = 0; i < n; i++) {
if ((rect[i][1] > p[1]) != (rect[j][1] > p[1])
&& (p[0]
< (rect[j][0] - rect[i][0])
* (p[1] - rect[i][1])
/ (rect[j][1] - rect[i][1])
+ rect[i][0])) {
inside = !inside;
}
j = i;
}
return inside;
}
public static void Main()
{
int [][] R = new int [][] { new int [] { 10, 10 },
new int [] { 10, -10 },
new int [] { -10, -10 },
new int [] { -10, 10 } };
bool inside1
= GFG.PointInRect(R, new int [] { 0, 0 });
Console.WriteLine(inside1);
bool inside2
= GFG.PointInRect(R, new int [] { 20, 20 });
Console.WriteLine(inside2);
}
}
|
Javascript
function point_in_rect(rect, p) {
const n = rect.length;
let inside = false ;
let j = n - 1;
for (let i = 0; i < n; i++) {
if ((rect[i][1] > p[1]) != (rect[j][1] > p[1])) {
if (p[0] < (rect[j][0] - rect[i][0]) * (p[1] - rect[i][1]) / (rect[j][1] - rect[i][1]) + rect[i][0]) {
inside = !inside;
}
}
j = i;
}
return inside;
}
let R = [[10, 10], [10, -10], [-10, -10], [-10, 10]];
let P = [0, 0];
console.log(point_in_rect(R, P));
R = [[10, 10], [10, -10], [-10, -10], [-10, 10]];
P = [20, 20];
console.log(point_in_rect(R, P));
|
Time Complexity: O(n), where n is the number of edges in the polygon (in this case, n=4)
Space Complexity: O(1)
Last Updated :
29 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...