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

解法: 如图:

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

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

要注意的是:

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

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

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define eps 1e-8
using namespace std; struct Point{
double x,y;
Point(double x=, double y=):x(x),y(y) {}
void input() { scanf("%lf%lf",&x,&y); }
};
typedef Point Vector;
int dcmp(double x) {
if(x < -eps) return -;
if(x > eps) return ;
return ;
}
template <class T> T sqr(T x) { return x * x;}
Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
Vector operator - (Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); }
Vector operator * (Vector A, double p) { return Vector(A.x*p, A.y*p); }
Vector operator / (Vector A, double p) { return Vector(A.x/p, A.y/p); }
bool operator < (const Point& a, const Point& b) { return a.x < b.x || (a.x == b.x && a.y < b.y); }
bool operator >= (const Point& a, const Point& b) { return a.x >= b.x && a.y >= b.y; }
bool operator <= (const Point& a, const Point& b) { return a.x <= b.x && a.y <= b.y; }
bool operator == (const Point& a, const Point& b) { return dcmp(a.x-b.x) == && dcmp(a.y-b.y) == ; }
double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; }
double Length(Vector A) { return sqrt(Dot(A, A)); }
double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); }
double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; } bool SegmentIntersection(Point A,Point B,Point C,Point D) {
return max(A.x,B.x) >= min(C.x,D.x) &&
max(C.x,D.x) >= min(A.x,B.x) &&
max(A.y,B.y) >= min(C.y,D.y) &&
max(C.y,D.y) >= min(A.y,B.y) &&
dcmp(Cross(C-A,B-A)*Cross(D-A,B-A)) <= &&
dcmp(Cross(A-C,D-C)*Cross(B-C,D-C)) <= ;
}
void SegIntersectionPoint(Point& P,Point a,Point b,Point c,Point d) //需保证ab,cd相交
{
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));
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));
}
//Data Segment
Point A,B,C,D;
//Data Ends int main()
{
int t,i,j;
scanf("%d",&t);
while(t--)
{
A.input(), B.input(), C.input(), D.input();
if(!SegmentIntersection(A,B,C,D)) { puts("0.00"); continue; }
Point P,Insec;
SegIntersectionPoint(P,A,B,C,D);
Vector PA = A-P, PB = B-P;
Vector PC = C-P, PD = D-P;
Point S1,S2,S3,S4,K1,K2; if(dcmp(PA.y) > ) K1 = A;
else if(dcmp(PB.y) > ) K1 = B;
else { puts("0.00"); continue; } if(dcmp(PC.y) > ) K2 = C;
else if(dcmp(PD.y) > ) K2 = D;
else { puts("0.00"); continue; } S3 = Point(K1.x,K1.y), S4 = Point(K1.x,);
if(SegmentIntersection(S3,S4,P,K2)) { puts("0.00"); continue; }
S3 = Point(K2.x,K2.y), S4 = Point(K2.x,);
if(SegmentIntersection(S3,S4,P,K1)) { puts("0.00"); continue; } if(dcmp(K1.y-K2.y) < )
{
S1 = Point(-,K1.y), S2 = Point(,K1.y);
SegIntersectionPoint(Insec,S1,S2,P,K2);
printf("%.2f\n",fabs(Cross(K1-P,Insec-P)*0.5));
}
else
{
S1 = Point(-,K2.y), S2 = Point(,K2.y);
SegIntersectionPoint(Insec,S1,S2,P,K1);
printf("%.2f\n",fabs(Cross(K2-P,Insec-P)*0.5));
}
}
return ;
}

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. java发送固定字节到C++接口

    需求:java工程需要发送一个socket消息到C++接口,C++接口中规定了若干个参数,并且每个参数的长度是固定的,起始位置也是固定的, C++那边是GB2312编码,java这边是UTF-8. 现 ...

  2. Microsoft SharePoint Server 2013 Service Pack 1 (sp1)终于出来了!!!

    Microsoft SharePoint Server 2013 Service Pack 1 终于出来了!以下是下载地址如下,大小1.25G. http://www.microsoft.com/zh ...

  3. 为Autodesk Viewer添加自定义工具条

    如果你参加过我们近期的活动,你就会频繁的听到我们现在正在做的Autodesk Viewer大模型浏览器,这是一个不需要下载任何插件,基于WebGL技术的浏览器,可以支持几十种数据格式.同时viewer ...

  4. android中Post方式发送HTTP请求

    Post方式比Get方式要复杂一点,因为该方式需要将请求的参数放在http请求的正文中,所以需要构造请求体. 步骤: 1.构造URL URL url = new URL(PATH); 2.设置连接 H ...

  5. 免费真机调试 -- Xcode7

    刚新安装了Xcode7 Version 7.1 beta , 据说这个版本可以免费真机调试,于是用了一个新的AppID测试了,发现真的可以免费真机调试了呢!新的appId账号,没有支付每年的99美刀, ...

  6. 解决Android studio 非法字符的问题

    前言:今天遇到一个问题,从一个项目中把一些类拷贝到Android studio 里面,运行的时候,报错. 显示刚才拷贝的类中有非法字符,我又检查了一遍,发现类中没有非法字符. 在网上查到了,相关的解决 ...

  7. xcode 7及以上版本网络请求不成功的原因

    在Xcode 7以前的版本使用的是http协议,从Xcode 7开始,默认使用的是https协议,这优化了加密性能. 要想在Xcode 7及以上版本能够进行网络请求,我们需要在info.plist文件 ...

  8. js 判断多个一样的name

    var items = document.getElementsByName("spec_spec_1[]"); alert(items.length); for (i = 0; ...

  9. 最新中国菜刀caidao-20160620下载和说明

    0x00 中国制造, 黑客之刀 中国菜刀是中国安全圈内使用非常广泛的一款Webshell管理工具,此次老兵大牛进行了更新,界面更加优美,功能更加丰富,有图有真相! 0x01 验证 此次更新还自带了官网 ...

  10. Mina 快速入门

    Mina是什么 Mina是一个基于NIO的网络框架,使用它编写程序时,可以专注于业务处理,而不用过于关心IO操作.不论应用程序采用什么协议(TCP.UDP)或者其它的,Mina提供了一套公用的接口,来 ...