poj2826—An Easy Problem?

题目链接:传送门

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

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,  x 1y 1x 2y 2x 3y 3x 4y 4. ( x 1y 1), ( x 2y 2) are the endpoints of one board, and ( x 3y 3), ( x 4y 4) 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


解题思路:各种判断

1.线段相交

2.线段共线

3.某条线段是水平的

4.某条线段覆盖了另一条线段

上述情况都是不可行的


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <stack>
#include <queue>

using namespace std;

typedef long long ll;
const int N = 100010;
const int M = 1000;
const int INF = 0x3fffffff;
const double eps = 1e-8;
const double PI = acos(-1.0);

struct Point
{
    double x,y;
    Point(){}
    Point(double _x,double _y){
        x = _x; y = _y;
    }
    Point operator - (const Point&a)const{
        return Point(x-a.x,y-a.y);
    }
    double operator ^ (const Point&a)const{
        return x*a.y-y*a.x;
    }
    double operator * (const Point&a)const{
        return x*a.x+y*a.y;
    }
    void input(){
        scanf("%lf%lf",&x,&y);
    }
};

struct Line
{
    Point s,e;
    Line(){}
    Line(Point _s,Point _e){
        s = _s; e = _e;
    }
    void getLine(Point _s,Point _e){
        s = _s; e = _e;
    }
};

int sgn(double x)
{
    if(fabs(x)<eps) return 0;
    if(x < 0) return -1;
    else return 1;
}

bool inter(Line l1,Line l2)
{
    return
    max(l1.s.x,l1.e.x) >= min(l2.s.x,l2.e.x) &&
    max(l1.s.y,l1.e.y) >= min(l2.s.y,l2.e.y) &&
    max(l2.s.x,l2.e.x) >= min(l1.s.x,l1.e.x) &&
    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)) <= 0 &&
    sgn((l1.s-l2.e)^(l2.s-l2.e))*sgn((l1.e-l2.e)^(l2.s-l2.e)) <= 0;
}

//返回线段的交点
Point SegInter(Line l1,Line l2)
{
    Point res = l1.s;
    double t = ((l1.s-l2.e)^(l2.s-l2.e))/((l1.s-l1.e)^(l2.s-l2.e));
    res.x += t*(l1.e.x-l1.s.x);
    res.y += t*(l1.e.y-l1.s.y);
    return res;
}

//给定一条直线和x值,返回一个直线上的点
Point getPointX(Line L,int x)
{
    double a,b,c;
    a = L.s.y-L.e.y;
    b = L.e.x-L.s.x;
    c = L.s.x*L.e.y-L.e.x*L.s.y;
    if(sgn(b) == 0) return Point(x,0.0);
    else return Point(x,(-c-a*x)/b);
}

//给定一条直线和y值,返回一个直线上的点
Point getPointY(Line L,int y)
{
    double a,b,c;
    a = L.s.y-L.e.y;
    b = L.e.x-L.s.x;
    c = L.s.x*L.e.y-L.e.x*L.s.y;
    if(sgn(a) == 0) return Point(0.0,y);
    else return Point((-c-b*y)/a,y);
}

//得到直线的斜率
//返回值:
//1:斜率为正,0:斜率为0,-1:斜率为负,INF:斜率不存在
int getK(Line L)
{
    if( L.s.x == L.e.x ) return INF;
    double k = (L.s.y-L.e.y)/(L.s.x-L.e.x);
    if( sgn(k) > 0 ) return 1;
    if( sgn(k) < 0 ) return -1;
    if( sgn(k) == 0 ) return 0;
}

double getCap(Line l1,Line l2)
{
    //线段不相交
    if( !inter(l1,l2) ) return 0.0;
    //线段共线
    if( sgn((l1.s-l1.e)^(l2.s-l2.e)) == 0 ) return 0.0;
    //有一根线段是平的
    if( sgn(l1.s.y-l1.e.y) == 0 || sgn(l2.s.y-l2.e.y) == 0) return 0.0;
    Point p2 = SegInter(l1,l2);
    int flag = sgn((l1.s-p2)^(l2.s-p2));
    //某根线段覆盖了另一根线段
    if( getK(l1) == 1 && getK(l2) == 1 && flag < 0 && l1.s.x >= l2.s.x ) return 0.0;
    if( getK(l1) == -1 && getK(l2) == -1 && flag > 0 && l1.s.x <= l2.s.x ) return 0.0;
    Point p1 = getPointY(l1,l2.s.y);
    return fabs((l2.s-p2)^(p1-p2))/2.0;
}

int main()
{
    int T;
    scanf("%d",&T);
    while( T-- ){
        Point p1,p2;
        Line l1,l2;
        p1.input(); p2.input();
        if( p1.y < p2.y ) swap(p1,p2);
        //点l1.s为上面的点
        l1.getLine(p1,p2);
        p1.input(); p2.input();
        if( p1.y < p2.y ) swap(p1,p2);
        l2.getLine(p1,p2);
        //l1在l2的上面
        if( l1.s.y < l2.s.y ) swap(l1,l2);
        printf("%.2lf\n",getCap(l1,l2));
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值