poj 2826 判断线段位置关系复杂题目(真的气)

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

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 1, y 1, x 2, y 2, x 3, y 3, x 4, y 4. ( x 1, y 1), ( x 2, y 2) are the endpoints of one board, and ( x 3, y 3), ( x 4, y 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.如果两条线段不相交或者平行,则装0;

2.有一条平行x轴,装0;

3.若上面覆盖下面的,装0;

4.其它,叉积求面积。




#include<math.h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

#define eps 1e-8
int dcmp(double x){
    if (fabs (x) < eps) return 0;
    else    return x < 0 ? -1 : 1;
}

struct point
{
    double x,y;
    point(){}
    point(double _x,double _y){
        x=_x,y=_y;
    }
};
struct line
{
    point a;
    point b;
    line(){}
    line(point _x,point _y){
        a=_x,b=_y;
    }
};

double Dis(point p1,point p2){
    return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
double Cross(point p1,point p2,point p3){
    return (p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x);
}
double Cross2(point A, point B){
    return A.x * B.y - A.y * B.x;
}
point operator - (point A,point B){
    return point(A.x-B.x, A.y-B.y);
}
point operator * (point A,double t){
    return point(A.x*t , A.y*t);
}
point operator + (point A,point B){
    return point(A.x+B.x, A.y+B.y);
}


bool SegmentProperIntersection(point a1, point a2, point b1, point b2)  {
    double c1 = Cross2 (a2 - a1, b1 - a1), c2 = Cross2 (a2 - a1, b2 - a1),
           c3 = Cross2 (b2 - b1, a1 - b1), c4 = Cross2 (b2 - b1, a2 - b1);
    return dcmp (c1) * dcmp (c2) <= 0 && dcmp (c3 * c4) <= 0;
}

point Intersection(point a,point b,point c,point d)
{
    point ret=a;
    double t=((a.x-c.x)*(c.y-d.y)-(a.y-c.y)*(c.x-d.x))
             /((a.x-b.x)*(c.y-d.y)-(a.y-b.y)*(c.x-d.x));
    ret.x+=(b.x-a.x)*t;
    ret.y+=(b.y-a.y)*t;
    return ret;
}


double disptoseg(point p,point l1,point l2)
{
    point t=p;
    t.x+=l1.y-l2.y;
    t.y+=l2.x-l1.x;
    if (Cross(l1,t,p)*Cross(l2,t,p)>eps)
        return Dis(p,l1)<Dis(p,l2)?Dis(p,l1):Dis(p,l2);
    return fabs(Cross(p,l1,l2))/Dis(l1,l2);
}

int main()
{
    int T;
    //freopen("in.txt","r",stdin);
    scanf("%d",&T);
    while(T--)
    {
        point p1,p2,p3,p4;
        scanf("%lf%lf%lf%lf",&p1.x,&p1.y,&p2.x,&p2.y);
        scanf("%lf%lf%lf%lf",&p3.x,&p3.y,&p4.x,&p4.y);

        if(!SegmentProperIntersection(p1,p2,p3,p4)){///无交点
            puts("0.00");
            continue;
        }
        if(Cross2(p1-p2,p3-p4)==0){///重合
            puts("0.00");
            continue;
        }
        if(p1.y==p2.y||p3.y==p4.y){///有一条线平行x轴
            puts("0.00");
            continue;
        }
        point t=Intersection(p1,p2,p3,p4);
        if(p1.y<p2.y)
            swap(p1,p2);
        if(p3.y<p4.y)
            swap(p3,p4);


        int flag=0;
        ///开口朝上但是接不到水的两种情况
        if(p1.x<=t.x&&p3.x<=t.x){
            if(p1.y>=p3.y&&p1.x<=p3.x&&Cross(t,p1,p3)>=0)
                flag=1;
            else if(p3.y>=p1.y&&p3.x<=p1.x&&Cross(t,p1,p3)<=0)
                flag=1;
        }
        else if(p1.x>=t.x&&p3.x>=t.x){
            if(p1.y>=p3.y&&p1.x>=p3.x&&Cross(t,p1,p3)<=0)
                flag=1;
            else if(p3.y>=p1.y&&p3.x>=p1.x&&Cross(t,p1,p3)>=0)
                flag=1;
        }
        if(flag){
            puts("0.00");
            continue;
        }


        double h = min(p1.y,p3.y);
        double q2 = p3.x+(p4.x-p3.x)*(h-p3.y)/(p4.y-p3.y);
        double q1 = p1.x+(p2.x-p1.x)*(h-p1.y)/(p2.y-p1.y);
        double area = (q2-q1)*(h-t.y)/2;
        printf ("%.2f\n", fabs(area)+eps);

    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值