1221-Rectangle and Circle(图形几何) HDU

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

思路

 矩形与圆不相交有三种情况

1.矩形包含于圆内(矩形四个顶点到圆心的距离都小于r)

2.圆包含于矩形内(圆心的范围为圆包含在矩形内时沿边矩形界滚动圆心形成的轨迹)

3.圆和矩形相离(圆心到平行于矩形中心的x坐标或y坐标所在的直线垂直距离都大于圆的半径)

代码

#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
struct node{
  double x;
  double y;
  double r;
}c,ld,lu,rd,ru,cre;
//定义6个对象,分别为圆、矩形左下顶点、左上顶点、右下顶点、右上顶点、矩阵中心
//计算两点间距离函数
double dis(node a,node b) 
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lf%lf%lf%lf%lf%lf%lf",&c.x,&c.y,&c.r,&ld.x,&ld.y,&ru.x,&ru.y);
        cre.x=(ld.x+ru.x)/2; cre.y=(ld.y+ru.y)/2;
        if(ld.x>ru.x) swap(ld,ru);
        lu.x=ld.x; lu.y=ru.y;
        rd.x=ru.x; rd.y=ld.y;
        //四个顶点到圆心的距离全部小于半径(矩形包含于圆内,矩形四个顶点到圆心的距离都小于r)
        if(dis(c,ld)<c.r&&dis(c,lu)<c.r&&dis(c,ru)<c.r&&dis(c,rd)<c.r)
            printf("NO\n");
        //圆心的范围为圆包含在矩形内时沿矩形边界滚动形成的轨迹
        else if(c.x>ld.x+c.r&&c.x<ru.x-c.r&&c.y>ld.y+c.r&&c.y<ru.y-c.r)
            printf("NO\n");
        //矩形与圆相离(圆心到平行于矩形中心的x坐标或y坐标所在的直线垂直距离都大于圆的半径)
        else if(fabs(ld.y-c.y)>c.r&&fabs(ru.y-c.y)>c.r||fabs(ld.x-c.x)>c.r&&fabs(ru.x-c.x)>c.r)
            printf("NO\n");
        else
            printf("YES\n");
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值