BZOJ2618凸多边形

obviously这是一道半平面交模板题目

有的dalao最开始会对其合理性产生疑惑,BUT它是逆时针给点,所以你可以用半平面交搞一搞

于是就水过去了

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<iomanip>
#define eps 1e-8
using namespace std;
inline int read(){
int i=0,f=1;
char ch;
for(ch=getchar();ch>'9'||ch<'0';ch=getchar())
if(ch=='-') f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())
i=(i<<3)+(i<<1)+(ch^48);
return i*f;
}
int n,cnt,tot;
double ans;
struct P{
double x;
double y;
}p[3000],a[3000];
P vec(P a,P b){
P s;
s.x=a.x-b.x;
s.y=a.y-b.y;
return s;
}
double xplus(P a,P b){
return a.x*b.y-a.y*b.x;
}
struct L{
P a,b;
double slope;
friend bool operator <(const L &a,const L &b){
if(a.slope!=b.slope) return a.slope<b.slope;
return xplus(vec(a.b,a.a),vec(b.b,a.a))>0;//想一想,为什么
}
}l[3000],que[3000];
P inter(L a,L b){//依然叉乘求交点
double k1,k2,t;
k1=xplus(vec(b.b,a.a),vec(a.b,a.a));
k2=xplus(vec(a.b,a.a),vec(b.a,a.a));
t=k1/(k1+k2);
P ret;
ret.x=b.b.x+(b.a.x-b.b.x)*t;
ret.y=b.b.y+(b.a.y-b.b.y)*t;
return ret;
}
bool check(L a,L b,L t){
P p=inter(a,b);
return xplus(vec(t.b,t.a),vec(p,t.a))<0;
}
void hpi(){
sort(l+1,l+cnt+1);
int ll=1,rr=0;tot=0;
for(int i=1;i<=cnt;++i){
if(l[i].slope!=l[i-1].slope) ++tot;
l[tot]=l[i];
}
cnt=tot;tot=0;
que[++rr]=l[1];
que[++rr]=l[2];
for(int i=3;i<=cnt;++i){
while(ll<rr&&check(que[rr-1],que[rr],l[i])) --rr;//弹一弹
while(ll<rr&&check(que[ll+1],que[ll],l[i])) ++ll;//弹一弹
que[++rr]=l[i];
}
while(ll<rr&&check(que[rr-1],que[rr],que[ll])) --rr;//弹一弹
while(ll<rr&&check(que[ll+1],que[ll],que[rr])) ++ll//弹一弹;
que[rr+1]=que[ll];
for(int i=ll;i<=rr;++i)
a[++tot]=inter(que[i],que[i+1]);
return;
}
void getans(){
if(tot<3) return;
a[++tot]=a[1];
for(int i=1;i<=tot;++i)
ans+=xplus(a[i],a[i+1]);//当然是叉积爆算求面积
ans=fabs(ans)/2;
}
int main(){
n=read();
ans=0.0000;
for(int i=1;i<=n;++i){
int k=read();
for(int j=1;j<=k;++j)
p[j].x=read(),p[j].y=read();
p[k+1]=p[1];
for(int j=1;j<=k;++j)
l[++cnt].a=p[j],l[cnt].b=p[j+1];
}
for(int i=1;i<=cnt;++i)
l[i].slope=atan2(l[i].b.y-l[i].a.y,l[i].b.x-l[i].a.x);
hpi();
getans();
printf("%.3lf",ans);
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值