链接:今天去西湖了
各种WA,后来没办法,改了一下计算点到线段的距离的那个函数,因为矩形的边是平行于坐标轴的,所以计算点到矩形边的距离变得十分容易。
思路:首先排除所有点在圆内部的特殊情况,然后计算圆心到所有点的最短距离,全部都在圆的外面,说明圆是在矩形的内部,否则,与圆相交
#include <iostream>
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include<stdio.h>
#include<math.h>
using namespace std;
double PointToSegDist(double x, double y, double x1, double y1, double x2, double y2)
{
if(y1 == y2)
{
if(x1 > x2) swap(x1, x2);
if(x < x1) return (x - x1) * (x - x1) + (y - y1) * (y - y1);
if(x > x2) return (x - x2) * (x - x2) + (y - y2) * (y - y2);
return fabs(y - y1) * fabs(y - y1);
}
else if(x1 == x2)
{
if(y1 > y2) swap(y1, y2);
if(y > y2) return (x - x1) * (x - x1) + (y - y2) * (y - y2);
if(y < y1) return (x - x1) * (x - x1) + (y - y1) * (y - y1);
return fabs(x - x1) * fabs(x - x1);
}
return 0;
}
double ptop(double x1, double y1, double x2, double y2)
{
return ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
int main()
{
int t;
while(scanf("%d", &t) != EOF)
{
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);
double d1, d2, d3, d4;
d1 = ptop(x, y, x1, y1);
d2 = ptop(x, y, x2, y2);
d3 = ptop(x, y, x1, y2);
d4 = ptop(x, y, x2, y1);
if(d1 < r * r && d2 < r * r && d3 < r * r && d4 < r * r)
{
puts("NO");
continue;
}
d1 = PointToSegDist(x, y, x1, y1, x1, y2);
d2 = PointToSegDist(x, y, x1, y1, x2, y1);
d3 = PointToSegDist(x, y, x1, y2, x2, y2);
d4 = PointToSegDist(x, y, x2, y1, x2, y2);
if(d1 > r * r && d2 > r * r && d3 > r * r && d4 > r * r)
puts("NO");
else puts("YES");
}
}
return 0;
}