半平面交模板

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int read()
{
    int out=0,sgn=1;
    char jp=getchar();
    while(jp!='-' && (jp<'0' || jp>'9'))
        jp=getchar();
    if(jp=='-')
        sgn=-1,jp=getchar();
    while(jp>='0' && jp<='9')
        out=out*10+jp-'0',jp=getchar();
    return out*sgn;
}
const int MAXN=1e5+10;
struct v2
{
    double x,y;
    v2(double x=0,double y=0):x(x),y(y) {}
    friend double operator * (const v2 &a,const v2 &b)
    {
        return a.x*b.y-a.y*b.x;
    }
    v2 operator + (const v2 &rhs) const
    {
        return v2(x+rhs.x,y+rhs.y);
    }
    v2 operator - (const v2 &rhs) const
    {
        return v2(x-rhs.x,y-rhs.y);
    }
    v2 operator ^ (const double &lambda) const
    {
        return v2(x*lambda,y*lambda);
    }
    double modulus()
    {
        return sqrt(x*x+y*y);
    }
    double angle()
    {
        return atan2(y,x);
    }
    bool operator < (const v2 &rhs) const
    {
        return x==rhs.x?y<rhs.y:x<rhs.x;
    }
};
struct Line
{
    v2 p,v;
    double angle()
    {
        return v.angle();
    }
    friend bool operator < (Line a,Line b)
    {
        if(a.angle()!=b.angle())
            return a.angle()<b.angle();
        return a.v*b.v<0;
    }
};
bool Onleft(Line L,v2 p)
{
    return (L.v*(p-L.p))>0;
}
v2 Intersection(Line a,Line b)
{
    v2 u=a.p-b.p;
    double t=(b.v*u)/(a.v*b.v);
    return a.p+(a.v^t);
}
int head,tail,n;//n-the num of Lines
Line q[MAXN],L[MAXN];
v2 p[MAXN];
void Hpi()
{
    sort(L+1,L+1+n);
    q[head=tail=1]=L[1];
    for(int i=2;i<=n;++i)
    {
        while(head<tail && !Onleft(L[i],p[tail-1]))
            --tail;
        while(head<tail && !Onleft(L[i],p[head]))
            ++head;
        ++tail;
        q[tail]=L[i];
        if(head<tail && (q[tail].v*q[tail-1].v)==0)
        {
            --tail;
            if(Onleft(q[tail],L[i].p))
                q[tail]=L[i];
        }
        if(head<tail)
            p[tail-1]=Intersection(q[tail-1],q[tail]);
    }
    while(head<tail && !Onleft(q[head],p[tail-1]))
        --tail;
    p[tail]=Intersection(q[tail],q[head]);
    p[tail+1]=p[head];
    double area=0;
    v2 O=v2(0,0);
    for(int i=head;i<=tail;++i)
        area+=((O-p[i])*(O-p[i+1]));
    printf("%.2lf\n",fabs(area/2.0));
}
int main()
{
    return 0;
}

转载于:https://www.cnblogs.com/jklover/p/11303018.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值