求交集多边形面积

Time Limit:1000MS Memory Limit:30000KB Total Submit:98 Accepted:42

Description 
在平面上有两给定的凸多边形,若两凸多边形相交,则它们的交集也是一个凸多边形。若两凸多边形不相交,指的是两凸多边形相离或仅限于边界点与边上相交,则相交面积为0。如图所示: 你的任务是编程给出交集多边形的面积。 两给定的凸多边形按顺时针方向依次给出多边形每个顶点的坐标。

Input 
输入文件第一行为一整数M,表示第一个凸多边形的边数,以后M行分别给出了M个顶点的坐标;接着,给出第二个凸多边形的边数N,以后N行分别给出了N个顶点的坐标。

Output 
只一个数据即交集面积,保留两位小数点。

Sample Input 
4

0 0

0 1

1 1

1 0

4

-0.5 -0.5

-0.5 0.5

0.5 0.5

0.5 -0.5

Sample Output 
0.25

只适用于两个凸多边形

半平面交、不确定是不是对的、- -

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <cmath>
  6. using namespace std;
  7. #define EPS 1e-8
  8. #define N 1010
  9.  
  10. int n;
  11. int dq[N];
  12. int top,bot,pn;
  13.  
  14. int dump(double x)
  15. {
  16. if(fabs(x)<EPS) return ;
  17. return x>?:-;
  18. }
  19.  
  20. struct Point
  21. {
  22. double x,y;
  23. Point (){}
  24. Point (double x,double y):x(x),y(y){}
  25. Point operator - (Point p){
  26. return Point(x-p.x,y-p.y);
  27. }
  28. Point operator + (Point p){
  29. return Point(x+p.x,y+p.y);
  30. }
  31. Point operator * (double d){
  32. return Point(x*d,y*d);
  33. }
  34. double operator ^ (Point p){
  35. return x*p.y-y*p.x;
  36. }
  37. };
  38.  
  39. struct Line
  40. {
  41. Point s,e;
  42. double k;
  43. Line(){}
  44. Line(Point s,Point e):s(s),e(e){
  45. k=atan2(e.y-s.y,e.x-s.x);
  46. }
  47. Point operator & (Line l){
  48. return s+(e-s)*(((l.e-l.s)^(l.s-s))/((l.e-l.s)^(e-s)));
  49. }
  50. };
  51.  
  52. Line l[N];
  53. Point p[N];
  54.  
  55. bool HPIcmp(Line a,Line b)
  56. {
  57. int d=dump(a.k-b.k);
  58. if(!d) return dump((b.s-a.s)^(b.e-a.s))>;
  59. return d<;
  60. }
  61.  
  62. bool Judge(Line a,Line b,Line c)
  63. {
  64. Point p=b&c;
  65. return dump((a.s-p)^(a.e-p))<;
  66. }
  67.  
  68. void HPI(int n)
  69. {
  70. int i,j;
  71. sort(l,l+n,HPIcmp);
  72. for(i=,j=;i<n;i++)
  73. {
  74. if(dump(l[i].k-l[j].k)>) l[++j]=l[i];
  75. }
  76. n=j+;
  77. dq[]=;
  78. dq[]=;
  79. top=;
  80. bot=;
  81. for(i=;i<n;i++)
  82. {
  83. while(top>bot && Judge(l[i],l[dq[top]],l[dq[top-]])) top--;
  84. while(top>bot && Judge(l[i],l[dq[bot]],l[dq[bot+]])) bot++;
  85. dq[++top]=i;
  86. }
  87. while(top>bot && Judge(l[dq[bot]],l[dq[top]],l[dq[top-]])) top--;
  88. while(top>bot && Judge(l[dq[top]],l[dq[bot]],l[dq[bot+]])) bot++;
  89. dq[++top]=dq[bot];
  90. for(pn=,i=bot;i<top;i++,pn++)
  91. {
  92. p[pn]=l[dq[i+]]&l[dq[i]];
  93. }
  94. }
  95.  
  96. double GetArea()
  97. {
  98. double marea=;
  99. if(pn<) return ;
  100. for(int i=;i<pn;i++)
  101. {
  102. marea+=(p[i]-p[])^(p[i-]-p[]);
  103. }
  104. return fabs(marea)/;
  105. }
  106.  
  107. int main()
  108. {
  109. int n,m,tot;
  110. while(scanf("%d%d",&n,&m)!=EOF)
  111. {
  112. tot=;
  113. Point p1[N],p2[N];
  114. for(int i=;i<n;i++) scanf("%lf%lf",&p1[i].x,&p1[i].y);
  115. for(int i=;i<m;i++) scanf("%lf%lf",&p2[i].x,&p2[i].y);
  116. for(int i=;i<n;i++) l[tot++]=Line(p1[i],p1[(i-+n)%n]);
  117. for(int i=;i<m;i++) l[tot++]=Line(p2[i],p2[(i-+m)%m]);
  118. HPI(tot);
  119. printf("%.2f\n",GetArea());
  120. }
  121. return ;
  122. }

[ECNU 1624] 求交集多边形面积的更多相关文章

  1. ecnu1624求交集多边形面积

    链接 本来在刷hdu的一道题..一直没过,看到谈论区发现有凹的,我这种方法只能过凸多边形的相交面积.. 就找来这道题试下水. 两个凸多边形相交的部分要么没有 要么也是凸多边形,那就可以把这部分单独拿出 ...

  2. hdu-2036求任意多边形面积

    改革春风吹满地 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  3. 求任意多边形面积 python实现

    数学解决方法: 多边形外选取一点,连接各点构成三角形,计算求和......  详细链接  http://blog.csdn.net/hemmingway/article/details/7814494 ...

  4. HDU 2036 求任意多边形面积向量叉乘

    三角形的面积可以使用向量的叉积来求: 对于 三角形的面积 等于: [(x2 - x1)*(y3 - y1)- ( y2 - y1 ) * ( x3 - x1 )  ] / 2.0 但是面积是有方向的, ...

  5. poj 1654 Area(计算几何--叉积求多边形面积)

    一个简单的用叉积求任意多边形面积的题,并不难,但我却错了很多次,double的数据应该是要转化为long long,我转成了int...这里为了节省内存尽量不开数组,直接计算,我MLE了一发...,最 ...

  6. EOJ 1058. 挤模具 (多边形面积)

    题目链接:1058. 挤模具 题意 给出模具的底和体积,求模具的高. 思路 模具的底为多边形,因此求出多边形面积,用体积除以底的面积就是答案. 多边形的面积求解见 EOJ 1127. 多边形面积(计算 ...

  7. 三角剖分求多边形面积的交 HDU3060

    //三角剖分求多边形面积的交 HDU3060 #include <iostream> #include <cstdio> #include <cstring> #i ...

  8. Area - POJ 1654(求多边形面积)

    题目大意:从原点开始,1-4分别代表,向右下走,向右走,向右上走,向下走,5代表回到原点,6-9代表,向上走,向左下走,向左走,向左上走.求出最后的多边形面积. 分析:这个多边形面积很明显是不规则的, ...

  9. hdu 2036 求多边形面积 (凸、凹多边形)

    <题目链接> Problem Description “ 改革春风吹满地,不会AC没关系;实在不行回老家,还有一亩三分地.谢谢!(乐队奏乐)” 话说部分学生心态极好,每天就知道游戏,这次考 ...

随机推荐

  1. How to hide an entry in the Add/Remove Programs applet?

    Original link: http://www.winhelponline.com/articles/15/1/How-to-hide-an-entry-in-the-AddRemove-Prog ...

  2. wait(...) notify() notifyAll()

    简介 wait.notify.notifyAll是Java中3个与线程有关的方法,它们都是Object类中的方法. 其中,wait方法有3个重载形式: 1.wait() 2.wait(long tim ...

  3. 模板方法模式(Template Pattern)

    模板方法模式:在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中.模板方法使得子类可以在不改变算法的结构下,重新定义算法中的某些步骤. 这个模式是用来创建一个算法模板.模板就是一个方法.更具体地 ...

  4. 排序算法ONE:选择排序SelectSort

    /** *选择排序: * 对冒泡排序的一个改进 * 进行一趟排序时,不用每一次都交换,只需要把最大的标示记下 * 然后再进行一次交换 */ public class SelectSort { /** ...

  5. 获取input标签的所有属性

    1.用jquery$("input[name='btnAdd']").attr("value") 获取value属性值,其它属性换attr的参数就OK 例1: ...

  6. jQuery—一些常见方法(3)【width(),innerWidth(),outerWidth()】

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. php 命名空间(要求php5.3以上)

    要求php5.3以上 <?phpnamespace test;// 命名空间与目录类似功能,也可定义子命名空间,用分层的方式定义:/*namespace mydir\ok\project; 在声 ...

  8. python 调用第三方库压缩png或者转换成webp

    因为工作需要去研究了下png的压缩,发现转换成webp可以小很多,但是webp在手机上的解码速度比png的解码速度慢很多.出于进几年手机设备的处理器的性能也不错了,所以准备两套方案. 在网上搜索了一些 ...

  9. CGAL Manual/tutorial_hello_world.html

    Hello World Author CGAL Editorial Board 本教程是为知道C++和几何算法的基本知识的CGAL新手准备的.第一节展示了如何特化点和段CGAL类,以及如何应用几何谓词 ...

  10. 【ElasticSearch】

    ElasticSearch是基于Lucene开发的分布式搜索框架,包含如下特性: 分布式索引.搜索 索引自动分片.负载均衡 自动发现机器.组建集群 支持Restful 风格接口 配置简单等.