uva 11177(凸多边形和圆的相交)

来源:互联网 发布:centos tmp目录 编辑:程序博客网 时间:2024/05/19 23:09

题意:按顺时针或逆时针顺序给出一个凸n边形的n个点的坐标,然后让一个圆心在(0,0)的圆和凸n边形相交的面积大于等于R,问圆的最小半径。
题解:这题简直坑爹啊,各种细节错误。。修修改改了一天,最后看别人题解也还是不懂为什么OnSegment函数要写成那样。。。明明不能判断点是否在线段上 ╮(╯▽╰)╭
画画图思路不难想到,把凸n边形的每条边都和圆判断关系,如果是边的两点都在圆内,两条边对应一个三角形的面积,如果一个点在圆外一个在圆内(包括边界),那就是一个三角形加一个扇形,如果两点都在圆外,分两种情况,和圆有两个交点的话是两个扇形和一个三角形,如果和圆无交点是一个扇形。

#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <cmath>using namespace std;const double eps = 1e-9;const double PI = acos(-1);int dcmp(double x) {    if (fabs(x) < eps)        return 0;    return x > 0 ? 1 : -1;}struct Point {    double x, y;    Point (double a = 0, double b = 0): x(a), y(b) {}};typedef Point Vector;Vector operator + (const Vector& a, const Vector& b) { return Vector(a.x + b.x, a.y + b.y); }Vector operator - (const Vector& a, const Vector& b) { return Vector(a.x - b.x, a.y - b.y); }Vector operator * (const Vector& a, double& b) { return Vector(a.x * b, a.y * b); }Vector operator / (const Vector& a, double& b) { return Vector(a.x / b, a.y / b); }bool operator == (const Vector& a, const Vector& b) { return !dcmp(a.x - b.x) && !dcmp(a.y - b.y); }bool operator < (const Vector& a, const Vector& b) { return a.x < b.x || (a.x == b.x && a.y < b.y); }double Dot(const Vector& a, const Vector& b) { return a.x * b.x + a.y * b.y; }double Length(const Vector& a) { return sqrt(Dot(a, a)); }double Cross(const Vector& a, const Vector& b) { return a.x * b.y - a.y * b.x; }double Angle(const Vector& a, const Vector& b) { return acos(Dot(a, b) / Length(a) / Length(b)); }struct Line {    Point p;    Vector v;    double ang;    Line() {}    Line(Point a, Vector b): p(a), v(b) { ang = atan2(b.y, b.x); }    bool operator < (const Line& L) const { return ang < L.ang; }    Point point(double a) { return p + v * a; }};struct Circle {    double r;    Point c;    Circle() {}    Circle(Point a, double b): c(a), r(b) {}    Point point(double a) { return Point(c.x + cos(a) * r, c.y + sin(a) * r); }};bool isPointInCircle(Point P, Circle C) {    return dcmp(Length(C.c - P) - C.r) <= 0;}bool OnSegment(Point p, Point p1, Point p2) {    return dcmp(Dot(p1 - p, p2 - p)) <= 0;//这里很费解}void SegmentCircleIntersection(Point p1, Point p2, Circle C, Point* sol, int& num) {    double t1, t2;    double a = (p1 - C.c).x, b = (p2 - p1).x, c = (p1 - C.c).y, d = (p2 - p1).y;    double e = b * b + d * d, f = 2 * (a * b + c * d), g = a * a + c * c - C.r * C.r;    double delta = f * f - 4 * e * g;    Point p;    num = 0;    if (dcmp(delta) < 0)        return;    else if (dcmp(delta) == 0) {        t1 = t2 = (-f) / (2 * e);        p = p1 + (p2 - p1) * t1;        if (OnSegment(p, p1, p2))            sol[num++] = p;    }    else {        t1 = (-f - sqrt(delta)) / (2 * e);        p = p1 + (p2 - p1) * t1;        if (OnSegment(p, p1, p2))            sol[num++] = p;        t2 = (-f + sqrt(delta)) / (2 * e);        p = p1 + (p2 - p1) * t2;        if (OnSegment(p, p1, p2))            sol[num++] = p;    }}double FanArea(Circle C, Vector v1, Vector v2) {    double rad = Angle(v1, v2);    if (dcmp(rad - PI) == 0)        rad = 0;    return C.r * C.r * rad * 0.5;}double GetCommonArea(Point p1, Point p2, Point p3, Circle C) {    int flag1 = isPointInCircle(p2, C);    int flag2 = isPointInCircle(p3, C);    int num;    Point sol[5];    if (flag1 + flag2 == 2)        return fabs(Cross(p2 - p1, p3 - p1)) * 0.5;    if (flag1 + flag2 == 1) {        SegmentCircleIntersection(p2, p3, C, sol, num);        if (flag1)            return FanArea(C, p3 - p1, sol[0] - p1) + fabs(Cross(p2 - p1, sol[0] - p1)) * 0.5;        return FanArea(C, p2 - p1, sol[0] - p1) + fabs(Cross(p3 - p1, sol[0] - p1)) * 0.5;    }    SegmentCircleIntersection(p2, p3, C, sol, num);//注意两个端点对应不同交点,写反就错了    if (num == 2)        return FanArea(C, p2 - p1, sol[0] - p1) + fabs(Cross(sol[0] - p1, sol[1] - p1)) * 0.5 + FanArea(C, p3 - p1, sol[1] - p1);    return FanArea(C, p2 - p1, p3 - p1);}const int N = 55;Point P[N];double R;int n;double CommonArea(Circle C) {    double res = 0;    for (int i = 0; i < n; i++)        res += GetCommonArea(C.c, P[i], P[i + 1], C);    return res;}int main() {    int cas = 1;    while (scanf("%d", &n) == 1 && n) {        scanf("%lf", &R);        for (int i = 0; i < n; i++)            scanf("%lf%lf", &P[i].x, &P[i].y);        P[n] = P[0];        double l = 0, r = 1e9;        while (dcmp(r - l) > 0) {            double mid = (l + r) * 0.5;            if (dcmp(CommonArea(Circle(Point(0, 0), mid)) - R) >= 0)                r = mid;            else                l = mid;        }        printf("Case %d: %.2lf\n", cas++, l);    }    return 0;}
0 0