An Easy Problem?! (POJ 2826)(计算几何线段直线的相关应用)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yuewenyao/article/details/86079693

题目链接

题目: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、不相交(两条直线平行,即斜率相同)

            2、相交(两直线相交) 

            3、相交且覆盖(高的那块板挡住雨水入口)

如果上边的这几点你都考虑到了,还不过不去,那就要考虑考虑是不是精度的问题了。。。

AC代码如下:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
#define N 100001
#define INF 99999999999.0

int n ;

struct Point
{
    double x , y ;
};

double chacheng(Point p0,Point p1,Point p2)
{
    return(p1.x-p0.x) * (p2.y-p0.y) - (p2.x - p0.x) * (p1.y - p0.y);
}

bool KK(Point x1 , Point y1 , Point x2 , Point y2)
{
    return (max(x1.x,y1.x) >= min(x2.x,y2.x)) &&
            (max(x2.x,y2.x) >= min(x1.x,y1.x)) &&
            (max(x1.y,y1.y) >= min(x2.y,y2.y)) &&
            (max(x2.y,y2.y) >= min(x1.y,y1.y)) &&
            (chacheng(x1,x2,y1) * chacheng(x1,y1,y2) + 1e-10 >= 0) &&
            (chacheng(x2,x1,y2) * chacheng(x2,y2,y1) + 1e-10 >= 0);
}

Point YY(Point u1 , Point u2 , Point v1 , Point v2)
{
    Point ret = u1;
    double t=((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x))/((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x));
    ret.x += (u2.x-u1.x)*t;
    ret.y += (u2.y-u1.y)*t;
    return ret;
}

int main()
{
    int t ;
    cin >>t;
    while(t--)
    {
        Point p[5];
        scanf("%lf%lf%lf%lf",&p[1].x,&p[1].y,&p[2].x,&p[2].y);
        scanf("%lf%lf%lf%lf",&p[3].x,&p[3].y,&p[4].x,&p[4].y);
        if(!KK(p[1],p[2],p[3],p[4]))
        {
            printf("0.00\n");
            continue;
        }
        Point s = YY(p[1],p[2],p[3],p[4]);
        Point a[3];
        int n = 0;   //高于交点的点的个数
        for(int i = 1 ; i < 5 ; i ++)
        {
            if(p[i].y > s.y)
            {
                n ++;
                a[n] = p[i];
            }
        }
        if(n < 2)
        {
            printf("0.00\n");
            continue;
        }
        double ans = 0;

        if(a[1].y > a[2].y)   //第一块高于第二块
        {
            if(fabs(a[1].x - a[2].x) < 1e-10)
            {
                ans = fabs(a[1].x-a[2].x) * fabs(s.y - a[2].y) / 2;
            }
            else
            {
                double k = (a[1].y - s.y) / (a[1].x - s.x);//a[1] > s 的斜率
                double b = a[1].y - k * a[1].x;
                double x = (a[2].y - b) / k ;   //a[1] -> s上纵坐标a[2].y的横坐标
                ans = fabs(a[2].y - s.y) * fabs(a[2].x - x) / 2;
            }
            Point Q;
            Q.x = a[2].x;
            Q.y = 100000.0;
            if(KK(a[1],s,a[2],Q))  //覆盖
            ans = 0.0 ;
        }
            else
            {
                if(fabs(a[2].x-s.x) < 1e-10)
                {
                    ans = fabs(a[1].x - a[2].x) * fabs(s.y - a[1].y) / 2;
                }
                else
                {
                    double k = (a[2].y - s.y) / (a[2].x - s.x);
                    double b = a[2].y - k * a[2].x;
                    double x = (a[1].y - b) / k;
                    ans = fabs(a[1].y - s.y) * fabs(a[1].x - x) / 2;
                }
            Point P;
            P.x = a[1].x;
            P.y = 10000000.0;
            if(KK(a[2],s,a[1],P))
                ans = 0.0;
        }
        printf("%.2f\n",ans);
    }
    return 0 ;
}

猜你喜欢

转载自blog.csdn.net/yuewenyao/article/details/86079693