给定一个矩形和三个圆形,问矩形上有没有没被圆形覆盖住的地方
我们在矩形上画很多条平行的线段,然后再看每条线段有没有没被圆形覆盖住的地方
判断每条线段有没有没被圆形覆盖住的地方的方法是,直接求圆与直线的交点,然后把所有的交点连通线段的两个顶点排序,相邻两个点之间的段上的点一定是同状态的,即同属于某圆或同不属于某圆,所以直接取相邻点的中点,若每个在线段内的中点都在某个圆内,则整条矩形都在某个圆内
#include <cstdio>
#include <algorithm>
#include <cmath>
const double eps=1e-9;
using namespace std;
struct Circle {
double x,y,r;
int read() {
return scanf("%lf%lf%lf",&x,&y,&r);
}
};
struct Point {
double x,y;
Point() {}
Point(double xx,double yy) {
x=xx;y=yy;
}
};
double dis(Point a,Point b) {
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
Circle a,b,c;
double x0,yy0,x1,yy1;
bool notIn(double y) {
double x[8];
double delta,bb,cc;
int xn=2;
x[0]=x0;
x[1]=x1;
bb=-2*a.x;
cc=-a.r*a.r+(a.y-y)*(a.y-y)+a.x*a.x;
delta=bb*bb-4*cc;
if (delta>eps) {
x[xn++]=(-bb+sqrt(delta))/2;
x[xn++]=(-bb-sqrt(delta))/2;
} else x[xn++]=-bb/2;
bb=-2*b.x;
cc=-b.r*b.r+(b.y-y)*(b.y-y)+b.x*b.x;
delta=bb*bb-4*cc;
if (delta>eps) {
x[xn++]=(-bb+sqrt(delta))/2;
x[xn++]=(-bb-sqrt(delta))/2;
} else x[xn++]=-bb/2;
bb=-2*c.x;
cc=-c.r*c.r+(c.y-y)*(c.y-y)+c.x*c.x;
delta=bb*bb-4*cc;
if (delta>eps) {
x[xn++]=(-bb+sqrt(delta))/2;
x[xn++]=(-bb-sqrt(delta))/2;
} else x[xn++]=-bb/2;
sort(x,x+xn);
for (int i=1;i<xn;i++) {
double t=(x[i-1]+x[i])/2;
if (t>x0-eps&&t<x1+eps)
if (dis(Point(t,y),Point(a.x,a.y))>a.r+eps
&&dis(Point(t,y),Point(b.x,b.y))>b.r+eps
&&dis(Point(t,y),Point(c.x,c.y))>c.r+eps)
return true;
}
return false;
}
int main() {
bool ans;
while (a.read()!=EOF) {
b.read();
c.read();
scanf("%lf%lf%lf%lf",&x0,&yy1,&x1,&yy0);
ans=false;
for (double y=yy0;y<=yy1;y+=0.001) {
if (notIn(y)) {
ans=true;
break;
}
}
if (ans) printf("Yes\n");
else printf("No\n");
}
return 0;
}