1. /*
  2. poj 1279 Art Gallery - 求多边形核的面积
  3. */
  4. #include<stdio.h>
  5. #include<math.h>
  6. #include <algorithm>
  7. using namespace std;
  8.  
  9. const double eps=1e-8;
  10. struct point
  11. {
  12. double x,y;
  13. }dian[20000+10];
  14. point jiao[203];
  15. struct line
  16. {
  17. point s,e;
  18. double angle;
  19. }xian[20000+10];
  20. int n,yong;
  21. bool mo_ee(double x,double y)
  22. {
  23. double ret=x-y;
  24. if(ret<0) ret=-ret;
  25. if(ret<eps) return 1;
  26. return 0;
  27. }
  28. bool mo_gg(double x,double y) { return x > y + eps;} // x > y
  29. bool mo_ll(double x,double y) { return x < y - eps;} // x < y
  30. bool mo_ge(double x,double y) { return x > y - eps;} // x >= y
  31. bool mo_le(double x,double y) { return x < y + eps;} // x <= y
  32. point mo_intersection(point u1,point u2,point v1,point v2)
  33. {
  34. point ret=u1;
  35. double t=((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x))
  36. /((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x));
  37. ret.x+=(u2.x-u1.x)*t;
  38. ret.y+=(u2.y-u1.y)*t;
  39. return ret;
  40. }
  41. double mo_xmult(point p2,point p0,point p1)//p1在p2左返回负,在右边返回正
  42. {
  43. return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
  44. }
  45.  
  46. void mo_HPI_addl(point a,point b)
  47. {
  48. xian[yong].s=a;
  49. xian[yong].e=b;
  50. xian[yong].angle=atan2(b.y-a.y,b.x-a.x);
  51. yong++;
  52. }
  53. //半平面交
  54. bool mo_HPI_cmp(const line& a,const line& b)
  55. {
  56. if(mo_ee(a.angle,b.angle))
  57. {
  58. return mo_gg( mo_xmult(b.e,a.s,b.s),0);
  59. }else
  60. {
  61. return mo_ll(a.angle,b.angle);
  62. }
  63. }
  64. int mo_HPI_dq[20000+10];
  65. bool mo_HPI_isout(line cur,line top,line top_1)
  66. {
  67. point jiao=mo_intersection(top.s,top.e,top_1.s,top_1.e);
  68. return mo_ll( mo_xmult(cur.e,jiao,cur.s),0);
  69. }
  70. int mo_HalfPlaneIntersect(line *xian,int n,point *jiao)
  71. {
  72. int i,j,ret=0;
  73. sort(xian,xian+n,mo_HPI_cmp);
  74. for (i = 0, j = 0; i < n; i++)
  75. {
  76. if (mo_gg(xian[i].angle,xian[j].angle))
  77. {
  78. xian[++j] = xian[i];
  79. }
  80. }
  81. n=j+1;
  82. mo_HPI_dq[0]=0;
  83. mo_HPI_dq[1]=1;
  84. int top=1,bot=0;
  85. for (i = 2; i < n; i++)
  86. {
  87. while (top > bot && mo_HPI_isout(xian[i], xian[mo_HPI_dq[top]], xian[mo_HPI_dq[top-1]])) top--;
  88. while (top > bot && mo_HPI_isout(xian[i], xian[mo_HPI_dq[bot]], xian[mo_HPI_dq[bot+1]])) bot++;
  89. mo_HPI_dq[++top] = i; //当前半平面入栈
  90. }
  91. while (top > bot && mo_HPI_isout(xian[mo_HPI_dq[bot]], xian[mo_HPI_dq[top]], xian[mo_HPI_dq[top-1]])) top--;
  92. while (top > bot && mo_HPI_isout(xian[mo_HPI_dq[top]], xian[mo_HPI_dq[bot]], xian[mo_HPI_dq[bot+1]])) bot++;
  93. mo_HPI_dq[++top] = mo_HPI_dq[bot];
  94. for (ret = 0, i = bot; i < top; i++, ret++)
  95. {
  96. jiao[ret]=mo_intersection(xian[mo_HPI_dq[i+1]].s,xian[mo_HPI_dq[i+1]].e,xian[mo_HPI_dq[i]].s,xian[mo_HPI_dq[i]].e);
  97. }
  98. return ret;
  99. }
  100. //求多边形面积
  101. double mo_area_polygon(point *dian,int n)
  102. {
  103. int i;
  104. point yuan;
  105. yuan.x=yuan.y=0;
  106. double ret=0;
  107. for(i=0;i<n;++i)
  108. {
  109. ret+=mo_xmult(dian[(i+1)%n],yuan,dian[i]);
  110. }
  111. return ret;
  112. }
  113.  
  114. int main()
  115. {
  116. int i,iofcase=1,t;
  117. scanf("%d",&t);
  118. while(t--)
  119. {
  120. scanf("%d",&n);
  121. yong=0;
  122. for(i=0;i<n;++i)
  123. {
  124. scanf("%lf%lf",&dian[i].x,&dian[i].y);
  125. }
  126. double area=mo_area_polygon(dian,n);
  127. if(area<0)//若是顺时针
  128. {
  129. for(i=0;i<n;++i)
  130. {
  131. mo_HPI_addl(dian[(i+1)%n],dian[i]);
  132. }
  133. }else
  134. {
  135. for(i=0;i<n;++i)
  136. {
  137. mo_HPI_addl(dian[i],dian[(i+1)%n]);
  138. }
  139. }
  140. int ret=mo_HalfPlaneIntersect(xian,n,jiao);
  141. area=mo_area_polygon(jiao,ret);
  142. if(area<0) area=-area;
  143. area=area/2;
  144. printf("%.2lf\n",area);
  145. }
  146. return 0;
  147. }

poj 1279 Art Gallery - 求多边形核的面积的更多相关文章

  1. POJ 1279 Art Gallery(半平面交求多边形核的面积)

    题目链接 题意 : 求一个多边形的核的面积. 思路 : 半平面交求多边形的核,然后在求面积即可. #include <stdio.h> #include <string.h> ...

  2. poj 1279 -- Art Gallery (半平面交)

    鏈接:http://poj.org/problem?id=1279 Art Gallery Time Limit: 1000MS   Memory Limit: 10000K Total Submis ...

  3. poj 1279 Art Gallery (Half Plane Intersection)

    1279 -- Art Gallery 还是半平面交的问题,要求求出多边形中可以观察到多边形所有边的位置区域的面积.其实就是把每一条边看作有向直线然后套用半平面交.这题在输入的时候应该用多边形的有向面 ...

  4. POJ 1279 Art Gallery 半平面交求多边形核

    第一道半平面交,只会写N^2. 将每条边化作一个不等式,ax+by+c>0,所以要固定顺序,方便求解. 半平面交其实就是对一系列的不等式组进行求解可行解. 如果某点在直线右侧,说明那个点在区域内 ...

  5. POJ 1279 Art Gallery 半平面交/多边形求核

    http://poj.org/problem?id=1279 顺时针给你一个多边形...求能看到所有点的面积...用半平面对所有边取交即可,模版题 这里的半平面交是O(n^2)的算法...比较逗比.. ...

  6. POJ 1279 Art Gallery【半平面交】(求多边形的核)(模板题)

    <题目链接> 题目大意: 按顺时针顺序给出一个N边形,求N边形的核的面积. (多边形的核:它是平面简单多边形的核是该多边形内部的一个点集该点集中任意一点与多边形边界上一点的连线都处于这个多 ...

  7. [POJ]1279: Art Gallery

    题目大意:有一个N边形展馆,问展馆内有多少地方可以看到所有墙壁.(N<=1500) 思路:模板题,半平面交求出多边形的核后计算核的面积. #include<cstdio> #incl ...

  8. POJ 1279 Art Gallery 半平面交 多边形的核

    题意:求多边形的核的面积 套模板即可 #include <iostream> #include <cstdio> #include <cmath> #define ...

  9. POJ 1279 Art Gallery(半平面交)

    题目链接 回忆了一下,半平面交,整理了一下模版. #include <cstdio> #include <cstring> #include <string> #i ...

随机推荐

  1. Python3 出现'ascii' codec can't encode characters问题

    当使用urllib.request.urlopen打开包含中文的链接时报错: from urllib import request url = 'https://baike.baidu.com/ite ...

  2. 日期时间设置 "2018-05-04T16:36:23.6341371+08:00" 格式

    using System;using System.Collections.Generic;using System.Globalization;using System.Text; namespac ...

  3. 洛谷P2422 良好的感觉

    题目意思就是:最大化一个区间的和与这个区间的最小值的乘积. 换一个角度看问题,如果我们穷举一个最小值 $ a_i $ ,然后往左右扩展,显然是对的,复杂度 $ O(n^2) $.所以我们要优化一下这个 ...

  4. type Iterator does not take parameters

    在ubuntu编译java程序时报错:type Iterator does not take parameters 源码如下: package object; import java.util.*; ...

  5. SQLSERVER 2008 编辑所有或者任意行

    选中表 右键选择“编辑前200行”,然后选择左上角的 sql图标,然后在右侧的SQL语句去掉 top 200 然后执行查询 就可以编辑所有的行了,可以选择自己需要写SQL,然后查询编辑. 第二种方法: ...

  6. 第六届CCF软件能力认证

    1.数位之和 问题描述 给定一个十进制整数n,输出n的各位数字之和. 输入格式 输入一个整数n. 输出格式 输出一个整数,表示答案. 样例输入 20151220 样例输出 13 样例说明 201512 ...

  7. 丑数(UVa136)

    题目具体描述见:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=835&a ...

  8. Rookey.Frame之菜单设置

    在上一篇博文 Rookey.Frame企业级快速开发框架开源了 中我们介绍了Rookey.Frame极速开发框架的最新更新及开源介绍,后面慢慢介绍该框架的使用方法,本人文笔不好,写得不够好的地方请大家 ...

  9. xmanager

    [root@upright91 run]# ./runBenchmark.sh updbtpcc.properties sqlTableCreates Exception in thread &quo ...

  10. idea导入或打开项目配置问题

    learn项目遇到问题: 1.IntelliJ Idea编译报错:请使用 -source 7 或更高版本以启用 diamond 运算符 file - project structure或者直接快捷键: ...