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

解法: 如图:

先看是否相交,不相交肯定不行,然后就要求出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. JavaScript数组与对象的关系

    JavaScript的数组,相比其他语言,是比较特殊的.数组是Object类型,只不过,有几个比较特殊的地方: 有索引下标 有默认的length属性 是有序的(注意,对象是无序的) 可以使用一些特殊的 ...

  2. 3D打印公司网站dedecms大气模板

    模板描述:1. 用FTP将安装包上传到服务器解压(或者解压在上传): 2. http://您的域名/install/ 进入到安装界面, 按照正常步骤安装即可:不要修改数据库表前缀,否则会造成原先数据无 ...

  3. HTML 文本格式化实例

    一,文本格式化:此例演示如何在一个 HTML 文件中对文本进行格式化. <html> <body> <b>This text is bold</b> & ...

  4. ae动态显示属性表————切记DataTable中要先Add(row)之后再往里传值。

    public partial class FrmAttributeTable : Form { private AxMapControl m_MapCtl; public FrmAttributeTa ...

  5. SharePoint如何关掉mysite. how to disable mysite creation

    一个很简单的问题 center admin --> application managment -->manage service application -->user profi ...

  6. Sqlite3中存储类型和数据类型结合文档解析。

    sqlite3是个很小的数据库,运行在手机,机顶盒上....那它就不可能像musql,sqlserver那么规范,有很多的数据类型,之前我也以为它定义了很多数据类型,其实不是他就5个存储类,那么多数据 ...

  7. App 即时通讯 SDK

    1.网易云信 http://netease.im/ 2.环信 http://www.easemob.com/customer/im 3.融云 http://www.rongcloud.cn/ 4.极光 ...

  8. android studio 使用SVN 锁定文件,防止别人修改(基于Android studio 1.4 )

    首先假设开发 A , 和 开发 B , 在使用 SVN 进行项目管理.那么A如何才能 某个锁定文件,防止B修改. 1.第一步,给这个文件加锁    完成这一步,则这个文件就别锁定了. 2.第二步,假如 ...

  9. 使用AndroidStudio进行NDK开发简单配置

    1. 准备工作 在实际写代码之前,首先我们还是需要做一些准备工作: 下载NDK开发包:Android官方下载页面 配置系统环境变量 下载好NDK开发包之后,直接解压到任意目录,然后需要配置一下系统环境 ...

  10. 【读书笔记】iOS网络-Cookie

    Cookie是HTTP协议在首个版本之后加入的一个重要组件.它向服务器提供了追踪会话状态的能力,同时又无须维持客户端与服务器之间的连接.在浏览器客户端,Cookie值是由服务器通过请求提供的,,然后被 ...