1. /*
  2. poj 2540 Hotter Colder 切割多边形
  3.  
  4. 用两点的中垂线切割多边形,根据冷热来判断要哪一半
  5.  
  6. 然后输出面积
  7.  
  8. */
  9. #include <stdio.h>
  10. #include<math.h>
  11. const double eps=1e-8;
  12. const int N=200;
  13. struct point
  14. {
  15. double x,y;
  16. point(){}
  17. point(double a,double b):x(a),y(b){}
  18. }dian[N];
  19. point jiao[N];
  20. inline bool mo_ee(double x,double y)
  21. {
  22. double ret=x-y;
  23. if(ret<0) ret=-ret;
  24. if(ret<eps) return 1;
  25. return 0;
  26. }
  27. inline bool mo_gg(double x,double y) { return x > y + eps;} // x > y
  28. inline bool mo_ll(double x,double y) { return x < y - eps;} // x < y
  29. inline bool mo_ge(double x,double y) { return x > y - eps;} // x >= y
  30. inline bool mo_le(double x,double y) { return x < y + eps;} // x <= y
  31. inline double mo_xmult(point p2,point p0,point p1)//p1在p2左返回负,在右边返回正
  32. {
  33. return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
  34. }
  35.  
  36. point mo_intersection(point u1,point u2,point v1,point v2)
  37. {
  38. point ret=u1;
  39. double t=((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x))
  40. /((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x));
  41. ret.x+=(u2.x-u1.x)*t;
  42. ret.y+=(u2.y-u1.y)*t;
  43. return ret;
  44. }
  45. /////////////////////////
  46. //求法向量
  47. point mo_getfaxian(point xiang)
  48. {
  49. point a;
  50. if(mo_ee(xiang.x,0))
  51. {
  52. a.x=1;
  53. a.y=0;
  54. return a;
  55. }else if(mo_ee(xiang.y,0))
  56. {
  57. a.x=0;
  58. a.y=1;
  59. return a;
  60. }else
  61. {
  62. a.x=1;
  63. a.y=-1.0*xiang.x/xiang.y;
  64. return a;
  65. }
  66.  
  67. }
  68.  
  69. //求多边形面积
  70. double mo_area_polygon(point *dian,int n)
  71. {
  72. int i;
  73. point yuan;
  74. yuan.x=yuan.y=0;
  75. double ret=0;
  76. for(i=0;i<n;++i)
  77. {
  78. ret+=mo_xmult(dian[(i+1)%n],yuan,dian[i]);
  79. }
  80. if(ret<0) ret=-ret;
  81. return ret/2;
  82. }
  83. point mo_banjiao_jiao_temp[N*2];
  84. void mo_banjiao_cut(point *ans,point qian,point hou,int &nofdian)
  85. {
  86. int i,k;
  87. for(i=k=0;i<nofdian;++i)
  88. {
  89. double a,b;
  90. a=mo_xmult(hou,ans[i],qian);
  91. b=mo_xmult(hou,ans[(i+1)%nofdian],qian);
  92. if(mo_ge(a,0))//顺时针就是<=0
  93. {
  94. mo_banjiao_jiao_temp[k++]=ans[i];
  95. }if(mo_ll(a*b,0))
  96. {
  97. mo_banjiao_jiao_temp[k++]=mo_intersection(qian,hou,ans[i],ans[(i+1)%nofdian]);
  98. }
  99. }
  100. for(i=0;i<k;++i)
  101. {
  102. ans[i]=mo_banjiao_jiao_temp[i];
  103. }
  104. nofdian=k;
  105. }
  106. int main()
  107. {
  108. point qian(0,0);
  109. point cur,mid,end;
  110. char order[20];
  111. int flag=0;
  112. jiao[0]=point(0,0);
  113. jiao[1]=point(10,0);
  114. jiao[2]=point(10,10);
  115. jiao[3]=point(0,10);
  116. int jiaodian=4;
  117. while(scanf("%lf",&cur.x)!=EOF)
  118. {
  119. scanf("%lf%s",&cur.y,order);
  120. getchar();
  121. if(order[0]=='S'||flag==1)
  122. {
  123. flag=1;
  124. printf("0.00\n");
  125. continue;
  126. }
  127. mid.x=(cur.x+qian.x)/2;
  128. mid.y=(cur.y+qian.y)/2;
  129. end=mo_getfaxian(point(cur.x-qian.x,cur.y-qian.y));
  130. end.x=mid.x+end.x;
  131. end.y=mid.y+end.y;
  132. bool zai=mo_gg(mo_xmult(end,cur,mid),0);
  133. if((order[0]=='H'&&zai)||(order[0]=='C'&&(!zai)))
  134. {
  135.  
  136. }else
  137. {
  138. point tem=end;
  139. end=mid;
  140. mid=tem;
  141. }
  142. mo_banjiao_cut(jiao,mid,end,jiaodian);
  143. double area=mo_area_polygon(jiao,jiaodian);
  144. printf("%.2lf\n",area);
  145. qian=cur;
  146. }
  147. return 0;
  148. }

poj 2540 Hotter Colder 切割多边形的更多相关文章

  1. POJ 2540 Hotter Colder(半平面交)

    Description The children's game Hotter Colder is played as follows. Player A leaves the room while p ...

  2. POJ 2540 Hotter Colder --半平面交

    题意: 一个(0,0)到(10,10)的矩形,目标点不定,从(0,0)开始走,如果走到新一点是"Hotter",那么意思是离目标点近了,如果是"Colder“,那么就是远 ...

  3. POJ 2540 Hotter Colder

    http://poj.org/problem?id=2540 题意:给你每次行走的路径,而且告诉你每次离一个点光源是远了还是近了,要求每次光源可能存在的位置的面积. 思路:如果出现"same ...

  4. poj 1474 Video Surveillance - 求多边形有没有核

    /* poj 1474 Video Surveillance - 求多边形有没有核 */ #include <stdio.h> #include<math.h> const d ...

  5. 【题解】切割多边形 [SCOI2003] [P4529] [Bzoj1091]

    [题解]切割多边形 [SCOI2003] [P4529] [Bzoj1091] 传送门:切割多边形 \(\text{[SCOI2003] [P4529]}\) \(\text{[Bzoj1091]}\ ...

  6. poj 1514 Metal Cutting (dfs+多边形切割)

    1514 -- Metal Cutting 一道类似于半平面交的题. 题意相当简单,给出一块矩形以及最后被切出来的的多边形各个顶点的位置.每次切割必须从一端切到另一端,问切出多边形最少要切多长的距离. ...

  7. bzoj1091: [SCOI2003]切割多边形

    Description 有一个凸p边形(p<=8),我们希望通过切割得到它.一开始的时候,你有一个n*m的矩形,即它的四角的坐标分别为(0,0), (0,m), (n,0), (n,m).每次你 ...

  8. poj 3525 半平面交求多边形内切圆最大半径【半平面交】+【二分】

    <题目链接> 题目大意:给出一个四面环海的凸多边形岛屿,求出这个岛屿中的点到海的最远距离. 解题分析: 仔细思考就会发现,其实题目其实就是让我们求该凸多边形内内切圆的最大半径是多少.但是, ...

  9. POJ 1265 Area (Pick定理 & 多边形面积)

    题目链接:POJ 1265 Problem Description Being well known for its highly innovative products, Merck would d ...

随机推荐

  1. 指定端口号,多线程扫描局域网内IP地址

    小白第一次发博客,请各路大神不要喷,有错的地方还请不吝啬指教,谢谢....... 因为注释基本上已经说清楚啦,在这里就不多说什么啦,知识不够怕误人子弟 # -*- coding:utf-8 -*-im ...

  2. Promise原理 && 简单实现

    Promise原理 参考https://github.com/chunpu/promise/blob/master/promise.js 个人认为原博的实现有点问题 在next函数的实现上, 会导致无 ...

  3. 【Chromium中文文档】沙箱FAQ

    沙箱FAQ 转载请注明出处:https://ahangchen.gitbooks.io/chromium_doc_zh/content/zh//General_Architecture/Sandbox ...

  4. CC++初学者编程教程(14) Redhat linux安装Oracle12c

    1选择虚拟机的设置 2 设置共享文件夹 3 使用共享文件夹向导 4 选择主机路径 5 启用文件共享 6 设置好文件共享以后,关闭虚拟机的设置 7 开启虚拟机 8 登陆 9输入密码 10 安装vmwar ...

  5. error: ld returned 1 exit status 和 error:undefined reference

    undefined reference 往往是链接时出现错误,无法解析引用.这篇文章总结的很好undefined reference问题总结 error: ld returned 1 exit sta ...

  6. CSS滤镜让图片模糊(毛玻璃效果)实例页面

    <pre name="code" class="css">CSS代码: .blur { filter: url(blur.svg#blur); /* ...

  7. List的方法和属性 方法或属性 作用

    List的方法和属性 方法或属性 作用 Capacity 用于获取或设置List可容纳元素的数量.当数量超过容量时,这个值会自动增长.您可以设置这个值以减少容量,也可以调用trin()方法来减少容量以 ...

  8. JQuery日记6.9 Promise/A之Callbacks

    JQuery并没有简单的使用一个Array来存储回调函数,而是通过JQuery.Callbacks(options)返回一个self对象,此对象能够动态的add,remove和fire回调函数队列.此 ...

  9. Linux下patch打补丁命令

    此命令用于为特定软件包打补丁,他使用diff命令对源文件进行操作. 基本命令语法: patch [-R] {-p(n)} [--dry-run] < patch_file_name p:为pat ...

  10. iOS获取本地ip(基本通用)

    今天有个朋友问我怎样訪问手机ip,上网找了几个,用了近200多行代码,最后发现头文件用的居然还是Linux中的,OC没有这个头文件.感觉socket本身应该能够后去自己的ip就试了一下,果然7.8行代 ...