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

📅  最后修改于: 2021-10-23 08:20:04             🧑  作者: 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