Thursday, January 16, 2025
Google search engine
HomeData Modelling & AICheck whether a given point lies on or inside the rectangle |...

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

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.

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 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments