Fork me on GitHub

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

题意:平面上有一些半径为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=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  阅读(263)  评论(0编辑  收藏  举报