HDU 1221 Rectangle and Circle(判断圆和矩形是不是相交)

本文介绍了一种判断矩形与圆是否相交的方法,通过计算圆心到矩形的最短距离和最长距离来确定相交状态,并提供了一个具体的实现代码。

传送门:

http://acm.hdu.edu.cn/showproblem.php?pid=1221

Rectangle and Circle

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3434    Accepted Submission(s): 904


Problem Description
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
 

 

Author
weigang Lee
 

 

Source
 

 

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

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

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

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

    最长距离:圆心到四个点距离的最大值
 
    注意矩形是平行xy轴的,计算方便了很多
 
code:
#include<bits/stdc++.h>
using namespace std;
double dis(double x1,double y1,double x2,double y2)
{
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
double f(double x,double y,double x1,double y1,double x2,double y2)//圆心到矩形边的距离最小值
{
    if(x1==x2)
    {
        if(y<=max(y1,y2)&&y>=min(y1,y2))
        {
            return fabs(x-x1);
        }else
        {
        double b=dis(x,y,x1,y1);
        double c=dis(x,y,x2,y2);
        double result=min(b,c);
        return result;
        }
    }else if(y1==y2)
    {
        if(x<=max(x1,x2)&&x>=min(x1,x2))
        {
            return fabs(y-y1);
        }else
        {
        double b=dis(x,y,x1,y1);
        double c=dis(x,y,x2,y2);
        double result=min(b,c);
        return result;
        }
    }
}
int main()
{
    double x,y,r,x1,y1,x2,y2;
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lf %lf %lf %lf %lf %lf %lf",&x,&y,&r,&x1,&y1,&x2,&y2);
        double x3=x1,y3=y2;
        double x4=x2,y4=y1;
        double l1=f(x,y,x1,y1,x3,y3);
        double l2=f(x,y,x1,y1,x4,y4);
        double l3=f(x,y,x2,y2,x3,y3);
        double l4=f(x,y,x2,y2,x4,y4);

        double L=min(l1,min(l2,min(l3,l4)));

        double r1=dis(x,y,x1,y1);
        double r2=dis(x,y,x2,y2);
        double r3=dis(x,y,x3,y3);
        double r4=dis(x,y,x4,y4);

        double R=max(r1,max(r2,max(r3,r4)));

        if(L>r)
            printf("NO\n");
        else if(R<r)
            printf("NO\n");
        else if(L<=r&&R>=r)
            printf("YES\n");
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/yinbiao/p/9326515.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值