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

#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=0, double y=0):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 -1;
if(x > eps) return 1;
return 0;
}
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) == 0 && dcmp(a.y-b.y) == 0; }
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)) > 0; } //not in border
int GetCircleCircleIntersection(Circle C1, Circle C2, vector<Point>& sol)  //return 交点个数
{
double d = Length(C1.c - C2.c);
if(dcmp(d) == 0){
if(dcmp(C1.r - C2.r) == 0) return -1;  //两圆重合
return 0;
}
if(dcmp(C1.r + C2.r - d) < 0) return 0;
if(dcmp(fabs(C1.r - C2.r) - d) > 0) return 0;

double a = angle(C2.c - C1.c);             //向量C1C2的极角
double da = acos((sqr(C1.r) + sqr(d) - sqr(C2.r)) / (2*C1.r*d)); //C1C2到C1P1的极角

Point p1 = C1.point(a-da), p2 = C1.point(a+da);
sol.push_back(p1);
if(p1 == p2) return 1;
sol.push_back(p2);
return 2;
}
double DISP(Point p) { return sqrt(p.x*p.x+p.y*p.y); }

Circle C[106],sC[106];
int n;

bool check(Point now) {
for(int i=1;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=1;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=1;i<=n;i++) {
for(j=i+1;j<=n;j++)
GetCircleCircleIntersection(C[i],C[j],sec);
}
double Mini = Mod;
if(check(Point(0,0))) { printf("%.6f\n",0.0); continue; }
for(i=1;i<=n;i++) {
if(dcmp(DISP(C[i].c)) == 0) {
if(check(Point(2*R,0))) Mini = min(Mini,2*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=0;i<sec.size();i++)
if(check(sec[i])) Mini = min(Mini,DISP(sec[i]));
printf("%.6f\n",Mini);
}
return 0;
}
View Code

posted @ 2015-01-20 19:00 whatbeg 阅读(...) 评论(...) 编辑 收藏