含【三点坐标计算面积】、【判断两线段是否有交点】、【求线段交点】模板
 
An Easy Problem?!
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions:15921   Accepted: 2459

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

题意:

给定四个坐标两条线

现在从上面倒水下来 问这两条线可以接住多少水

思路:

用G++WA了,用C++过了

两条线没有交点的0.00

两条线平行或重合的0.00

需要注意 如果口被封掉的 也是0.00

像这样

之前就在想这样的话需要怎么判断 其实就是如果某条线右边的端点往上找发现被截断了就是口被封住了

之后只需要先求出两线段交点 然后用纵坐标矮一点的那个点水平画线 找到和另一条线的交点

这三个点构成一个三角形 输出其面积

 //#include <bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<stdio.h>
#include<cstring> using namespace std;
typedef long long int LL; 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;
}
void input()
{
scanf("%lf%lf", &x, &y);
}
}; struct line{
point s, e;
line(){}
line(point _s, point _e)
{
s = _s;
e = _e;
}
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.s) ^ (l1.e - l1.s)) * sgn((l2.e - l1.s) ^ (l1.e - l1.s)) <= &&
sgn((l1.s - l2.s) ^ (l2.e - l1.s)) * sgn((l1.e - l2.s) ^ (l2.e - l2.s)) <= ;
} double area(point a, point b, point c)
{
return fabs((1.0 / ) * (a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y)));
} int t;
line l1, l2; int main()
{
scanf("%d", &t);
while(t--){
l1.s.input();l1.e.input();
l2.s.input();l2.e.input();
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)){
printf("0.00\n");
}
else if(inter(line(l1.s, point(l1.s.x, )), l2)){
printf("0.00\n");
}
else if(inter(line(l2.s, point(l2.s.x, )), l1)){
printf("0.00\n");
}
else{
pair<int, point>pr = l1 & l2;
if(pr.first != ){
printf("0.00\n");
}
else{
point a, b;
if(l1.s.y > l1.e.y){
a = l1.s;
}
else{
a = l1.e;
}
if(l2.s.y > l2.e.y){
b = l2.s;
}
else{
b = l2.e;
}
if(a.y < b.y){
b.x = a.x + ;b.y = a.y;
line l3 = line(a, b);
pair<int, point>ppr = l2 & l3;
b = ppr.second;
}
else{
a.x = b.x + ; a.y = b.y;
line l3 = line(a, b);
pair<int, point>ppr = l1 & l3;
a = ppr.second;
}
double ans = area(a, b, pr.second);
printf("%.2f\n", ans);
}
}
}
return ;
}

poj2826 An Easy Problem?!【计算几何】的更多相关文章

  1. poj2826 An Easy Problem?!(计算几何)

    传送门 •题意 两根木块组成一个槽,给定两个木块的两个端点 雨水竖直下落,问槽里能装多少雨水, •思路 找不能收集到雨水的情况 我们令线段较高的点为s点,较低的点为e点 ①两条木块没有交点 ②平行或重 ...

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

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

  3. bnuoj 1053 EASY Problem (计算几何)

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=1053 [题意]:基本上就是求直线与圆的交点坐标 [题解]:这种题我都比较喜欢用二分,三分做,果然可以 ...

  4. Poj2826 An Easy Problem

    呵呵哒.WA了无数次,一开始想的办法最终发现都有缺陷.首先需要知道: 1)线段不相交,一定面积为0 2)有一条线段与X轴平行,面积一定为0 3)线段相交,但是能接水的三角形上面线段把下面的线段完全覆盖 ...

  5. HDU 5572 An Easy Physics Problem (计算几何+对称点模板)

    HDU 5572 An Easy Physics Problem (计算几何) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5572 Descripti ...

  6. An Easy Problem?!(细节题,要把所有情况考虑到)

    http://poj.org/problem?id=2826 An Easy Problem?! Time Limit: 1000MS   Memory Limit: 65536K Total Sub ...

  7. UVA-11991 Easy Problem from Rujia Liu?

    Problem E Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for ...

  8. An easy problem

    An easy problem Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Sub ...

  9. UVa 11991:Easy Problem from Rujia Liu?(STL练习,map+vector)

    Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for example, ...

随机推荐

  1. 算法5-6:Kd树

    问题 给定一系列的点.和一个矩形.求矩形中包括的点的数量. 解答 这个问题能够通过建立矩阵来进行求解.首先将一个空间切割成矩阵,将点放置在相应的格子中.再计算矩形覆盖的格子.再推断格子中的点是否包括在 ...

  2. libvirt网络过滤规则简单总结

    libvirt网络过滤规则, 一个过滤规则定义的示例: < filter name='no-ip-spoold'chain='ipv4' >  < uuid >fce8ae33 ...

  3. iOS_文件上传进度条的实现思路-AFNettworking

    iOS_文件上传进度条的实现思路-AFNettworking //要上传的文件名,在这里我使用当前日期做为文件的名称 NSString * fileName =[NSString stringWith ...

  4. php读取csv的问题

    csv文件要用utf-8 无bom格式保存 如果有英文外的字符,另外每项要用双引号,不用双引号不能保存非英文字符

  5. Android——excise(用线性布局、表格布局、相对布局做发送邮件界面)

    LinearLayout <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns ...

  6. Android——Activity恢复用户用EditText输入的数据

    说明: 在横屏输入的内容,在Activity销毁后,即横屏后,获取用户输入的内容 步骤: 1.在xml页面定义EditText的id 2.用onSaveInstanceState保存用户输入的数据 ( ...

  7. 数据库递归查询(CET)

    IF OBJECT_ID('[ta]') IS NOT NULL      DROP TABLE [ta] Go CREATE TABLE ta([id] INT,[name] NVARCHAR(4) ...

  8. kickstart安装步骤

    1.1 环境说明 [root@test ~]# cat /etc/redhat-release CentOS release 6.9 (Final) [root@test ~]# uname -r 2 ...

  9. jQuery && jEasyUI 扩展功能集合

    jquery-extensions:jQuery && jEasyUI 扩展功能集合 该扩展功能基于 jQuery 1.9.x / 1.10.x / 1.11.x 和 jQuery E ...

  10. 开源 java CMS - FreeCMS2.3 移动app生成栏目数据

    原文地址:http://javaz.cn/site/javaz/site_study/info/2015/28230.html​ 项目地址:http://www.freeteam.cn/ 生成栏目数据 ...