计算几何POJ 2826 接水WA哭

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tju_peter/article/details/52154109
An Easy Problem?!
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12375   Accepted: 1897

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

Source

POJ Monthly--2006.04.28, Dagger@PKU_RPWT

水题,考虑特殊情况:
1. 两条线没有交点,结果为0
2. 一个板子与x轴平行结果为0
3. 两条线斜率同号,并且上面的那个板子能盖住下面的板子接不到水,结果为0
此题较坑的地方,导致自己被WA哭,其实至今也没找到原因,在POJ上的discuss上看到结果加一个eps会蜜汁过题,就加了一下结果真的过了。。。仔细考虑可能是会输出-0.00的原因吧
附AC代码
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

const double eps=1e-6;

struct point
{
    double x,y;

};
typedef point vec;

struct line
{
    point a,b;
};

vec operator -(point m,point n)
{
    vec c;
    c.x=m.x-n.x;
    c.y=m.y-n.y;
    return c;
}

vec operator +(point m,point n)
{
    vec c;
    c.x=m.x+n.x;
    c.y=m.y+n.y;
    return c;
}

vec operator * (vec c,int t)
{
    vec m;
    m.x=c.x*t;
    m.y=c.y*t;
    return m;
}
double operator /(vec m, vec n)
{
    return m.x*n.y-m.y*n.x;
}

int cross(line m,line n)
{
    if(((n.a-m.a)/(m.b-m.a))*((n.b-m.a)/(m.b-m.a))<=0
       && ((m.a-n.a)/(n.b-n.a))*((m.b-n.a)/(n.b-n.a))<=0)
        return 1;
    else return 0;
}

point maxpoint(line m)
{
    if(m.a.y>m.b.y)
        return m.a;
    else return m.b;
}

int pallx(line m)
{
    if(m.b.y-m.a.y==0)
        return 0;
    else return 1;
}

double dis(point a,point b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

double sq(point a,point b,point c)
{
    double x=dis(a,b),y=dis(b,c),z=dis(c,a);
    double p=0.5*(x+y+z);
    double s=sqrt(p*(p-x)*(p-y)*(p-z));
    return s;
}

point jiaop(line m,line n)
{
    point o;
    double t=fabs((n.a-m.a)/(n.b-m.a))/fabs((m.b-m.a)/(n.b-n.a));
    o=m.a;
    o.x+=(m.b.x-m.a.x)*t;
    o.y+=(m.b.y-m.a.y)*t;
    return o;
}

int main()
{
    int t;
    cin>>t;
    line m,n;
    while(t--)
    {
        cin>>m.a.x>>m.a.y>>m.b.x>>m.b.y;
        cin>>n.a.x>>n.a.y>>n.b.x>>n.b.y;
        double k1=(m.b.y-m.a.y)/(m.b.x-m.a.x),k2=(n.b.y-n.a.y)/(n.b.x-n.a.x);
        if(pallx(m)==0 || pallx(n)==0)
        {
            cout<<"0.00"<<endl;
            continue;
        }
        else if(cross(m,n)==0)
        {
            cout<<"0.00"<<endl;
            continue;
        }
        else if(k1==k2)
        {
            cout<<"0.00"<<endl;
            continue;
        }
        if(k1*k2>0)
        {
            if(k1>0)
            {
                if(k1>k2)
                {
                    if(max(m.a.x,m.b.x)>=max(n.a.x,n.b.x))
                    {
                        cout<<"0.00"<<endl;
                        continue;
                    }
                }
                else
                {
                    if(max(n.a.x,n.b.x)>=max(m.a.x,m.b.x))
                    {
                        cout<<"0.00"<<endl;
                        continue;
                    }
                }
            }
            else
            {
                if(k1<k2)
                {
                    if(min(m.a.x,m.b.x)<=min(n.a.x,n.b.x))
                    {
                        cout<<"0.00"<<endl;
                        continue;
                    }
                }
                else
                {
                    if(min(n.a.x,n.b.x)<=min(m.a.x,m.b.x))
                    {
                        cout<<"0.00"<<endl;
                        continue;
                    }
                }
            }
        }
            int flag=0;
            double s,y;
            point a,b;
            a=maxpoint(m);
            b=maxpoint(n);
            if(a.y>=b.y)
            {
                flag=1;
                y=b.y;
                a=b;
            }
            else
            {
                y=a.y;
            }
            if(b.y==y)
                a=b;
            b.y=y;
            if(flag==1)
            {
                point i=m.a,j=m.b;
                if(m.b.y>m.a.y)
                {
                    j=m.a;
                    i=m.b;
                }
                double x3=i.x,x4=j.x,y3=i.y,y4=j.y,y2=y;
                b.x=(x4*y3-y2*x4+x3*y2-x3*y4)/(y3-y4);
            }
            else
            {
                point i=n.a,j=n.b;
                if(n.b.y>n.a.y)
                {
                    j=n.a;
                    i=n.b;
                }
                double x3=i.x,x4=j.x,y3=i.y,y4=j.y,y2=y;
                b.x=(x4*y3-y2*x4+x3*y2-x3*y4)/(y3-y4);
            }
            point c=jiaop(m,n);
            cout<<fixed<<setprecision(2)<<sq(a,b,c)+eps<<endl;
    }
    return 0;
}



猜你喜欢

转载自blog.csdn.net/tju_peter/article/details/52154109