关闭

[POJ1279]Art Gallery(半平面交)

347人阅读 评论(0) 收藏 举报
分类:

题目描述

传送门
题意:求多边形的核可行区域的面积

题解

用半平面交求出凸壳了之后求面积
都是基本操作

代码

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
#define N 10005

const double inf=1e9;
const double eps=1e-9;
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;
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);}

int T,n,cnt,ncnt;
double x,y,ans;
Point p[N],poly[N],npoly[N];

double Cross(Vector A,Vector B)
{
    return A.x*B.y-A.y*B.x;
}
bool insLS(Point A,Point B,Point C,Point D)
{
    Vector v=B-A,w=C-A,u=D-A;
    return dcmp(Cross(v,w))!=dcmp(Cross(v,u));
}
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;
}
void init()
{
    cnt=0;
    poly[++cnt]=Point(inf,inf);
    poly[++cnt]=Point(inf,-inf);
    poly[++cnt]=Point(-inf,-inf);
    poly[++cnt]=Point(-inf,inf);
}
void halfp(Point A,Point B)
{
    ncnt=0;
    Point C,D;
    for (int i=1;i<=cnt;++i)
    {
        C=poly[i%cnt+1];
        D=poly[(i+1)%cnt+1];
        if (dcmp(Cross(B-A,C-A))<=0)
            npoly[++ncnt]=C;
        if (insLS(A,B,C,D))
            npoly[++ncnt]=GLI(A,B-A,C,D-C);
    }
    cnt=ncnt;
    for (int i=1;i<=cnt;++i)
        poly[i]=npoly[i];
}
double Area()
{
    double area=0;
    for (int i=2;i<cnt;++i)
        area+=Cross(poly[i]-poly[1],poly[i+1]-poly[1]);
    return fabs(area/2.0);
}
int main()
{
    scanf("%d",&T);
    while (T--)
    {
        scanf("%d",&n);
        for (int i=1;i<=n;++i)
        {
            scanf("%lf%lf",&x,&y);
            p[i]=Point(x,y);
        }
        init();
        for (int i=1;i<=n;++i)
            halfp(p[i%n+1],p[(i+1)%n+1]);
        ans=Area();
        printf("%.2lf\n",ans);
    }
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:480486次
    • 积分:16226
    • 等级:
    • 排名:第684名
    • 原创:1151篇
    • 转载:7篇
    • 译文:0篇
    • 评论:151条
    联系方式
    QQ:1209628538 加好友请备注省份和ID
    E-mail:fiona_2000@126.com
    最新评论