📌  相关文章
📜  检查给定点是否位于矩形上或矩形内。套装3

📅  最后修改于: 2021-04-29 03:14:38             🧑  作者: Mango

给定两个数字a和b,其中b 例子:

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

方法:一种有效的方法是形成一个矩形的4线方程。然后,如果给定点位于矩形中或矩形上,则当且仅当将给定点替换为:

  1. 右侧边线( xyb = 0 )的值必须小于或等于零。
  2. 左侧边线( x-y + a = 0 )必须提供大于或等于1的值。
  3. 上边线( x + y-2 * a + b = 0 )必须使值小于或等于零。
  4. 下边线( x + yb = 0 )必须提供大于或等于1的值。
C++
// C++ program to Check whether a given point
// lies inside or on the rectangle or not
#include 
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;
}


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


输出:
Given point lies inside the rectangle