[CQOI2006] 凸多边形


题目描述

逆时针给出n个凸多边形的顶点坐标,求它们交的面积。例如n=2时,两个凸多边形如下图:
这里写图片描述
则相交部分的面积为5.233。


输入格式

第一行有一个整数n,表示凸多边形的个数,以下依次描述各个多边形。第i个多边形的第一行包含一个整数mi,表示多边形的边数,以下mi行每行两个整数,逆时针给出各个顶点的坐标。
【限制】
50%的数据满足:n=2
100%的数据满足:2<=n<=10,3<=mi<=50,每维坐标为[-1000,1000]内的整数


输出格式

仅包含一个实数,表示相交部分的面积,保留三位小数。


样例数据

样例输入

2
6
-2 0
-1 -2
1 -2
2 0
1 2
-1 2
4
0 -3
1 -1
2 2
-1 0

样例输出

5.233


题目分析

拆直线大法好,注意逆时针


源代码

#include<algorithm>
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cstdlib>
#include<vector>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
inline const int Get_Int() {
    int num=0,bj=1;
    char x=getchar();
    while(x<'0'||x>'9') {
        if(x=='-')bj=-1;
        x=getchar();
    }
    while(x>='0'&&x<='9') {
        num=num*10+x-'0';
        x=getchar();
    }
    return num*bj;
}
const double eps=1e-10;
struct Point {
    double x,y;
    Point(double _x,double _y):x(_x),y(_y){}
    Point(){}
    Point operator + (const Point& a) const {
        return Point(x+a.x,y+a.y);
    }
    Point operator - (const Point& a) const {
        return Point(a.x-x,a.y-y);
    }
    Point operator * (const double a) const {
        return Point(x*a,y*a);
    }
} ;
typedef Point Vector;
double Cross(Vector a,Vector b) { //叉积
    return a.x*b.y-b.x*a.y;
}
double Area(Point a,Point b,Point c) { //三点的平行四边形有向面积
    Vector u=b-a;
    Vector v=c-a;
    return Cross(u,v);
}
double Area(int n,Point* P) { //计算多边形有向面积(剖分法)
    double ans=0;
    for(int i=2; i<n; i++)ans+=Area(P[1],P[i],P[i+1]);
    return ans/2;
}
struct Line { //有向直线,左边为半平面
    Point p; //直线上任意一点
    Vector v; //方向向量
    double ang;
    Line() {}
    Line(Point p,Vector v):p(p),v(v) {
        ang=atan2(v.y,v.x);
    }
    bool operator < (const Line& L) const {
        return ang<L.ang;
    }
};
bool OnLeft(Line L,Point p) { //判断点p是否在有向直线L左边
    return Cross(L.v,L.p-p)>eps;
}
Point GetIntersection(Line a,Line b) {
    Vector u=a.p-b.p;
    double t=Cross(u,b.v)/Cross(a.v,b.v);
    return a.p+a.v*t;
}
int HalfplaneIntersection(int n,Line* L,Point* poly) {
    sort(L+1,L+n+1); //极角排序
    int first=1,last=1;
    Point p[n+5];
    Line q[n+5];
    q[last]=L[1];
    for(int i=2; i<=n; i++) {
        while(first<last&&!OnLeft(L[i],p[last-1]))last--;
        while(first<last&&!OnLeft(L[i],p[first]))first++;
        q[++last]=L[i];
        if(fabs(Cross(q[last].v,q[last-1].v))<eps) { //平行同向,取内侧 
            last--;
            if(!OnLeft(L[i],p[last-1]))q[last]=L[i];
        }
        if(first<last)p[last-1]=GetIntersection(q[last-1],q[last]);
    }
    while(first<last&&!OnLeft(q[first],p[last-1]))last--; //删除无用平面
    if(last-first<=1)return 0; //空集
    p[last]=GetIntersection(q[last],q[first]);
    int m=0;
    for(int i=first; i<=last; i++)poly[++m]=p[i]; 
    return m; 
}
///////////////
Point a[10005],p[10005];
Line l[10005];
int k,n,m,sum=0;
int main() {
    scanf("%d",&k);
    while(k--) {
        scanf("%d",&n);
        for(int i=1; i<=n; i++)scanf("%lf%lf",&p[i].x,&p[i].y);
        l[++sum]=Line(p[1],p[n]-p[1]);
        for(int i=2; i<=n; i++)l[++sum]=Line(p[i],p[i-1]-p[i]);
    }
    int cnt=HalfplaneIntersection(sum,l,p);
    printf("%0.3lf",abs(Area(cnt,p)));
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值