📜  检查是否有任何点与给定的 Circle 和 Rectangle 重叠

📅  最后修改于: 2021-10-23 09:10:29             🧑  作者: Mango

给定矩形的两个对角点(X1, Y1), (X2, Y2)和圆心,圆的半径R, (Xc, Yc) ,任务是检查是否存在属于两者的任何点P圆形和矩形一样。
例子:

方法:这个想法是简单地检查圆和矩形是否相交。发生交叉时基本上有两种可能的情况。

  1. 情况 1:矩形的边与圆接触或相交。为了检查形状是否相交,我们需要在最靠近圆心的矩形上或矩形内找到一个点。如果该点位于圆上或圆内,则保证两个形状相交。让最近的点表示为(Xn, Yn) 。然后可以使用sqrt((Xc- Xn) 2 + (Yc-Yn) 2 )找到最近点和圆心之间的距离。如果此距离≤ 圆的半径,则两个形状相交。
  2. 情况 2:圆心在矩形内。由于圆心位于矩形内部,因此最近的点将是 (Xc, Yc)。

仔细观察,可以观察到兴趣点仅取决于(X1,Y1)和(X2,Y2)相对于(Xc,Yc)的位置。因此,上述两种情况下的最近点都可以计算为:

  1. Xn= max(X1, min(Xc, X2))
  2. Yn= max(Y1, min(Yc, Y2))

下面是上述方法的实现:

C++
// C++ implementation to check if any
// point overlaps the given circle
// and rectangle
#include
using namespace std;
 
// Function to check if any point
// overlaps the given circle
// and rectangle
bool checkOverlap(int R, int Xc, int Yc,
                         int X1, int Y1,
                         int X2, int Y2)
{
 
    // Find the nearest point on the
    // rectangle to the center of
    // the circle
    int Xn = max(X1, min(Xc, X2));
    int Yn = max(Y1, min(Yc, Y2));
     
    // Find the distance between the
    // nearest point and the center
    // of the circle
    // Distance between 2 points,
    // (x1, y1) & (x2, y2) in
    // 2D Euclidean space is
    // ((x1-x2)**2 + (y1-y2)**2)**0.5
    int Dx = Xn - Xc;
    int Dy = Yn - Yc;
    return (Dx * Dx + Dy * Dy) <= R * R;
}
 
// Driver code
int main()
{
    int R = 1;
    int Xc = 0, Yc = 0;
    int X1 = 1, Y1 = -1;
    int X2 = 3, Y2 = 1;
     
    if(checkOverlap(R, Xc, Yc,
                       X1, Y1,
                       X2, Y2))
    {
        cout << "True" << endl;
    }
    else
    {
        cout << "False";
    }
}
 
// This code is contributed by BhupendraSingh


Java
// Java implementation to check if any
// point overlaps the given circle
// and rectangle
class GFG{
 
// Function to check if any point
// overlaps the given circle
// and rectangle
static boolean checkOverlap(int R, int Xc, int Yc,
                                   int X1, int Y1,
                                   int X2, int Y2)
{
 
    // Find the nearest point on the
    // rectangle to the center of
    // the circle
    int Xn = Math.max(X1, Math.min(Xc, X2));
    int Yn = Math.max(Y1, Math.min(Yc, Y2));
     
    // Find the distance between the
    // nearest point and the center
    // of the circle
    // Distance between 2 points,
    // (x1, y1) & (x2, y2) in
    // 2D Euclidean space is
    // ((x1-x2)**2 + (y1-y2)**2)**0.5
    int Dx = Xn - Xc;
    int Dy = Yn - Yc;
    return (Dx * Dx + Dy * Dy) <= R * R;
}
 
// Driver code
public static void main(String[] args)
{
    int R = 1;
    int Xc = 0, Yc = 0;
    int X1 = 1, Y1 = -1;
    int X2 = 3, Y2 = 1;
     
    if(checkOverlap(R, Xc, Yc,
                       X1, Y1,
                       X2, Y2))
    {
        System.out.print("True" + "\n");
    }
    else
    {
        System.out.print("False");
    }
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation to check if any
# point overlaps the given Circle
# and Rectangle
 
# Function to check if any point
# overlaps the given Circle
# and Rectangle
def checkOverlap(R, Xc, Yc, X1, Y1, X2, Y2):
 
    # Find the nearest point on the
    # rectangle to the center of
    # the circle
    Xn = max(X1, min(Xc, X2))
    Yn = max(Y1, min(Yc, Y2))
     
    # Find the distance between the
    # nearest point and the center
    # of the circle
    # Distance between 2 points,
    # (x1, y1) & (x2, y2) in
    # 2D Euclidean space is
    # ((x1-x2)**2 + (y1-y2)**2)**0.5
    Dx = Xn - Xc
    Dy = Yn - Yc
    return (Dx**2 + Dy**2) <= R**2
 
# Driver code
if(__name__ == "__main__"):
    R = 1
    Xc, Yc = 0, 0
    X1, Y1 = 1, -1
    X2, Y2 = 3, 1
     
    print(checkOverlap(R, Xc, Yc, X1, Y1, X2, Y2))


C#
// C# implementation to check if any
// point overlaps the given circle
// and rectangle
using System;
class GFG{
 
// Function to check if any point
// overlaps the given circle
// and rectangle
static bool checkOverlap(int R, int Xc, int Yc,
                                int X1, int Y1,
                                int X2, int Y2)
{
 
    // Find the nearest point on the
    // rectangle to the center of
    // the circle
    int Xn = Math.Max(X1,
             Math.Min(Xc, X2));
    int Yn = Math.Max(Y1,
             Math.Min(Yc, Y2));
     
    // Find the distance between the
    // nearest point and the center
    // of the circle
    // Distance between 2 points,
    // (x1, y1) & (x2, y2) in
    // 2D Euclidean space is
    // ((x1-x2)**2 + (y1-y2)**2)**0.5
    int Dx = Xn - Xc;
    int Dy = Yn - Yc;
    return (Dx * Dx + Dy * Dy) <= R * R;
}
 
// Driver code
public static void Main()
{
    int R = 1;
    int Xc = 0, Yc = 0;
    int X1 = 1, Y1 = -1;
    int X2 = 3, Y2 = 1;
     
    if(checkOverlap(R, Xc, Yc,
                       X1, Y1,
                       X2, Y2))
    {
        Console.Write("True" + "\n");
    }
    else
    {
        Console.Write("False");
    }
}
}
 
// This code is contributed by Nidhi_biet


Javascript


输出:
True