The Wayback Machine - https://web.archive.org/web/20240823160310/https://www.geeksforgeeks.org/check-whether-a-given-point-lies-on-or-inside-the-rectangle-set-3/
Open In App

Check whether a given point lies on or inside the rectangle | Set 3

Last Updated : 31 Aug, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow
Show Topics
Topics:
Solve Problem
Easy
26.87%
186

Given two numbers a and b where b < a form a rectangle with points (0, b), (b, 0), (a-b, b), (b, a-b). Given a point (x, y), the task is to check whether this point lies inside or on the rectangle or not. 
Examples: 
 

Input: a = 7, b = 2, x = 5, y = 2;
Output: Given point does not lie on the rectangle

Input: a = 7, b = 2, x = 4, y = 5;
Output: Given point lies inside the rectangle

 

Approach: An efficient way is to form 4 line’s equation of a rectangle. Then, if a given point lies in or on the rectangle if and only if, substituting the given point on: 
 

  1. The right sideline ( x-y-b = 0 ) must give the value less than or equals to zero.
  2. The left sideline ( x-y+a = 0 ) must give the value greater than or equals to one.
  3. The upper sideline ( x+y-2*a+b = 0 ) must give the value less than or equals to zero.
  4. The lower sideline ( x+y-b = 0 ) must give the value greater than or equals to one.

 

C++




// C++ program to Check whether a given point
// lies inside or on the rectangle or not
#include <bits/stdc++.h>
using namespace std;
 
// function to Check whether a given point
// lies inside or on the rectangle or not
bool LiesInsieRectangle(int a, int b, int x, int y)
{
    if (x - y - b <= 0 && x - y + b >= 0
        && x + y - 2 * a + b <= 0 && x + y - b >= 0)
        return true;
 
    return false;
}
 
// Driver code
int main()
{
    int a = 7, b = 2, x = 4, y = 5;
 
    if (LiesInsieRectangle(a, b, x, y))
        cout << "Given point lies inside the rectangle";
    else
        cout << "Given point does not lie on the rectangle";
 
return 0;
}


C




// C program to Check whether a given point
// lies inside or on the rectangle or not
#include <stdio.h>
#include <stdbool.h>
 
// function to Check whether a given point
// lies inside or on the rectangle or not
bool LiesInsieRectangle(int a, int b, int x, int y)
{
    if (x - y - b <= 0 && x - y + b >= 0
        && x + y - 2 * a + b <= 0 && x + y - b >= 0)
        return true;
 
    return false;
}
 
// Driver code
int main()
{
    int a = 7, b = 2, x = 4, y = 5;
 
    if (LiesInsieRectangle(a, b, x, y))
        printf("Given point lies inside the rectangle");
    else
        printf("Given point does not lie on the rectangle");
 
return 0;
}
 
// This code is contributed by kothavvsaakash.


Java




// Java program to Check whether
// a given point lies inside or
// on the rectangle or not
class GFG
{
 
// function to Check whether
// a given point lies inside
// or on the rectangle or not
static boolean LiesInsieRectangle(int a, int b,
                                  int x, int y)
{
if (x - y - b <= 0 && x - y + b >= 0 &&
    x + y - 2 * a + b <= 0 && x + y - b >= 0)
    return true;
 
return false;
}
 
// Driver code
public static void main(String[] args)
{
    int a = 7, b = 2, x = 4, y = 5;
 
    if (LiesInsieRectangle(a, b, x, y))
        System.out.println("Given point lies " +
                        "inside the rectangle");
    else
        System.out.println("Given point does not " +
                            "lie on the rectangle");
}
}
 
// This code is contributed
// by ChitraNayal


Python3




# Python 3 program to Check whether
# a given point lies inside or on
# the rectangle or not
 
# function to Check whether a given
# point lies inside or on the
# rectangle or not
def LiesInsieRectangle(a, b, x, y) :
 
    if(x - y - b <= 0 and
       x - y + b >= 0 and
       x + y - 2 * a + b <= 0 and
       x + y - b >= 0) :
        return True
     
    return False
 
# Driver code
if __name__ == "__main__" :
 
    # multiple assignments
    a, b, x, y = 7, 2, 4, 5
 
    if LiesInsieRectangle(a, b, x, y) :
        print("Given point lies inside"
                      " the rectangle")
    else :
        print("Given point does not lie"
                    " on the rectangle")
 
# This code is contributed by ANKITRAI1


C#




// C# program to Check whether
// a given point lies inside
// or on the rectangle or not
using System;
class GFG
{
 
// function to Check whether
// a given point lies inside
// or on the rectangle or not
static bool LiesInsieRectangle(int a, int b,
                               int x, int y)
{
if (x - y - b <= 0 &&
    x - y + b >= 0 &&
    x + y - 2 * a + b <= 0 &&
    x + y - b >= 0)
    return true;
 
return false;
}
 
// Driver code
public static void Main()
{
    int a = 7, b = 2, x = 4, y = 5;
 
    if (LiesInsieRectangle(a, b, x, y))
        Console.Write("Given point lies " +
                   "inside the rectangle");
    else
        Console.Write("Given point does not " +
                       "lie on the rectangle");
}
}
 
// This code is contributed
// by ChitraNayal


PHP




<?php
// PHP program to Check whether
// a given point lies inside
// or on the rectangle or not
 
// function to Check whether
// a given point lies inside
// or on the rectangle or not
function LiesInsieRectangle($a, $b,
                            $x, $y)
{
    if ($x - $y - $b <= 0 &&
        $x - $y + $b >= 0 &&
        $x + $y - 2 * $a + $b <= 0 &&
        $x + $y - $b >= 0)
        return true;
 
    return false;
}
 
// Driver code
$a = 7;
$b = 2;
$x = 4;
$y = 5;
 
if (LiesInsieRectangle($a, $b,
                       $x, $y))
    echo "Given point lies ".
         "inside the rectangle";
else
    echo "Given point does not".
        " lie on the rectangle";
 
// This code is contributed by mits
?>


Javascript




<script>
 
// Javascript program to Check whether a given point
// lies inside or on the rectangle or not
 
 
// function to Check whether a given point
// lies inside or on the rectangle or not
function LiesInsieRectangle(a, b, x, y)
{
    if (x - y - b <= 0 && x - y + b >= 0
        && x + y - 2 * a + b <= 0 && x + y - b >= 0)
        return true;
 
    return false;
}
 
// Driver code
  
    let a = 7, b = 2, x = 4, y = 5;
 
    if (LiesInsieRectangle(a, b, x, y))
        document.write("Given point lies inside the rectangle");
    else
        document.write("Given point does not lie on the rectangle");
 
// This code is contributed by Mayank Tyagi
 
</script>


Output: 

Given point lies inside the rectangle

 

Time Complexity: O(1), since there is no loop or recursion.
Auxiliary Space: O(1), since no extra space has been taken.



Similar Reads

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 : yesIllustration : Input : R = [(10, 10), (10, -10), (-10, -10), (-10, 10)], P = (20, 20)Output : noIllustration : Prerequisite: Che
14 min read
Check if a point lies inside a rectangle | Set-2
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 pos
6 min read
Check whether a given point lies inside a triangle or not
Given three corner points of a triangle, and one more point P. Write a function to check whether P lies within the triangle or not. Example: Input: A = (0, 0), B = (10, 30), C = (20, 0), P(10, 15)Output: InsideExplanation: B(10,30) / \ / \ / \ / P \ P' / \ A(0,0) ----------- C(20,0) Input: A = (0, 0), B = (10, 30), C = (20, 0), P(30, 15)Output: Out
15+ min read
Check whether a point lies inside a sphere or not
Given co-ordinates of the center of a sphere (cx, cy, cz) and its radius r. Our task is to check whether a point (x, y, z) lies inside, outside or on this sphere.Examples: Input : Centre(0, 0, 0) Radius 3 Point(1, 1, 1) Output :Point is inside the sphere Input :Centre(0, 0, 0) Radius 3 Point(2, 1, 2) Output :Point lies on the sphere Input :Centre(0
7 min read
Check if the given point lies inside given N points of a Convex Polygon
Given coordinates of the N points of a Convex Polygon. The task is to check if the given point (X, Y) lies inside the polygon. Examples: Input: N = 7, Points: {(1, 1), (2, 1), (3, 1), (4, 1), (4, 2), (4, 3), (4, 4)}, Query: X = 3, Y = 2 Below is the image of plotting of the given points: Output: YES Input: N = 7, Points: {(1, 1), (2, 1), (3, 1), (4
15 min read
How to check if a given point lies inside or outside a polygon?
Given a polygon and a point 'p', find if 'p' lies inside the polygon or not. The points lying on the border are considered inside. Examples: Recommended ProblemPlease solve it on PRACTICE first, before moving on to the solution Solve ProblemApproach: The idea to solve this problem is based on How to check if two given line segments intersect, and t
9 min read
Check whether the point (x, y) lies on a given line
Given the values of m and c for the equation of a line y = (m * x) + c, the task is to find whether the point (x, y) lies on the given line. Examples: Input: m = 3, c = 2, x = 1, y = 5 Output: Yes m * x + c = 3 * 1 + 2 = 3 + 2 = 5 which is equal to y Hence, the given point satisfies the line's equation Input: m = 5, c = 2, x = 2, y = 5 Output: No A
4 min read
Find a point that lies inside exactly K given squares
Given an integer K and an array arr each of whose element x represents a square with two of its vertices as (0, 0) and (x, x). The task is to find a point which lies in exactly K squares. Examples: Input: arr[] = {1, 2, 3, 4}, K = 2 Output: (3, 3) The point (3, 3) lies inside 3rd and 4th square only. Input: arr[] = {8, 1, 55, 90}, K = 3 Output: (8,
3 min read
Find if a point lies inside a Circle
Given a circle (coordinates of centre and radius) and a point (coordinate), find if the point lies inside or on the circle, or not. Examples : Input: x = 4, y = 4 // Given Point circle_x = 1, circle_y = 1, rad = 6; // Circle Output: Inside Input: x = 3, y = 3 // Given Point circle_x = 0, circle_y = 1, rad = 2; // Circle Output: Outside We strongly
5 min read
Find if a point lies inside, outside or on the circumcircle of three points A, B, C
Given four non-collinear points A, B, C, and P. The task is to find whether point P lies inside, outside or on the circumcircle formed by points A, B, and C.Examples Input: A = {2, 8}, B = {2, 1}, C = {4, 5}, P = {2, 4}; Output: Point (2, 4) is inside the circumcircle. Input: A = {2, 8}, B = {2, 1}, C = {4, 5}, P = {3, 0}; Output: Point (3, 0) lies
15+ min read
Check if given point lies in range of any of the given towers
Given a 2D array arr[][] consisting of N rows of the form {Xi, Yi, Ri} such that (Xi, Yi) represents the position of a tower and Ri represents the network range of that tower. Given two integers X and Y, the task is to check if the point (X, Y) lies in the network range of the towers or not. Examples: Input: arr[][] = { {1, 1, 3}, {10, 10, 5}, {15,
7 min read
Check if a given circle lies completely inside the ring formed by two concentric circles
Given two circles of radius r and R, both have their centre at the origin. Now, given another circle of radius r1 and centre at (x1, y1). Check, if the third circle(circle of radius r1) lies completely inside the ring formed by two circles of radius r and R.Examples : Input : r = 8 R = 4 r1 = 2 x1 = 6 y1 = 0 Output : yes Input : r = 8 R = 4 r1 = 2
6 min read
Make N pairs from Array as (X, Y) coordinate point that are enclosed inside a minimum area rectangle
Given a number N, and an array A[] of size 2N, the task is to make N pairs of these array elements and place them on an X-Y coordinate plane, such that they are enclosed inside a minimum area rectangle( with sides parallel to the X-axis and Y-axis) and print the area of the rectangle. Examples: Input: N = 4, A = {1, 4, 2, 5, 3, 6, 7, 8}Output: 9Exp
7 min read
Check if given point lies on the Square Spiral Path
Given a 2D Graph, present initially at (0, 0). Print "YES" if the given point (X, Y) lies on the spiral path, otherwise "NO". The spiral path is to be followed ln a sequential manner as given below: Move one unit right side from the current position.Move two units upwards from the current position.Move three units left side from the current positio
6 min read
Check if a circle lies inside another circle or not
Given two circles with radii and centres given. The task is to check whether the smaller circle lies inside the bigger circle or not.  Examples:   Input: x1 = 10, y1 = 8, x2 = 1, y2 = 2, r1 = 30, r2 = 10 Output: The smaller circle lies completely inside the bigger circle without touching each other at a point of circumference. Input :x1 = 7, y1 = 8
6 min read
Check if any point overlaps the given Circle and Rectangle
Given two opposite diagonal points of a rectangle (X1, Y1), (X2, Y2) and the center, radius of the circle R, (Xc, Yc), the task is to check if there exists any point P that belongs to both the circle as well as the rectangle. Examples: Input: R = 2, Xc = 0, Yc = 0, X1 = 1, Y1 = 0, X2 = 3, Y2 = 3 Output: true Explanation: Clearly, from the below ill
7 min read
Count points from an array that lies inside a semi-circle
Given two pairs (X, Y), (P, Q) and R the coordinate of the center of semi-circle, coordinate of the intersection of semicircle and diameter of the semicircle and, the radius of the semicircle, and an array arr[] of dimension N*2 consisting of the coordinates of few points, the task is to find the number of points from the array that lies inside or
7 min read
Count of integral coordinates that lies inside a Square
Given lower left and upper right coordinates (x1, y1) and (x2, y2) of a square, the task is to count the number of integral coordinates that lies strictly inside the square.Examples: Input: x1 = 1, y1 = 1, x2 = 5, x3 = 5 Output: 9 Explanation: Below is the square for the given coordinates: Input: x1 = 1, y1 = 1, x2 = 4, x3 = 4 Output: 4 Approach: T
4 min read
Maximum area of a Rectangle that can be circumscribed about a given Rectangle of size LxW
Given a rectangle of dimensions L and W. The task is to find the maximum area of a rectangle that can be circumscribed about a given rectangle with dimensions L and W. Examples: Input: L = 10, W = 10Output: 200 Input: L = 18, W = 12Output: 450 Approach: Let below is the given rectangle EFGH of dimensions L and W. We have to find the area of rectang
4 min read
Coordinates of rectangle with given points lie inside
Given two arrays X[] and Y[] with n-elements, where (Xi, Yi) represent a point on coordinate system, find the smallest rectangle such that all points from given input lie inside that rectangle and sides of rectangle must be parallel to Coordinate axis. Print all four coordinates of an obtained rectangle. Note: n &gt;= 4 (we have at least 4 points a
6 min read
Ratio of area of a rectangle with the rectangle inscribed in it
Given two rectangles, X with a ratio of length to width a:b and Y with a ratio of length to width c:d respectively. Both the rectangles can be resized as long as the ratio of sides remains the same. The task is to place the second rectangle inside the first rectangle such that at least 1 side is equal and that side overlaps of both the rectangles a
7 min read
Largest subset of rectangles such that no rectangle fit in any other rectangle
Given height and width of N rectangles. The task is to find the size of the largest subset such that no pair of rectangles fit within each other. Note that if H1 ? H2 and W1 ? W2 then rectangle 1 fits inside rectangle 2. Examples: Input: arr[] = {{1, 3}, {2, 2}, {1, 3}} Output: 2 The required sub-set is {{1, 3}, {2, 2}} {1, 3} is included only once
15 min read
Count ways to reach target cell in a rectangle with restricted sub-rectangle
Given a grid of size M x N and two integers X and Y. A restricted area is defined as the lower left sub-rectangle consisting of X x Y blocks. Starting from the upper left cell find the number of ways to reach the bottom right cell where a legal move is defined to move right or down but never into the restricted area or outside the given grid.  Note
13 min read
Area of the largest Rectangle without a given point
Given the length L and breadth B of a rectangle and the position of a hole in the rectangle as (X, Y) coordinate, the task is to find the area of largest Rectangle within the given Rectangle such that it does not contain the hole.Note: The rectangle is placed at the origin by two of its side touching the Co-ordinate axis.Examples: Input: L = 8, B =
6 min read
Check if a point is inside, outside or on a Hyperbola
Given a hyperbola centered at (h, k), with semi-major axis a, semi-minor axis b, both aligned with the Cartesian plane, the task is to determine if the point (x, y) lies within the area bounded by the hyperbola or not. Examples: Input: h = 0, k = 0, x = 2, y = 1, a = 4, b = 5 Output: Inside Input: h = 1, k = 2, x = 200, y = 100, a = 6, b = 5Output:
5 min read
Check if a point is inside, outside or on the ellipse
Given an ellipse centered at (h, k), with semi-major axis a, and semi-minor axis b, both aligned with the Cartesian plane. The task is to determine if the point (x, y) is within the area bounded by the ellipse. Examples:  Input: h = 0, k = 0, x = 2, y = 1, a = 4, b = 5 Output: InsideInput: h = 1, k = 2, x = 200, y = 100, a = 6, b = 5Output: Outside
10 min read
Check if a point is inside, outside or on the parabola
Given a parabola with vertex (h, k), and [Tex]a     [/Tex], the distance between focus and vertex. The task is to determine if the point (x, y) is inside, outside or on the parabola.Examples:   Input: h = 100, k = 500, x = 20, y = 10, a = 4 Output: OutsideInput: h = 0, k = 0, x = 2, y = 1, a = 4Output: Inside     Approach: It is very simple, we hav
5 min read
Check whether the given floating point number is a palindrome
Given a floating-point number N, the task is to check whether it is palindrome or not. Input: N = 123.321 Output: YesInput: N = 122.1 Output: No Approach: First, convert the given floating-point number into a character array.Initialize the low to first index and high to the last index.While low &lt; high: If the character at low is not equal to the
4 min read
Check whether given floating point number is even or odd
Given a floating-point number, check whether it is even or odd. We can check whether a integer is even or odd by dividing its last digit by 2. But in case of floating point number we can't check a given number is even or odd by just dividing its last digit by 2. For example, 100.70 is an odd number but its last digit is divisible by 2. Examples : I
6 min read
Find the number of rectangles of size 2*1 which can be placed inside a rectangle of size n*m
Given two integers n, m. Find the number of rectangles of size 2*1 that can be placed inside a rectangle of size n*m. Note: No two small rectangles overlap.Each small rectangle lies entirely inside the large rectangle. It is allowed to touch the edges of the large rectangle. Examples: Input : n = 3, m =3 Output : 4 Input : n = 2, m = 4 Output : 4 A
6 min read
three90RightbarBannerImg