[BZOJ2618][Cqoi2006]凸多边形(半平面交)

211 篇文章 0 订阅
55 篇文章 0 订阅

题目描述

传送门

题解

半平面交裸题
注意判断直线和线段是否相交的时候要特判线段在直线上的情况,否则有可能导致被0除

代码

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

const double eps=1e-9;
const double inf=1e9;
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 b) {return Vector(a.x*b,a.y*b);}

int T,t,n,cnt;
Point p[N],poly[N],npoly[N];
double ans;

double Dot(Vector a,Vector b)
{
    return a.x*b.x+a.y*b.y;
}
double Len(Vector a)
{
    return sqrt(Dot(a,a));
}
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)
{
    int x=dcmp(Cross(A-B,C-B));
    int y=dcmp(Cross(A-B,D-B));
    return x&&y&&x!=y;
}
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()
{
    n=0;
    poly[++n]=Point(-inf,-inf);
    poly[++n]=Point(inf,-inf);
    poly[++n]=Point(inf,inf);
    poly[++n]=Point(-inf,inf);
}
void halfp(Point A,Point B)
{
    Point C,D;
    cnt=0;
    for (int i=1;i<=n;++i)
    {
        C=poly[i%n+1];
        D=poly[(i+1)%n+1];
        if (dcmp(Cross(C-A,B-A))<=0)
            npoly[++cnt]=C;
        if (insLS(A,B,C,D))
            npoly[++cnt]=GLI(B,B-A,C,D-C);
    }
    n=cnt;
    for (int i=1;i<=n;++i)
        poly[i]=npoly[i];
}
double Area()
{
    double area=0;
    for (int i=2;i<n;++i)
        area+=Cross(poly[i]-poly[1],poly[i+1]-poly[1]);
    return area/2;
}
int main()
{
    scanf("%d",&T);
    init();
    while (T--)
    {
        scanf("%d",&t);
        for (int i=1;i<=t;++i) scanf("%lf%lf",&p[i].x,&p[i].y);
        for (int i=1;i<=t;++i)
            halfp(p[i%t+1],p[(i+1)%t+1]);
    }
    ans=Area();
    printf("%.3lf\n",ans);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值