题意: 在墙上钉两块木板,问能装多少水。即两条线段所夹的中间开口向上的面积(到短板的水平线截止)

解法: 如图:

先看是否相交,不相交肯定不行,然后就要求出P与A,B / C,D中谁形成的向量是指向上方的。

然后求出y值比较小的,建一条水平线,求出与另一条的交点,然后求面积。

要注意的是:

这种情况是不能装水的,要判掉。

还有 交G++会WA, 交C++就可以了, 不知道是POJ的问题还是 G++/C++的问题。

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <algorithm>
  7. #define eps 1e-8
  8. using namespace std;
  9.  
  10. struct Point{
  11. double x,y;
  12. Point(double x=, double y=):x(x),y(y) {}
  13. void input() { scanf("%lf%lf",&x,&y); }
  14. };
  15. typedef Point Vector;
  16. int dcmp(double x) {
  17. if(x < -eps) return -;
  18. if(x > eps) return ;
  19. return ;
  20. }
  21. template <class T> T sqr(T x) { return x * x;}
  22. Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
  23. Vector operator - (Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); }
  24. Vector operator * (Vector A, double p) { return Vector(A.x*p, A.y*p); }
  25. Vector operator / (Vector A, double p) { return Vector(A.x/p, A.y/p); }
  26. bool operator < (const Point& a, const Point& b) { return a.x < b.x || (a.x == b.x && a.y < b.y); }
  27. bool operator >= (const Point& a, const Point& b) { return a.x >= b.x && a.y >= b.y; }
  28. bool operator <= (const Point& a, const Point& b) { return a.x <= b.x && a.y <= b.y; }
  29. bool operator == (const Point& a, const Point& b) { return dcmp(a.x-b.x) == && dcmp(a.y-b.y) == ; }
  30. double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; }
  31. double Length(Vector A) { return sqrt(Dot(A, A)); }
  32. double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); }
  33. double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; }
  34.  
  35. bool SegmentIntersection(Point A,Point B,Point C,Point D) {
  36. return max(A.x,B.x) >= min(C.x,D.x) &&
  37. max(C.x,D.x) >= min(A.x,B.x) &&
  38. max(A.y,B.y) >= min(C.y,D.y) &&
  39. max(C.y,D.y) >= min(A.y,B.y) &&
  40. dcmp(Cross(C-A,B-A)*Cross(D-A,B-A)) <= &&
  41. dcmp(Cross(A-C,D-C)*Cross(B-C,D-C)) <= ;
  42. }
  43. void SegIntersectionPoint(Point& P,Point a,Point b,Point c,Point d) //需保证ab,cd相交
  44. {
  45. P.x = (Cross(d-a,b-a)*c.x - Cross(c-a,b-a)*d.x)/(Cross(d-a,b-a)-Cross(c-a,b-a));
  46. P.y = (Cross(d-a,b-a)*c.y - Cross(c-a,b-a)*d.y)/(Cross(d-a,b-a)-Cross(c-a,b-a));
  47. }
  48. //Data Segment
  49. Point A,B,C,D;
  50. //Data Ends
  51.  
  52. int main()
  53. {
  54. int t,i,j;
  55. scanf("%d",&t);
  56. while(t--)
  57. {
  58. A.input(), B.input(), C.input(), D.input();
  59. if(!SegmentIntersection(A,B,C,D)) { puts("0.00"); continue; }
  60. Point P,Insec;
  61. SegIntersectionPoint(P,A,B,C,D);
  62. Vector PA = A-P, PB = B-P;
  63. Vector PC = C-P, PD = D-P;
  64. Point S1,S2,S3,S4,K1,K2;
  65.  
  66. if(dcmp(PA.y) > ) K1 = A;
  67. else if(dcmp(PB.y) > ) K1 = B;
  68. else { puts("0.00"); continue; }
  69.  
  70. if(dcmp(PC.y) > ) K2 = C;
  71. else if(dcmp(PD.y) > ) K2 = D;
  72. else { puts("0.00"); continue; }
  73.  
  74. S3 = Point(K1.x,K1.y), S4 = Point(K1.x,);
  75. if(SegmentIntersection(S3,S4,P,K2)) { puts("0.00"); continue; }
  76. S3 = Point(K2.x,K2.y), S4 = Point(K2.x,);
  77. if(SegmentIntersection(S3,S4,P,K1)) { puts("0.00"); continue; }
  78.  
  79. if(dcmp(K1.y-K2.y) < )
  80. {
  81. S1 = Point(-,K1.y), S2 = Point(,K1.y);
  82. SegIntersectionPoint(Insec,S1,S2,P,K2);
  83. printf("%.2f\n",fabs(Cross(K1-P,Insec-P)*0.5));
  84. }
  85. else
  86. {
  87. S1 = Point(-,K2.y), S2 = Point(,K2.y);
  88. SegIntersectionPoint(Insec,S1,S2,P,K1);
  89. printf("%.2f\n",fabs(Cross(K2-P,Insec-P)*0.5));
  90. }
  91. }
  92. return ;
  93. }

POJ 2826 An Easy Problem?! --计算几何,叉积的更多相关文章

  1. POJ 2826 An Easy Problem? 判断线段相交

    POJ 2826 An Easy Problem?! -- 思路来自kuangbin博客 下面三种情况比较特殊,特别是第三种 G++怎么交都是WA,同样的代码C++A了 #include <io ...

  2. POJ 2826 An Easy Problem?!

    An Easy Problem?! Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7837   Accepted: 1145 ...

  3. POJ 2826 An Easy Problem?![线段]

    An Easy Problem?! Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12970   Accepted: 199 ...

  4. POJ 2826 An Easy Problem?! 好的标题

    受该两块木板以形成槽的效果.Q槽可容纳雨水多,注意雨爆跌,思想是非常easy,分类讨论是有点差. 1.假定两条线段不相交或平行,然后再装0: 2.有一个平行x轴.连衣裙0. 3.若上面覆盖以下的,装0 ...

  5. 简单几何(线段相交) POJ 2826 An Easy Problem?!

    题目传送门 题意:两条线段看成两块木板,雨水从上方往下垂直落下,问能接受到的水的体积 分析:恶心的分类讨论题,考虑各种情况,尤其是入口被堵住的情况,我的方法是先判断最高的两个点是否在交点的同一侧,然后 ...

  6. POJ 2826 An Easy Problem!(简单数论)

    Description Have you heard the fact "The base of every normal number system is 10" ? Of co ...

  7. POJ 2826 An Easy Problem?!(线段交点+简单计算)

    Description It's raining outside. Farmer Johnson's bull Ben wants some rain to water his flowers. Be ...

  8. POJ 1152 An Easy Problem! (取模运算性质)

    题目链接:POJ 1152 An Easy Problem! 题意:求一个N进制的数R.保证R能被(N-1)整除时最小的N. 第一反应是暴力.N的大小0到62.发现当中将N进制话成10进制时,数据会溢 ...

  9. [POJ] 2453 An Easy Problem [位运算]

    An Easy Problem   Description As we known, data stored in the computers is in binary form. The probl ...

随机推荐

  1. 详细解读XMLHttpRequest(一)同步请求和异步请求

    本文主要参考:MDN XMLHttpRequest 让发送一个HTTP请求变得非常容易.你只需要简单的创建一个请求对象实例,打开一个URL,然后发送这个请求.当传输完毕后,结果的HTTP状态以及返回的 ...

  2. overflow 属性

    写在前面的话: 2016年5月4日青年节,作为一名正青春的学生党,开始了博客生涯,励志做个勤奋上进的好青年.幻想着毕业后月薪W+ .走上人生巅峰的职场生活...... 然而 然而 然而 ,自制力有限的 ...

  3. SharePoint 2013 搭建负载均衡(NLB)

    服务器架构(三台虚机:AD和Sql在一台,前端两台) DC.Sql Server,其中包括:AD.DNS.DHCP服务(非必须): SPWeb01,其中包括:IIS.SharePoint: SPWeb ...

  4. Javascript - Arraylike的7种实现

    jQuery的崛起让ArrayLike(类数组)在javascript中大放异彩,它的出现为一组数据的行为(函数)扩展提供了基础. 类数组和数组相似,具有数组的某些行为,但是它相比数组可以更加自由的扩 ...

  5. [转]MOSS通过此命令注册模板,web应用程序可以根据stp模块生成网站集

    注:C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\bin          stsadm –o add ...

  6. xmpp整理笔记:环境的快速配置(附安装包)

    现在虽然环信的xmpp框架很火,但是也有一些弊端.环信的框架部分代码不开源,而且收费模式不科学,用户量一直低于免费线则好,一旦超过,收费极高. xmpp感觉还是从xmppFramework框架学起比较 ...

  7. JQuery隐藏显示详情功能

    放置两个DIV:初始DIV :在Repetr绑定设置文字隐藏(三元运算符):'<%# Eval("字段2").ToString().Length>11?Eval(&qu ...

  8. Android微信登陆

    前言 分享到微信朋友圈的功能早已经有了,但微信登录推出并不久,文档写的也并不是很清楚,这里记录分享一下. 声明 欢迎转载,但请保留文章原始出处:)  博客园:http://www.cnblogs.co ...

  9. Javascript之旅——第七站:说说js的调试

    最近比较吐槽,大家都知道,现在web前端相对几年前来说已经变得很重了,各种js框架,各种面对对象,而且项目多了,就会提取公共模块, 这些模块的UI展示都一样,不一样的就是后台逻辑,举个例子吧,我们做企 ...

  10. SQLServer中ISNULL、NULLIF和CONVERT函数

    create view sss as(select ISNULL(operate_time, CONVERT(VARCHAR(20),create_time,120)) time from s_pro ...