POJ 3384 Feng Shui(半平面交)

题意 : 给你一个凸多边形,让你在其中找两个圆,使得圆的覆盖面积最大。

这个题目和 poj 3525 有点类似,那个题目是一个圆,想到两者的联系,可以发现两个圆覆盖面积最大就是重叠面积最小,怎样使得重叠面积最小呢 ? 肯定就是两个圆心的圆心距最大,这样就可以了,先将边向内部缩 R (半径大小)求一个半平面交,然后在半平面交上找到距离最远的两个点就可以了。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#define PI 3.1415926

using namespace std;
#define N 1005
const double eps=1e-8;
int dcmp(double x) {
    if (x<=eps&&x>=-eps) return 0;
    return (x>0)?1:-1;
}
struct Vector {
    double x,y;
    Vector(double X=0,double Y=0){
        x=X,y=Y;
    }
};
typedef Vector Point;

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

struct Line {
    Point p;
    Vector v;
    double ang;
    Line(Point P=Point(0,0),Vector V=Vector(0,0)) {
        p=P,v=V;
        ang=atan2(v.y,v.x);
    }
    bool operator < (const Line &a) const {
        return ang<a.ang;
    }
};
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 b) {return Vector(a.x*b,a.y*b);}

int n,l,r,cnt;
double ans;
Line L[N],q[N];
Point p[N],poly[N];
double Cross(Vector a,Vector b) {
    return a.x*b.y-a.y*b.x;
}
Point GLI(Point P,Vector v,Point Q,Vector w) {
    Vector u=P-Q;
    double t=Cross(w,u)/Cross(v,w);
    return P+v*t;
}
bool Onleft(Line m,Point P) {
    Vector w=P-m.p;
    return dcmp(Cross(m.v,w))>=0;
}
void halfp(){
    sort(L+1,L+n+1);
    cnt=0;
    q[l=r=1]=L[1];
    for (int i=2;i<=n;++i) {
        while (l<r&&!Onleft(L[i],p[r-1])) --r;
        while (l<r&&!Onleft(L[i],p[l])) ++l;
        q[++r]=L[i];
        if (dcmp(Cross(q[r].v,q[r-1].v))==0) {
            --r;
            if (Onleft(q[r],L[i].p))
                q[r]=L[i];
        }
        if (l<r)
            p[r-1]=GLI(q[r-1].p,q[r-1].v,q[r].p,q[r].v);
    }
    while (l<r&&!Onleft(q[l],p[r-1]))
        --r;
    if (r-l<=1) return;
    p[r]=GLI(q[r].p,q[r].v,q[l].p,q[l].v);
    for (int i=l;i<=r;++i) poly[++cnt]=p[i];
}
double Area() {
    double ans=0;
    for (int i=2;i<cnt;++i)
        ans+=Cross(poly[i]-poly[1],poly[i+1]-poly[1]);
    return fabs(ans/2);
}

int main() {
    double R;
    while(scanf("%d%lf",&n,&R) != EOF) { // 输入点的个数
        Point temp[N],temp0,temp1;
        Line LL[N];
        Point pp[N];
        double ang[N];
        for (int i=1;i<=n;++i) {
            double x,y;
            scanf ("%lf%lf",&x,&y);
            pp[i] = Point (x,y);
        } // 开始需要设置一个无限大的区域
        pp[n + 1] = pp[1];
        for (int i = n + 1;i >= 2 ; -- i) {
            LL[i - 1] = Line(pp[i - 1],pp[i - 1] - pp[i]);
        }
        temp[1] = pp[1];
        L[1] = LL[n];
        ang[1] = L[1].ang;
        for (int i = 2;i <= n; ++ i) {
            L[i] = LL[n + 1 - i];
            temp[i] = pp[n + 2 - i];
            ang[i] = L[i].ang;
        }
        temp[n + 1] = temp[1];
        L[++n] = Line(Point(-10000,10000),Vector(0,-10000));
        L[++n] = Line(Point(-10000,-10000),Vector(10000,0));
        L[++n] = Line(Point(10000,-10000),Vector(0,10000));
        L[++n] = Line(Point(10000,10000),Vector(-10000,0));
        for (int i = 1;i <= n - 4; ++ i) {
            temp0 = temp[i];
            temp1 = temp[i + 1];
            double angd = ang[i] + PI / 2.0;
//            printf ("%.6f\n",angd);
            double addx = R * cos (angd);
            double addy = R * sin (angd);
            temp0.x += addx,temp1.x += addx;
            temp1.y += addy,temp0.y += addy;
            L[i] = Line(temp0,temp1 - temp0);
        }
        halfp();
        int id1 = 1,id2 = 1;
        double ans = 0;
        for (int i = 1;i <= cnt; ++ i) {
//            printf ("%.6f %.6f\n",poly[i].x,poly[i].y);
            for (int j = i + 1;j <= cnt ;++ j) {
                if (dist (poly[i],poly[j]) > ans) {
                    ans = dist(poly[i],poly[j]);
                    id1 = i;
                    id2 = j;
                }
            }
        }
        printf ("%.6f %.6f %.6f %.6f\n",poly[id1].x,poly[id1].y,poly[id2].x,poly[id2].y);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值