Rectangle and Circle

该问题要求根据输入的矩形和圆的坐标信息判断它们的边界是否相交,包括相切的情况。输入包含圆心坐标、半径以及矩形对角线的两个点坐标,输出为"YES"或"NO",表示相交或不相交。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given a rectangle and a circle in the coordinate system(two edges of the rectangle are parallel with the X-axis, and the other two are parallel with the Y-axis), you have to tell if their borders intersect. 

Note: we call them intersect even if they are just tangent. The circle is located by its centre and radius, and the rectangle is located by one of its diagonal. 

 
Input

The first line of input is a positive integer P which indicates the number of test cases. Then P test cases follow. Each test cases consists of seven real numbers, they are X,Y,R,X1,Y1,X2,Y2. That means the centre of a circle is (X,Y) and the radius of the circle is R, and one of the rectangle's diagonal is (X1,Y1)-(X2,Y2). 
 
Output

For each test case, if the rectangle and the circle intersects, just output "YES" in a single line, or you should output "NO" in a single line. 
 
Sample Input

 2
1 1 1 1 2 4 3
1 1 1 1 3 4 4.5 
 
Sample Output

 YES
NO 
 

   判断圆和矩形是不是相交的
 
    给出圆心点,半径,矩形对角线两点
 
    我们只要求出圆心到矩形的最短距离L和圆心到矩形的最长距离R.

    如果L>r(r为圆半径),圆肯定与矩形不相交.

    如果R<r,圆包含了矩形,依然与矩形不相交.

    如果L<=r且R>=r,那么圆肯定与矩形相交.
 
    最短距离 圆心到边上点的距离的最小值

    最长距离:圆心到四个点距离的最大值

#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
double getdis(double x,double y,double a,double b){
return sqrt(pow(x-a,2)+pow(y-b,2));
}
int main()
{

    int t;cin>>t;
    while(t--)
    {
        double X,Y,R,X1,Y1,X2,Y2;
        scanf("%lf%lf%lf%lf%lf%lf%lf",&X,&Y,&R,&X1,&Y1,&X2,&Y2);
        if(X1>X2){
            swap(X1,X2);swap(Y1,Y2);//让X1,Y1始终位于左边
        }
        double maxL,minL;
        if(Y1>Y2){
            maxL=max(getdis(X,Y,X1,Y1),getdis(X,Y,X2,Y2));
            maxL=max(maxL,getdis(X,Y,X1,Y1-fabs(Y1-Y2)));
            maxL=max(maxL,getdis(X,Y,X2,Y2+fabs(Y1-Y2)));
            if(maxL<R){
                printf("NO\n");continue;
            }

            if(X>=X1&&X<=X2){
                minL=min(fabs(Y-Y1),fabs(Y-Y2));
            }else if(Y>=Y2&&Y<=Y1)
            {
                minL=min(fabs(X-X1),fabs(X-X2));
            }else{
                minL=min(min(min(getdis(X,Y,X1,Y1),getdis(X,Y,X2,Y2)),getdis(X,Y,X2,Y2+fabs(Y1-Y2))),getdis(X,Y,X1,Y1-fabs(Y1-Y2)));
            }

            if(minL>R){
                printf("NO\n");continue;
            }
            else{
                printf("YES\n");
            }
        }else{
            maxL=max(getdis(X,Y,X1,Y1),getdis(X,Y,X2,Y2));
            maxL=max(maxL,getdis(X,Y,X1,Y1+fabs(Y1-Y2)));
            maxL=max(maxL,getdis(X,Y,X2,Y2-fabs(Y1-Y2)));
            if(maxL<R){
                printf("NO\n");continue;
            }

            if(X>=X1&&X<=X2){
                minL=min(fabs(Y-Y1),fabs(Y-Y2));
            }else if(Y>=Y1&&Y<=Y2)
            {
                minL=min(fabs(X-X1),fabs(X-X2));
            }else{
                minL=min(min(min(getdis(X,Y,X1,Y1),getdis(X,Y,X2,Y2)),getdis(X,Y,X2,Y2-fabs(Y1-Y2))),getdis(X,Y,X1,Y1+fabs(Y1-Y2)));
            }

            if(minL>R){
                printf("NO\n");continue;
            }
            else{
                printf("YES\n");
            }
        }

    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值