📌  相关文章
📜  检查是否有任何点与给定的圆和矩形重叠

📅  最后修改于: 2021-05-04 13:43:59             🧑  作者: Mango

给定矩形(X1,Y1)(X2,Y2)的两个对角线对角点以及圆R的中心半径(Xc,Yc) ,任务是检查是否存在属于两个点的点P圆和矩形。

例子:

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

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

  2. 情况2:圆心位于矩形内。由于圆的中心位于矩形内部,因此最接近的点将是(Xc,Yc)。

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

  1. Xn =最大值(X1,最小值(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


输出:
True