J. Polygons Intersection

time limit per test:2 seconds
memory limit per test:64 megabytes
input:standard input
output:standard output

We will not waste your time, it is a straightforward problem. Given multiple polygons, calculate the area of their intersection. For simplicity, there will be exactly 2 polygons both of them are convex, given in the counterclockwise order and have non-zero areas. Furthermore, in one polygon a vertex won't be on the sides of the other one. The figure below demonstrates the first test case.

Input

The first line of the input will be a single integer T, the number of test cases (1  ≤  T  ≤  20). each test case contains two integers (3  ≤  N, M  ≤  40) Then a line contains N pairs of integers xi, yi (-1000  ≤  xi, yi  ≤  1000) coordinates of the ith vertex of polygon A, followed by a line contains M pairs of integers xj, yj (-1000  ≤  xj, yj  ≤  1000) coordinates of the jth vertex of polygon B. The coordinates are separated by a single space.

Output

For each test case, print on a single line, a single number representing the area of intersection, rounded to four decimal places.

Examples
Input
  1. 2
    5 3
    0 3 1 1 3 1 3 5 1 5
    1 3 5 3 3 6
    3 3
    -1 -1 -2 -1 -1 -2
    1 1 2 1 1 2
Output
  1. 2.6667
    0.0000

题目链接:http://codeforces.com/gym/100952/problem/J

题意:给2个凸多边形,求相交面积

思路:板子题,学习一下!

下面给出AC代码:

  1. #include "iostream"
  2. #include "string.h"
  3. #include "stack"
  4. #include "queue"
  5. #include "string"
  6. #include "vector"
  7. #include "set"
  8. #include "map"
  9. #include "algorithm"
  10. #include "stdio.h"
  11. #include "math.h"
  12. #define ll long long
  13. #define bug(x) cout<<x<<" "<<"UUUUU"<<endl;
  14. #define mem(a) memset(a,0,sizeof(a))
  15. #define mp(x,y) make_pair(x,y)
  16. using namespace std;
  17. const long long INF = 1e18+1LL;
  18. const int inf = 1e9+1e8;
  19. const int N=1e5+;
  20. #define maxn 510
  21. const double eps=1E-;
  22. int sig(double d){
  23. return(d>eps)-(d<-eps);
  24. }
  25. struct Point{
  26. double x,y; Point(){}
  27. Point(double x,double y):x(x),y(y){}
  28. bool operator==(const Point&p)const{
  29. return sig(x-p.x)==&&sig(y-p.y)==;
  30. }
  31. };
  32. double cross(Point o,Point a,Point b){
  33. return(a.x-o.x)*(b.y-o.y)-(b.x-o.x)*(a.y-o.y);
  34. }
  35. double area(Point* ps,int n){
  36. ps[n]=ps[];
  37. double res=;
  38. for(int i=;i<n;i++){
  39. res+=ps[i].x*ps[i+].y-ps[i].y*ps[i+].x;
  40. }
  41. return res/2.0;
  42. }
  43. int lineCross(Point a,Point b,Point c,Point d,Point&p){
  44. double s1,s2;
  45. s1=cross(a,b,c);
  46. s2=cross(a,b,d);
  47. if(sig(s1)==&&sig(s2)==) return ;
  48. if(sig(s2-s1)==) return ;
  49. p.x=(c.x*s2-d.x*s1)/(s2-s1);
  50. p.y=(c.y*s2-d.y*s1)/(s2-s1);
  51. return ;
  52. }
  53. //多边形切割
  54. //用直线ab切割多边形p,切割后的在向量(a,b)的左侧,并原地保存切割结果
  55. //如果退化为一个点,也会返回去,此时n为1
  56. void polygon_cut(Point*p,int&n,Point a,Point b){
  57. static Point pp[maxn];
  58. int m=;p[n]=p[];
  59. for(int i=;i<n;i++){
  60. if(sig(cross(a,b,p[i]))>) pp[m++]=p[i];
  61. if(sig(cross(a,b,p[i]))!=sig(cross(a,b,p[i+])))
  62. lineCross(a,b,p[i],p[i+],pp[m++]);
  63. }
  64. n=;
  65. for(int i=;i<m;i++)
  66. if(!i||!(pp[i]==pp[i-]))
  67. p[n++]=pp[i];
  68. while(n>&&p[n-]==p[])n--;
  69. }
  70. //---------------华丽的分隔线-----------------//
  71. //返回三角形oab和三角形ocd的有向交面积,o是原点//
  72. double intersectArea(Point a,Point b,Point c,Point d){
  73. Point o(,);
  74. int s1=sig(cross(o,a,b));
  75. int s2=sig(cross(o,c,d));
  76. if(s1==||s2==)return 0.0;//退化,面积为0
  77. if(s1==-) swap(a,b);
  78. if(s2==-) swap(c,d);
  79. Point p[]={o,a,b};
  80. int n=;
  81. polygon_cut(p,n,o,c);
  82. polygon_cut(p,n,c,d);
  83. polygon_cut(p,n,d,o);
  84. double res=fabs(area(p,n));
  85. if(s1*s2==-) res=-res;return res;
  86. }
  87. //求两多边形的交面积
  88. double intersectArea(Point*ps1,int n1,Point*ps2,int n2){
  89. if(area(ps1,n1)<) reverse(ps1,ps1+n1);
  90. if(area(ps2,n2)<) reverse(ps2,ps2+n2);
  91. ps1[n1]=ps1[];
  92. ps2[n2]=ps2[];
  93. double res=;
  94. for(int i=;i<n1;i++){
  95. for(int j=;j<n2;j++){
  96. res+=intersectArea(ps1[i],ps1[i+],ps2[j],ps2[j+]);
  97. }
  98. }
  99. return res;//assumeresispositive!
  100. }
  101. //hdu-3060求两个任意简单多边形的并面积
  102. Point ps1[maxn],ps2[maxn];
  103. int n1,n2;
  104. int main(){
  105. int t;
  106. cin>>t;
  107. while(t--){
  108. scanf("%d%d",&n1,&n2);
  109. for(int i=;i<n1;i++)
  110. scanf("%lf%lf",&ps1[i].x,&ps1[i].y);
  111. for(int i=;i<n2;i++)
  112. scanf("%lf%lf",&ps2[i].x,&ps2[i].y);
  113. double ans=intersectArea(ps1,n1,ps2,n2);
  114. //ans=fabs(area(ps1,n1))+fabs(area(ps2,n2))-ans;//容斥
  115. printf("%.4f\n",ans);
  116. }
  117. return ;
  118. }

Gym 100952J&&2015 HIAST Collegiate Programming Contest J. Polygons Intersection【计算几何求解两个凸多边形的相交面积板子题】的更多相关文章

  1. Gym 100952E&&2015 HIAST Collegiate Programming Contest E. Arrange Teams【DFS+剪枝】

    E. Arrange Teams time limit per test:2 seconds memory limit per test:64 megabytes input:standard inp ...

  2. Gym 100952F&&2015 HIAST Collegiate Programming Contest F. Contestants Ranking【BFS+STL乱搞(map+vector)+优先队列】

    F. Contestants Ranking time limit per test:1 second memory limit per test:24 megabytes input:standar ...

  3. Gym 100952A&&2015 HIAST Collegiate Programming Contest A. Who is the winner?【字符串,暴力】

    A. Who is the winner? time limit per test:1 second memory limit per test:64 megabytes input:standard ...

  4. Gym 100952I&&2015 HIAST Collegiate Programming Contest I. Mancala【模拟】

    I. Mancala time limit per test:3 seconds memory limit per test:256 megabytes input:standard input ou ...

  5. Gym 100952H&&2015 HIAST Collegiate Programming Contest H. Special Palindrome【dp预处理+矩阵快速幂/打表解法】

    H. Special Palindrome time limit per test:1 second memory limit per test:64 megabytes input:standard ...

  6. Gym 100952D&&2015 HIAST Collegiate Programming Contest D. Time to go back【杨辉三角预处理,组合数,dp】

    D. Time to go back time limit per test:1 second memory limit per test:256 megabytes input:standard i ...

  7. Gym 100952C&&2015 HIAST Collegiate Programming Contest C. Palindrome Again !!【字符串,模拟】

    C. Palindrome Again !! time limit per test:1 second memory limit per test:64 megabytes input:standar ...

  8. Gym 100952G&&2015 HIAST Collegiate Programming Contest G. The jar of divisors【简单博弈】

    G. The jar of divisors time limit per test:2 seconds memory limit per test:64 megabytes input:standa ...

  9. Gym 100952B&&2015 HIAST Collegiate Programming Contest B. New Job【模拟】

    B. New Job time limit per test:1 second memory limit per test:64 megabytes input:standard input outp ...

随机推荐

  1. Oracle索引详解

    Oracle索引详解(二) --索引分类   Oracle 提供了大量索引选项.知道在给定条件下使用哪个选项对于一个程序的性能来说非常重要.一个错误的选择可能会引发死锁,并导致数据库性能急剧下降或进程 ...

  2. HTML5 桌面通知:Notification API

    原文地址:http://blog.gdfengshuo.com/article/23/ 前言 Notification API 是 HTML5 新增的桌面通知 API,用于向用户显示通知信息.该通知是 ...

  3. verilog抓外部低频输入信号的上升沿和下降沿

    版权申明:本文为博主窗户(Colin Cai)原创,欢迎转帖.如要转贴,必须注明原文网址 http://www.cnblogs.com/Colin-Cai/p/7220107.html 作者:窗户 Q ...

  4. ORACLE环境变量设置

    用oracle帐号登录,配置相关环境变量: vi .bash_profile export ORACLE_BASE=/u01/app/oracleexport ORACLE_HOME=/u01/app ...

  5. 浏览器根对象window之screen

    1. screen 1.1 availHeight/Width screen.availWidth返回浏览器窗口可占用的水平宽度(单位:像素). screen.availHeight返回浏览器窗口在屏 ...

  6. I/O模型详细解析

    内核空间和用户空间:由于操作系统都包括内核空间和用户空间(或者说内核态和用户态),内核空间主要存放的是内核代码和数据,是供系统进程使用的空间.而用户空间主要存放的是用户代码和数据,是供用户进程使用的空 ...

  7. 转深入理解 AngularJS 的 Scope作用域

    文章转载英文:what-are-the-nuances-of-scope-prototypal-prototypical-inheritance-in-angularjs 中文:http://www. ...

  8. (5编译使用最新opencv)从零开始的嵌入式图像图像处理(PI+QT+OpenCV)实战演练

    从零开始的嵌入式图像图像处理(PI+QT+OpenCV)实战演练 1综述http://www.cnblogs.com/jsxyhelu/p/7907241.html 2环境架设http://www.c ...

  9. gitignore样例解析

    # 这是注释行 -- 被忽略 *.a # 忽略所有以 .a 为扩展名的文件 !lib.a # 但是lib.a 文件或目录不要忽略,即使前面设置了对*.a的忽略 /TODO # 只忽略此目录下的TODO ...

  10. 《深入解剖Yii2框架》前言

    写代码需要站在巨人的肩膀上,将主要精力集中在自己所需要实现的业务上面,避免反复搭建基础服务,重复造轮子.PHP框架就是这样一些巨人的"肩膀",使得我们"站"得更 ...