Polygons

HDU - 1632

题意:求两个多边形的"异或"面积.

半平面交~

  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 const int maxn = 50010;
  4 const int inf = 0x3f3f3f3f;
  5 const double eps = 1e-8;
  6 struct Point {
  7     double x,y;
  8     Point (double x = 0, double y = 0) : x(x), y(y) {}
  9 };
 10 typedef Point Vector;
 11 Vector operator + (Vector a, Vector b) {
 12     return Vector (a.x + b.x, a.y + b.y);
 13 }
 14 Vector operator * (Vector a, double s) {
 15     return Vector (a.x * s, a.y * s);
 16 }
 17 Vector operator / (Vector a, double p) {
 18     return Vector (a.x / p, a.y / p);
 19 }
 20 Vector operator - (Point a, Point b) {
 21     return Vector (a.x - b.x, a.y - b.y);
 22 }
 23 bool operator < (Point a, Point b) {
 24     return a.x < b.x || (a.x == b.x && a.y < b.y);
 25 }
 26 int dcmp (double x) {
 27     if(fabs(x) < eps) return 0;
 28     return x < 0 ? -1 : 1;
 29 }
 30 bool operator == (const Point &a, const Point &b) {
 31     return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
 32 }
 33 double Dot(Vector a, Vector b) {
 34     return a.x * b.x + a.y * b.y;
 35 }
 36 double Cross (Vector a, Vector b) {
 37     return a.x * b.y - a.y * b.x;
 38 }
 39 //半平面交
 40 struct Line{
 41     Point p;
 42     Vector v;
 43     double rad;
 44     Line () {}
 45     Line (Point p, Vector v) : p(p), v(v) {
 46         rad = atan2(v.y,v.x);
 47     }
 48     bool operator < (const Line &L) const {
 49         return rad < L.rad;
 50     }
 51 };
 52 bool OnLeft(Line L, Point p) {
 53     return Cross(L.v, p - L.p) > 0;
 54 }
 55 Point GetLineIntersection (Line a, Line b) {
 56     Vector u = a.p - b.p;
 57     double t = Cross(b.v, u) / Cross(a.v, b.v);
 58     return a.p + a.v*t;
 59 }
 60
 61 int HalfplaneIntersection(Line *L, int n,Point *poly) {
 62     sort(L, L+n);
 63     int first,last;
 64     Point *p = new Point[n];
 65     Line *q = new Line[n];  //双端队列
 66     q[first = last = 0] = L[0];
 67     for(int i = 1; i < n; i++) {
 68         while(first < last && !OnLeft(L[i], p[last-1])) last--;   //去尾
 69         while(first < last && !OnLeft(L[i], p[first])) first++;
 70         q[++last] = L[i];
 71         if(dcmp(Cross(q[last].v, q[last-1].v)) == 0) {
 72             last--;
 73             if(OnLeft(q[last], L[i].p)) q[last] = L[i];
 74         }
 75         if(first < last) p[last-1] = GetLineIntersection (q[last-1],q[last]);
 76     }
 77     while(first < last && !OnLeft(q[first], p[last-1])) last--;  //删除无用平面
 78     if(last - first <= 1) {
 79         delete []p;
 80         delete []q;
 81        return 0;  //空集
 82     }
 83     p[last] = GetLineIntersection (q[last], q[first]);
 84     int m = 0;
 85     for(int i = first; i <= last; i++) poly[m++] = p[i];
 86     delete []p;
 87     delete []q;
 88     return m;
 89 }
 90 Point p1[maxn], p2[maxn], poly[maxn];
 91 Line line[maxn];
 92
 93 double ConvexPolygonArea(Point *p, int n) {
 94     double area = 0;
 95     for(int i = 1; i < n-1; i++) {
 96         area += Cross(p[i] - p[0], p[i+1] - p[0]);
 97     }
 98     return area / 2;
 99 }
100
101 int main(){
102     int n, m;
103 //    freopen("in.txt", "r", stdin);
104     while(scanf("%d", &n) && n){
105         for(int i = 0; i < n; i++){
106             scanf("%lf %lf", &p1[i].x, &p1[i].y);
107         }
108         p1[n] = p1[0];
109         for(int i = 0; i < n; i++){
110            line[i] = Line(p1[i], p1[i] - p1[i+1]);
111         }
112         scanf("%d", &m);
113         for(int i = 0; i < m; i++){
114             scanf("%lf %lf", &p2[i].x, &p2[i].y);
115         }
116         p2[m] = p2[0];
117         for(int i = 0; i < m; i++){
118             line[i+n] = Line(p2[i], p2[i] - p2[i+1]);
119         }
120         int sz = HalfplaneIntersection(line, n+m, poly);
121         double ans = - ConvexPolygonArea(p1, n) - ConvexPolygonArea(p2, m) - 2*ConvexPolygonArea(poly, sz);
122         printf("%8.2lf", ans);
123     }
124     puts("");
125     return 0;
126 }

View Code

转载于:https://www.cnblogs.com/yijiull/p/7679605.html

Polygons HDU - 1632 (半平面交)相关推荐

  1. HDU 6617 Enveloping Convex(凸包+半平面交+二分)

    首先对于这m个点维护出一个凸包M,那么问题就变成了判断凸包P进行放大缩小能不能包含凸包M.(凸包P可以进行中心对称变换再进行放大缩小,见题意) 如何判断合适的相似比呢,我们可以用二分去放大缩小凸包P的 ...

  2. [凸多边形最大内切圆][半平面交]Most Distant Point from the Sea POJ3525

    The main land of Japan called Honshu is an island surrounded by the sea. In such an island, it is na ...

  3. UVA1396 Most Distant Point from the Sea(AM - ICPC - Tokyo - 2007)(计算几何,半平面交 + 二分答案)

    整理的算法模板合集: ACM模板 题目传送门 见<训练指南>P279 很明显就是一个二分答案,它问的是最远的点,直接枚举因为这里都是double类型的数所以有无限个点,我们可以直接二分. ...

  4. POJ 1474 Video Surveillance(半平面交)

    题意:半平面交求多边形内核(我明明及的我之前是会用kuangbin第一份版平面交的,现在怎么就不会用了呢,补第二份代码) 代码: #include<cstdio> #include< ...

  5. LA 2218 (半平面交) Triathlon

    题意: 有n个选手,铁人三项有连续的三段,对于每段场地选手i分别以vi, ui 和 wi匀速通过. 对于每个选手,问能否通过调整每种赛道的长度使得他成为冠军(不能并列). 分析: 粗一看,这不像一道计 ...

  6. POJ3335(半平面交)

    POJ3335 半平面交裸题 //poj3335 #include <cstdio> #include <cmath> #include <algorithm> # ...

  7. LA 3890 (半平面交) Most Distant Point from the Sea

    题意: 给出一个凸n边形,求多边形内部一点使得该点到边的最小距离最大. 分析: 最小值最大可以用二分. 多边形每条边的左边是一个半平面,将这n个半平面向左移动距离x,则将这个凸多边形缩小了.如果这n个 ...

  8. [BZOJ1007](HNOI2008)水平可见直线(半平面交习题)

    Description 在xoy直角坐标平面上有n条直线L1,L2,...Ln,若在y值为正无穷大处往下看,能见到Li的某个子线段,则称Li为可见的,否则Li为被覆盖的.     例如,对于直线:   ...

  9. 半平面交练习(计算几何)

    四:半平面交 Rotating Scoreboard /*Author : lifehappy */ #include <cstdio> #include <cmath> #i ...

最新文章

  1. Python中if __name__ == ‘__main__‘:的作用和原理(自用笔记)
  2. C语言线性插值lerp算法(附完整源码)
  3. PHP如何获取用户IP地址
  4. element-UI 表单校验失效处理
  5. MATLAB图像处理(包括图像类型转换)
  6. delphi串行通信源码教程
  7. css动感线条,使用css3制作动感导航条示例
  8. <Healing Psoriasis The Natural Alternative>笔记(持续进行中)
  9. html一般用那种方式定位,使用三种方式定位html中的元素
  10. 基于卷积自编码网络结构的图像修复
  11. 45.常用的数学工具类2-三角函数的使用
  12. Deepin系统navicat15安装
  13. KDA的新宠儿,金贝KD6,更大算力,探索无限可能
  14. java环境变量classpath的作用_JAVA环境变量中 classpath、path、JAVA_HOME的作用
  15. SerialFeature
  16. Java泛型方法的定义
  17. ocp 认证 043
  18. 计算机等级考试excel试题,计算机等级考试试题及答案解析(Excel) -电脑资料
  19. vue---十分钟搞懂vue计算属性
  20. protobuf_name_conflict问题解决

热门文章

  1. java+switch语句+枚举_Java:在子类下使用带有枚举的switch语句
  2. pytorch Tensor autograd functions
  3. click Arguments
  4. click Parameters
  5. nginx 小简单指令
  6. C语言 底层IO openclose
  7. 3.5 Bounding Box预测
  8. flask.Blueprint
  9. debian9.4配置iso作为更新源
  10. 在 ESXi 上配置 syslog (2003322)