BZOJ 2618 CQOI 2006 凸多边形 半平面交
2016-01-07 23:11
429 查看
当成模板题来做了。。
给出几个多边形求交面积。
把这些多边形拆成一些半平面的交。最后算所有的半平面的交即可。
代码还蛮长的。
Submit: 644 Solved: 333
[Submit][Status][Discuss]
则相交部分的面积为5.233。
6
-2 0
-1 -2
1 -2
2 0
1 2
-1 2
4
0 -3
1 -1
2 2
-1 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 ; 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 ; 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 ; 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 MBSubmit: 644 Solved: 333
[Submit][Status][Discuss]
Description
逆时针给出n个凸多边形的顶点坐标,求它们交的面积。例如n=2时,两个凸多边形如下图:则相交部分的面积为5.233。
Input
第一行有一个整数n,表示凸多边形的个数,以下依次描述各个多边形。第i个多边形的第一行包含一个整数mi,表示多边形的边数,以下mi行每行两个整数,逆时针给出各个顶点的坐标。Output
输出文件仅包含一个实数,表示相交部分的面积,保留三位小数。Sample Input
26
-2 0
-1 -2
1 -2
2 0
1 2
-1 2
4
0 -3
1 -1
2 2
-1 0
Sample Output
5.233HINT
100%的数据满足:2<=n<=10,3<=mi<=50,每维坐标为[-1000,1000]内的整数相关文章推荐
- 使用FLANN进行特征点匹配
- 不惧面试:委托
- 光流算法:Brox光流的OpenCV源码解析
- python 打印杨辉三角
- 从LeNet-5看卷积神经网络CNNs
- Java并发编程:线程创建
- [转] Cz/C++中栈空间、堆空间,及内存区域的划分
- C#高级编程笔记(一)
- Win7 Wifi 老断线
- Mac OS X 下完全删除MySQL的方法
- MySQL-----安装MySQL(免安装版方式)
- Python/C API中的引用计数修改
- Oracle入门2
- 源码篇:MBProgressHUD
- 菜鸟学python(1) hello world!
- nodejs的某些api~(六)HTTPS
- git过滤文件和文件夹
- IOCP 概述
- Dubbo分布式服务框架
- 移动端常用的数据交互格式有xml和json两种方式