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

Description

It's raining outside. Farmer Johnson's bull Ben wants some rain to water his flowers. Ben nails two wooden boards on the wall of his barn. Shown in the pictures below, the two boards on the wall just look like two segments on the plane, as they have the same width. 

Your mission is to calculate how much rain these two boards can collect. 

Input

The first line contains the number of test cases. 
Each test case consists of 8 integers not exceeding 10,000 by absolute value, x1y1x2y2x3y3x4y4. (x1y1), (x2y2) are the endpoints of one board, and (x3y3), (x4y4) are the endpoints of the other one. 

Output

For each test case output a single line containing a real number with precision up to two decimal places - the amount of rain collected. 

Sample Input

2
0 1 1 0
1 0 2 1 0 1 2 1
1 0 1 2

Sample Output

1.00
0.00

Source

POJ Monthly--2006.04.28, Dagger@PKU_RPWT
 
 
 
 
 
需要想到好几种特殊情况。
 
线段不相交,结果为0.
有一条线段平行于x轴结果也为0;
 
尤其是第三种情况,很难想到。
 
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <string.h>
#include <math.h>
using namespace std; const double eps = 1e-;
int sgn(double x)
{
if(fabs(x) < eps)return ;
if(x < )return -;
else return ;
}
struct Point
{
double x,y;
Point(){}
Point(double _x,double _y)
{
x = _x;y = _y;
}
Point operator -(const Point &b)const
{
return Point(x - b.x,y - b.y);
}
//叉积
double operator ^(const Point &b)const
{
return x*b.y - y*b.x;
}
//点积
double operator *(const Point &b)const
{
return x*b.x + y*b.y;
}
};
struct Line
{
Point s,e;
Line(){}
Line(Point _s,Point _e)
{
s = _s;e = _e;
}
//两直线相交求交点
//第一个值为0表示直线重合,为1表示平行,为0表示相交,为2是相交
//只有第一个值为2时,交点才有意义
pair<int,Point> operator &(const Line &b)const
{
Point res = s;
if(sgn((s-e)^(b.s-b.e)) == )
{
if(sgn((s-b.e)^(b.s-b.e)) == )
return make_pair(,res);//重合
else return make_pair(,res);//平行
}
double t = ((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));
res.x += (e.x-s.x)*t;
res.y += (e.y-s.y)*t;
return make_pair(,res);
}
}; //*判断线段相交
bool inter(Line l1,Line l2)
{
return
max(l1.s.x,l1.e.x) >= min(l2.s.x,l2.e.x) &&
max(l2.s.x,l2.e.x) >= min(l1.s.x,l1.e.x) &&
max(l1.s.y,l1.e.y) >= min(l2.s.y,l2.e.y) &&
max(l2.s.y,l2.e.y) >= min(l1.s.y,l1.e.y) &&
sgn((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s-l1.e)) <= &&
sgn((l1.s-l2.e)^(l2.s-l2.e))*sgn((l1.e-l2.e)^(l2.s-l2.e)) <= ;
} int main()
{
int x1,y1,x2,y2,x3,y3,x4,y4;
int T;
Line l1,l2;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
l1 = Line(Point(x1,y1),Point(x2,y2));
l2 = Line(Point(x3,y3),Point(x4,y4));
if(sgn(l1.s.y-l1.e.y)== || sgn(l2.s.y-l2.e.y) == )
{
printf("0.00\n");
continue;
}
if(sgn(l1.s.y-l1.e.y) < )swap(l1.s,l1.e);
if(sgn(l2.s.y-l2.e.y) < )swap(l2.s,l2.e);
if(inter(l1,l2) == false)
{
printf("0.00\n");
continue;
}
//口被封掉的情况
if(inter(Line(l1.s,Point(l1.s.x,)),l2) )
{
printf("0.00\n");
continue;
}
//口被封掉
if(inter(Line(l2.s,Point(l2.s.x,)),l1) )
{
printf("0.00\n");
continue;
}
pair<int,Point>pr;
pr = l1 & l2;
Point p = pr.second;
double ans1;
pr = l1 & Line(Point(,l2.s.y),l2.s);
Point p1 = pr.second;
ans1 = fabs( (l2.s-p)^(p1-p) )/;
double ans2;
pr = l2 & Line(Point(,l1.s.y),l1.s);
Point p2 = pr.second;
ans2 = fabs( (l1.s-p)^(p2-p) )/;
printf("%.2lf\n",min(ans1,ans2));
}
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: 12970   Accepted: 199 ...

  3. POJ 2826 An Easy Problem?! --计算几何,叉积

    题意: 在墙上钉两块木板,问能装多少水.即两条线段所夹的中间开口向上的面积(到短板的水平线截止) 解法: 如图: 先看是否相交,不相交肯定不行,然后就要求出P与A,B / C,D中谁形成的向量是指向上 ...

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

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

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

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

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

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

  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. .gitignore规则不生效的解决办法

    .gitignore规则不生效的解决办法 使用git 的时候,在.gitignore中已经添加了某个文件或者文件夹,但是使用git status还能看见该文件的修改提示--–说明.gitignore未 ...

  2. GUI for git|SourceTree|入门基础

    原文链接:http://www.jianshu.com/p/be9f0484af9d 目录 SourceTree简介 SourceTree基本使用 SourceTree&Git部分名词解释 相 ...

  3. IOS中用UIFont返回字体的行高、动态改变tableView中Cell的高度

    一.动态改变Cell的高度 1.利用tableView代理方法的返回值决定每一行cell的高度 - (CGFloat)tableView:(UITableView *)tableView height ...

  4. POJ 1861 Network (MST)

    题意:求解最小生成树,以及最小瓶颈生成树上的瓶颈边. 思路:只是求最小生成树即可.瓶颈边就是生成树上权值最大的那条边. //#include <bits/stdc++.h> #includ ...

  5. MySQL基础之第16章 数据备份与还原

    16.1.数据备份 16.1.1.使用 mysqldump 命令备份 mysqldump [OPTIONS] database [tables]mysqldump [OPTIONS] --databa ...

  6. core文件分析

    http://baidutech.blog.51cto.com/4114344/904419/ http://www.newsmth.net/pc/pccon.php?id=10001977& ...

  7. 继承TextView简单画一个尺子

    import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; impor ...

  8. Web开发中设置快捷键来增强用户体验

    从事对日外包一年多以来,发现日本的无论是WinForm项目还是Web项目都注重快捷键的使用,日本人操作的时候都喜欢用键盘而不是用鼠标去点,用他们的话来说"键盘永远比鼠标来的快",所 ...

  9. IA32系统级架构总览(一) 实模式和保护模式

    应用程序的编写大部分的时候是不必关心系统级架构的,最多学习一下平台所给的API即可,也就是我们通常说的黑箱子.但是在学习操作系统的时候,系统级架构是要关心的. 系统级架构很难学习,其中一个很大的原因是 ...

  10. javascript函数sort

    sort函数用于对数组的元素进行排序 语法:arrayObject.sort(sortby); 参数sortby可选,规定排序顺序,必须是函数. 如果调用该方法时没有使用参数,将按照字幕顺序进行排序, ...