UVALive 4428 Solar Eclipse --计算几何,圆相交

时间:2022-01-12 08:19:32

题意:平面上有一些半径为R的圆,现在要在满足不与现有圆相交的条件下放入一个圆,求这个圆能放的位置的圆心到原点的最短距离。

解法:我们将半径扩大一倍,R = 2*R,那么在每个圆上或圆外的位置都可以放圆心了。

首先特判放到原点可不可以,如果不可以,再将所有圆的圆心与原点的直线与该圆相交的点放入队列,再将所有圆两两相交的点放入队列,然后处理整个队列,一一判断这些点行不行,可以证明,最优点一定在这些里面。

如果有一个圆的圆心在(0,0)点,那么要特判一下,因为此时圆心与原点连的直线长度为0,对于这种情况,我们判一下(R,0)这个就行了。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#define Mod 1000000007
#define eps 1e-7
using namespace std; struct Point{
double x,y;
Point(double x=, double y=):x(x),y(y) {}
void input() { scanf("%lf%lf",&x,&y); }
};
typedef Point Vector;
struct Circle{
Point c;
double r;
Circle(){}
Circle(Point c,double r):c(c),r(r) {}
Point point(double a) { return Point(c.x + cos(a)*r, c.y + sin(a)*r); }
void input() { scanf("%lf%lf%lf",&c.x,&c.y,&r); }
};
int dcmp(double x) {
if(x < -eps) return -;
if(x > eps) return ;
return ;
}
template <class T> T sqr(T x) { return x * x;}
Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
Vector operator - (Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); }
Vector operator * (Vector A, double p) { return Vector(A.x*p, A.y*p); }
Vector operator / (Vector A, double p) { return Vector(A.x/p, A.y/p); }
bool operator < (const Point& a, const Point& b) { return a.x < b.x || (a.x == b.x && a.y < b.y); }
bool operator >= (const Point& a, const Point& b) { return a.x >= b.x && a.y >= b.y; }
bool operator <= (const Point& a, const Point& b) { return a.x <= b.x && a.y <= b.y; }
bool operator == (const Point& a, const Point& b) { return dcmp(a.x-b.x) == && dcmp(a.y-b.y) == ; }
double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; }
double Length(Vector A) { return sqrt(Dot(A, A)); }
double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); }
double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; }
double angle(Vector v) { return atan2(v.y, v.x); } bool InCircle(Point x, Circle c) { return dcmp(c.r - Length(c.c-x)) > ; } //not in border
int GetCircleCircleIntersection(Circle C1, Circle C2, vector<Point>& sol) //return 交点个数
{
double d = Length(C1.c - C2.c);
if(dcmp(d) == ){
if(dcmp(C1.r - C2.r) == ) return -; //两圆重合
return ;
}
if(dcmp(C1.r + C2.r - d) < ) return ;
if(dcmp(fabs(C1.r - C2.r) - d) > ) return ; double a = angle(C2.c - C1.c); //向量C1C2的极角
double da = acos((sqr(C1.r) + sqr(d) - sqr(C2.r)) / (*C1.r*d)); //C1C2到C1P1的极角 Point p1 = C1.point(a-da), p2 = C1.point(a+da);
sol.push_back(p1);
if(p1 == p2) return ;
sol.push_back(p2);
return ;
}
double DISP(Point p) { return sqrt(p.x*p.x+p.y*p.y); } Circle C[],sC[];
int n; bool check(Point now) {
for(int i=;i<=n;i++) {
if(InCircle(now,C[i]))
return false;
}
return true;
} int main()
{
int i,j;
double R;
while(scanf("%d%lf",&n,&R)!=EOF && n+R)
{
for(i=;i<=n;i++)
{
scanf("%lf%lf",&C[i].c.x,&C[i].c.y), C[i].r = 2.0*R;
sC[i] = C[i], sC[i].r = R;
}
vector<Point> sec;
sec.clear();
for(i=;i<=n;i++) {
for(j=i+;j<=n;j++)
GetCircleCircleIntersection(C[i],C[j],sec);
}
double Mini = Mod;
if(check(Point(,))) { printf("%.6f\n",0.0); continue; }
for(i=;i<=n;i++) {
if(dcmp(DISP(C[i].c)) == ) {
if(check(Point(*R,))) Mini = min(Mini,*R);
continue;
}
sec.push_back(Point(C[i].c+C[i].c*(-2.0*R/DISP(C[i].c))));
sec.push_back(Point(C[i].c+C[i].c*(2.0*R/DISP(C[i].c))));
}
for(i=;i<sec.size();i++)
if(check(sec[i])) Mini = min(Mini,DISP(sec[i]));
printf("%.6f\n",Mini);
}
return ;
}