码迷,mamicode.com
首页 > 其他好文 > 详细

最小圆覆盖 gym-102006 I

时间:2018-12-29 23:10:42      阅读:347      评论:0      收藏:0      [点我收藏+]

标签:opera   span   tco   scanf   etc   using   space   clu   ++   

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long
using namespace std;

const int N = 2e5 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1.1e-4;
const  double PI = acos(-1);

struct Point {
    double x, y;
    Point(double x = 0, double y = 0) : x(x), y(y) { }
};
typedef Point Vector;

int dcmp(double x) {
    if(fabs(x) < eps) return 0;
    else return x < 0 ? -1 : 1;
}

Point operator + (Vector A, Vector B) {return Point(A.x + B.x, A.y + B.y);}
Point operator - (Vector A, Vector B) {return Point(A.x - B.x, A.y - B.y);}
Point operator * (Vector A, double p) {return Point(A.x * p, A.y * p);}
Point operator / (Vector A, double p) {return Point(A.x / p, A.y / p);}
bool operator < (const Vector &A, const Vector &B) {return A.y < B.y || (A.y == B.y && A.x < B.x);}
bool operator == (const Vector &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 Area2(Point A, Point B, Point C) {return Cross(B - A, C - A);}

Vector Rotate(Vector A, double rad) {
    return Vector(A.x*cos(rad)-A.y*sin(rad), A.x*sin(rad)+A.y*cos(rad));
}

double dist(const Point& a, const Point &b) {
    return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
}

int n;
double r, R;
Point p[N];

Point GetCircleCenter(Point A, Point B, Point C) {
    Point o;
    double a1 = B.x-A.x, b1 = B.y-A.y, c1 = (a1*a1+b1*b1)/2;
    double a2 = C.x-A.x, b2 = C.y-A.y, c2 = (a2*a2+b2*b2)/2;
    double d = a1*b2-a2*b1;
    o.x=A.x+(c1*b2-c2*b1)/d;
    o.y=A.y+(a1*c2-a2*c1)/d;
    return o;
}

void MinPointCoverByCircle(Point *p, int n, Point &o, double &r) {
    random_shuffle(p, p+n);
    o = p[0], r = 0;
    for(int i = 1; i < n; i++) {
        if(dist(p[i], o) > r + eps) {
            o = p[i]; r = 0;
            for(int j = 0; j < i; j++) {
                if(dist(p[j], o) > r + eps) {
                    o.x = (p[i].x+p[j].x)/2;
                    o.y = (p[i].y+p[j].y)/2;
                    r = dist(p[j], o);
                    for(int k = 0; k < j; k++) {
                        if(dist(p[k], o) > r + eps) {
                            o = GetCircleCenter(p[i], p[j], p[k]);
                            r = dist(p[i], o);
                        }
                    }
                }
            }
        }
    }
}

int main() {
//    freopen("robots.in", "r", stdin);
    int T; scanf("%d", &T);
    while(T--) {
        scanf("%d%lf%lf", &n, &R, &r); n++;
        p[0] = Point(0, 0);
        for(int i = 1; i < n; i++) {
            double x, y; scanf("%lf%lf", &x, &y);
            p[i] = p[i-1] + Point(x, y);
        }
        sort(p, p+n);
        n = unique(p, p+n)-p;
        Point o;
        MinPointCoverByCircle(p, n, o, r);
        printf("%.12f %.12f\n", -o.x, -o.y);
    }
    return 0;
}
/*
*/

 

最小圆覆盖 gym-102006 I

标签:opera   span   tco   scanf   etc   using   space   clu   ++   

原文地址:https://www.cnblogs.com/CJLHY/p/10197821.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!