POJ 2826 An Easy Problem?!

POJ 2826 An Easy Problem?!

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, x1, y1, x2, y2, x3, y3, x4, y4. (x1, y1), (x2, y2) are the endpoints of one board, and (x3, y3), (x4, y4) 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,直接输出0,下图为交点以上的点为0和1的情况:
在这里插入图片描述
[2] 有一点与y轴平行,算面积,图如下:
在这里插入图片描述
[3]正常三角形面积的计算,如下图:
在这里插入图片描述
[4] 有一条边覆盖另一条边的情况,输出0,图如下:
在这里插入图片描述

#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;
typedef long long ll;

double eps=1e-8;//精度
struct point{
double x,y;
point(double a=0,double b=0) {x=a; y=b;}
}p[5];

struct segment
{
 point s;
 point e;
segment(point a,  point b) { s=a; e=b;}
segment() { }
};

double cross(point a,point b,point c){
    return (a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x);
}

bool intersect(segment u,segment v){//线段相交
    return( (max(u.s.x,u.e.x)>=min(v.s.x,v.e.x))&&                     //排斥实验
   (max(v.s.x,v.e.x)>=min(u.s.x,u.e.x))&&
   (max(u.s.y,u.e.y)>=min(v.s.y,v.e.y))&&
   (max(v.s.y,v.e.y)>=min(u.s.y,u.e.y))&&
   (cross(v.s,u.e,u.s)*cross(u.e,v.e,u.s)+eps>=0)&&         //跨立实验
   (cross(u.s,v.e,v.s)*cross(v.e,u.e,v.s)+eps>=0));
}

point intersection(point s1,point e1,point s2,point e2) {
    point ret=s1;
    double t=((s1.x-s2.x)*(s2.y-e2.y)-(s1.y-s2.y)*(s2.x-e2.x))
             /((s1.x-e1.x)*(s2.y-e2.y)-(s1.y-e1.y)*(s2.x-e2.x));
    ret.x+=(e1.x-s1.x)*t;
    ret.y+=(e1.y-s1.y)*t;
    return ret;
}

int main(){
    int t;
    cin>>t;
    while(t--){
        for(int i=1;i<=4;i++) cin>>p[i].x>>p[i].y;
        segment u,v;
        u.s=p[1],u.e=p[2];
        v.s=p[3],v.e=p[4];
        if(!intersect(u,v)) {puts("0.00");continue;}
        point P=intersection(p[1],p[2],p[3],p[4]);
        point a[3];
        int num=0;
        for(int i=1;i<=4;i++){
            if(p[i].y>P.y) a[++num]=p[i];
        }
        if(num<2) {puts("0.00");continue;}//[1]
        double ans=0;
        if(a[2].y>a[1].y) swap(a[1],a[2]);
            if(fabs(a[1].x-P.x)<eps){//[2]
                ans=fabs(a[1].x-a[2].x)*fabs(P.y-a[2].y)/2;
            }
            else{//[3]
                double k=(a[1].y-P.y)/(a[1].x-P.x);
                double b=a[1].y-k*a[1].x;
                double x=(a[2].y-b)/k;
                ans=fabs(a[2].y-P.y)*fabs(a[2].x-x)/2;
            }

        point pp;pp.x=a[2].x,pp.y=1000000.0;
        u.s=a[1],u.e=P,v.s=a[2],v.e=pp;
        if(intersect(u,v)) {puts("0.00");continue;}//[4]
        printf("%.2f\n",ans+eps);//不加eps用G++交会WA
    }
    return 0;
}
发布了288 篇原创文章 · 获赞 14 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_43765333/article/details/104355864