含【三点坐标计算面积】、【判断两线段是否有交点】、【求线段交点】模板
 
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. uint64, sizet_t, ssizet_t

    uint64 在32位平台 typedef unsigned long long int uint64_t;在64位平台 typedef unsigned long int uint64_t;不同的t ...

  2. Windows Phone 性能优化(二)

    这篇文章的 demo 是在 (一)的基础上进行的调整,逻辑基本相似.本文只列和 上一篇出不同的代码. 为了实现自定义的虚拟化,把上一篇文章的 ListBox 换成 ScrollViewer + Ite ...

  3. oracle 函数判断字符串是否包含图片格式

    首先是写一个分割字符串的函数,返回table类型 CREATE OR REPLACE FUNCTION fn_split (p_str IN VARCHAR2, p_delimiter IN VARC ...

  4. 将多个文件夹内的txt合并

    import os import re def text_create(name): """ 创建txt文件夹 """ desktop_pa ...

  5. hdu3457(有向图的dp问题)

    同http://www.cnblogs.com/ziyi--caolu/p/3202511.html #include<iostream> #include<stdio.h> ...

  6. background-origin:规定 background-position 属性相对于什么位置来定位

    background-origin:border-box;此时设置background-size:contain; 根据容器的边框定位 例如:容器的盒模型如下:设置了padding:20px;bord ...

  7. Cannot proceed with delivery: an existing transporter instance is currently uploading this package

    当使用Xcode的Application Loader上传spa到AppStore的过程中,如果临时中断,当你再次进行上传的过程时,就发发现如下现象: Cannot proceed with deli ...

  8. C语言 · 数对

    算法训练 数对   时间限制:1.0s   内存限制:512.0MB      问题描述 编写一个程序,该程序从用户读入一个整数,然后列出所有的数对,每个数对的乘积即为该数. 输入格式:输入只有一行, ...

  9. QListView和QListWidget的区别

    QListView是基于Model,而QListWidget是基于Item.这是它们的本质区别. 往QListView中添加条目需借助QAbstractListModel: 如: MainWindow ...

  10. QQ会员活动运营平台架构设计实践——高效自动化运营

    QQ会员活动运营平台(AMS),是QQ会员增值运营业务的重要载体之一,承担海量活动运营的Web系统.在过去四年的时间里,AMS日请求量从200-500万的阶段,一直增长到日请求3-5亿,最高CGI日请 ...