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