三角剖分求多边形面积的交 HDU3060
1 //三角剖分求多边形面积的交 HDU3060 2 3 #include <iostream> 4 #include <cstdio> 5 #include <cstring> 6 #include <stack> 7 #include <queue> 8 #include <cmath> 9 #include <algorithm> 10 using namespace std; 11 12 const int maxn = 555; 13 const int maxisn = 10; 14 const double eps = 1e-8; 15 const double pi = acos(-1.0); 16 17 int dcmp(double x) { 18 if(x > eps) return 1; 19 return x < -eps ? -1 : 0; 20 } 21 22 struct Point { 23 double x, y; 24 Point() { 25 x = y = 0; 26 } 27 Point(double a, double b) { 28 x = a, y = b; 29 } 30 inline Point operator-(const Point &b)const { 31 return Point(x - b.x, y - b.y); 32 } 33 inline Point operator+(const Point &b)const { 34 return Point(x + b.x, y + b.y); 35 } 36 inline double dot(const Point &b)const { 37 return x * b.x + y * b.y; 38 } 39 inline double cross(const Point &b, const Point &c)const { 40 return (b.x - x) * (c.y - y) - (c.x - x) * (b.y - y); 41 } 42 }; 43 44 Point LineCross(const Point &a, const Point &b, const Point &c, const Point &d) { 45 double u = a.cross(b, c), v = b.cross(a, d); 46 return Point((c.x * v + d.x * u) / (u + v), (c.y * v + d.y * u) / (u + v)); 47 } 48 49 double PolygonArea(Point p[], int n) { 50 if(n < 3) return 0.0; 51 double s = p[0].y * (p[n - 1].x - p[1].x); 52 p[n] = p[0]; 53 for(int i = 1; i < n; ++ i) 54 s += p[i].y * (p[i - 1].x - p[i + 1].x); 55 return fabs(s * 0.5); 56 } 57 58 double CPIA(Point a[], Point b[], int na, int nb) { //ConvexPolygonIntersectArea 59 Point p[maxisn], tmp[maxisn]; 60 int i, j, tn, sflag, eflag; 61 a[na] = a[0], b[nb] = b[0]; 62 memcpy(p, b, sizeof(Point) * (nb + 1)); 63 for(i = 0; i < na && nb > 2; ++ i) { 64 sflag = dcmp(a[i].cross(a[i + 1], p[0])); 65 for(j = tn = 0; j < nb; ++ j, sflag = eflag) { 66 if(sflag >= 0) tmp[tn ++] = p[j]; 67 eflag = dcmp(a[i].cross(a[i + 1], p[j + 1])); 68 if((sflag ^ eflag) == -2) 69 tmp[tn ++] = LineCross(a[i], a[i + 1], p[j], p[j + 1]); 70 } 71 memcpy(p, tmp, sizeof(Point) * tn); 72 nb = tn, p[nb] = p[0]; 73 } 74 if(nb < 3) return 0.0; 75 return PolygonArea(p, nb); 76 } 77 78 double SPIA(Point a[], Point b[], int na, int nb) { //SimplePolygonIntersectArea 79 int i, j; 80 Point t1[4], t2[4]; 81 double res = 0, if_clock_t1, if_clock_t2; 82 a[na] = t1[0] = a[0], b[nb] = t2[0] = b[0]; 83 for(i = 2; i < na; ++ i) { 84 t1[1] = a[i - 1], t1[2] = a[i]; 85 if_clock_t1 = dcmp(t1[0].cross(t1[1], t1[2])); 86 if(if_clock_t1 < 0) std::swap(t1[1], t1[2]); 87 for(j = 2; j < nb; ++ j) { 88 t2[1] = b[j - 1], t2[2] = b[j]; 89 if_clock_t2 = dcmp(t2[0].cross(t2[1], t2[2])); 90 if(if_clock_t2 < 0) std::swap(t2[1], t2[2]); 91 res += CPIA(t1, t2, 3, 3) * if_clock_t1 * if_clock_t2; 92 } 93 } 94 return PolygonArea(a, na) + PolygonArea(b, nb) - res; 95 } 96 97 Point p1[maxn], p2[maxn]; 98 int n1, n2; 99 100 int main() { 101 int i; 102 while(scanf("%d%d", &n1, &n2) != EOF) { 103 for(i = 0; i < n1; ++ i) scanf("%lf%lf", &p1[i].x, &p1[i].y); 104 for(i = 0; i < n2; ++ i) scanf("%lf%lf", &p2[i].x, &p2[i].y); 105 printf("%.2f\n", SPIA(p1, p2, n1, n2) + eps); 106 } 107 return 0; 108 }
转载于:https://www.cnblogs.com/ITUPC/p/5891030.html
三角剖分求多边形面积的交 HDU3060相关推荐
- 使用行列式公式求多边形面积
namespace SKJZ {namespace lib{public struct Point { public float x, y;}public class Polygon{/// < ...
- 给定点坐标求多边形面积模板
给定点坐标求多边形面积模板 方法:利用向量叉积求三角形面积的方法,把多边形分割成若干个三角形,然后求和得到多边形面积,计算时需要注意,给定的点的顺序必须是逆时针或者顺时针. 逆时针的方向是叉积的正方向 ...
- HDU 2036 改革春风吹满地(求多边形面积)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2036 改革春风吹满地 ...
- pku 2954 Triangle pku 1265 Area Pick定理的应用 + 叉积求多边形面积
Pick定理证明:http://translate.google.com/translate?u=http://episte.math.ntu.edu.tw/articles/sm/sm_25_10_ ...
- HDOJ-2036 求多边形面积
求给定的多边形面积,首先可以分割为数个三角形,分别求面积,最后累加即可. 对上图而言,多边形的面积就是:(S:1,a:2,b:3,c:4,d:5,e:6) S(1->6) = S(1,2,3) ...
- c语言给坐标求多边形面积,多边形的面积问题
多边形的面积问题 设构成多边形的坐标串为(xi,yi)(i=1,2,--,n),求此多边形面积A. #include #define N 10 float Area(float (*x)[2],int ...
- 计算几何之用叉乘求多边形面积
一.理论基础 二维向量叉乘得到的是对应平行四边形的有向面积,所以,只要保证按照逆时针对点排序,以下的算法都是可行的: ①凸多边形,选取一个A0,从A1开始依次挑选相邻的两个点,求三角形面积,求和即可 ...
- HDU 2036 改革春风吹满地 (求多边形面积)
传送门: 改革春风吹满地 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- 杭电 2036 改革春风吹满地【求多边形面积】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2036 解题思路:将多边形划分成n个三角形,将这些三角形的面积依次累加求和即可,这里将多边形划分成若干个 ...
最新文章
- XML 命名空间概述
- 关于arm处理器 内存编址模式 与 字节对齐方式 (转)
- ARCore中根据屏幕坐标计算射线的算法
- JS event使用方法详解
- arm linux 添加ntp服务,嵌入式linux添加NTP服务
- IE8给你选择的理由
- 安装(python 版)
- Spring Boot 实际应用(三)发送邮件实现
- 数字信号处理实验感想matlab,数字信号处理MATLAB实验报告 4.doc
- 在android系统中制作系统签名jks
- AVOD-理解系列(二)
- 解决python的OverflowError: int too large to convert to float
- 【毕业设计】基于Android的家校互动平台开发(内含完整代码和所有文档)——爱吖校推(你关注的,我们才推)...
- 从面试官的角度,聊聊java面试流程
- 3306端口被占用解决方式
- 使用文本标签做一个简单的自我介绍
- matlab 求一元二次方程的根,如何用Matlab求一元二次方程式解的个数以及解
- LaTeX 多语言支持
- 02 shell编程之条件语句
- CorelDRAW X8超低价优惠啦,你却还在用CDR X4破解?!