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;
}