BZOJ 2618 CQOI 2006 凸多边形 半平面交

75 篇文章 1 订阅
20 篇文章 0 订阅

当成模板题来做了。。

给出几个多边形求交面积。

把这些多边形拆成一些半平面的交。最后算所有的半平面的交即可。

代码还蛮长的。

#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int N = 1005;
struct Point {
    double x, y;
    Point(){}
    Point(double _x, double _y) : x(_x), y(_y) {}
    Point operator +(const Point &b) const { return Point(x + b.x, y + b.y); }
    Point operator -(const Point &b) const { return Point(x - b.x, y - b.y); }
    double operator *(const Point &b) const { return x * b.y - y * b.x; }
    Point operator *(double b) const { return Point(b * x, b * y); }
} poly[N];

struct Line {
    Point x, v;
    double ang;
    Line(){}
    Line(const Point &_x, const Point &_v) : x(_x), v(_v), ang(atan2(_v.y, _v.x)) { }
    
    bool operator <(const Line &b) const {
        if (ang == b.ang) return v * (b.x - x) < 0; // 极角相等时,这么算的叉积表示a在b的左边时返回Line a < b,unique的时候就会选择a而丢掉b。
        return ang < b.ang;
    }
    
    bool operator ==(const Line &b) const { return ang == b.ang; }
    
    Point intersection(const Line &b) {
        Point u = x - b.x;
        double t = (b.v * u) / (v * b.v); // 画了图才知道这么写的意思。。
        return x + v * t;
    }
} lines[N];

bool right(const Point &p, const Line &l) {
    return l.v * (p - l.x) < 0;
}

int half_plane_intersection(Line l[], int n, Point poly[]) {
    static Line q[N];
    sort(l + 1, l + n + 1);
    n = unique(l + 1, l + n + 1) - (l + 1); // 极角相等时,挑出最靠近“凸多边形中心”的那个,见Line::operator<。
    int f = 1, r = 0, m = 0;
    q[++r] = l[1]; q[++r] = l[2];
    for (int i = 3; i <= n; i++) { // 逐个添加并删除半平面
        while (f < r && right(q[r].intersection(q[r - 1]), l[i])) r--;
        while (f < r && right(q[f].intersection(q[f + 1]), l[i])) f++;
        q[++r] = l[i];
    }
    while (f < r && right(q[r].intersection(q[r - 1]), q[f])) r--; // 还要处理首尾
    while (f < r && right(q[f].intersection(q[f + 1]), q[r])) f++;
    q[r + 1] = q[f];
    for (int i = f; i <= r; i++)
        poly[++m] = q[i].intersection(q[i + 1]);
    return m;
}

double calc_area(Point poly[], int m) {
    double ans = 0;
    if (m <= 2) return ans;
    poly[m + 1] = poly[1];
    for (int i = 1; i <= m; i++)
        ans += poly[i] * poly[i + 1];
    return fabs(ans) / 2;
}

int main() {
    int t, n, i, j, line_num = 0;
    scanf("%d", &t);
    for (i = 1; i <= t; i++) {
        scanf("%d", &n);
        for (j = 1; j <= n; j++)
            scanf("%lf %lf", &poly[j].x, &poly[j].y);
        poly[n + 1] = poly[1];
        for (j = 1; j <= n; j++)
            lines[++line_num] = Line(poly[j], poly[j + 1] - poly[j]);
    }
    int m = half_plane_intersection(lines, line_num, poly);
    printf("%.3lf", calc_area(poly, m));
    return 0;
}

2618: [Cqoi2006]凸多边形

Time Limit: 5 Sec  Memory Limit: 128 MB
Submit: 644  Solved: 333
[ Submit][ Status][ Discuss]

Description

逆时针给出 n个凸多边形的顶点坐标,求它们交的面积。例如n=2时,两个凸多边形如下图:

则相交部分的面积为5.233。

Input

第一行有一个整数n,表示凸多边形的个数,以下依次描述各个多边形。第i个多边形的第一行包含一个整数mi,表示多边形的边数,以下mi行每行两个整数,逆时针给出各个顶点的坐标。

 

Output

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

 

Sample Input

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

Sample Output

5.233

HINT

100%的数据满足:2<=n<=10,3<=mi<=50,每维坐标为[-1000,1000]内的整数


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值