📜  检查点是否位于矩形内|套装2

📅  最后修改于: 2021-04-30 03:10:46             🧑  作者: Mango

给定矩形左下角和右上角的坐标。检查点(x,y)是否位于此矩形内。
例子:

在上一篇文章中已经讨论了这个问题。在这篇文章中,我们讨论了一种新方法。
方法:以上问题可以通过观察解决。当且仅当它的x坐标位于给定矩形的右下角和左上角坐标的x坐标之间并且y坐标位于给定的下半角的y坐标之间时,点才位于矩形的内部或不位于矩形的内部-右和左上角坐标。
下面是上述方法的实现:

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 assigment
    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


输出:
Yes