1. 程式人生 > >POJ 2826 An Easy Problem?! 叉積求多邊形面積 【計算幾何】

POJ 2826 An Easy Problem?! 叉積求多邊形面積 【計算幾何】

An Easy Problem?!

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7837 Accepted: 1145

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 (x

3, 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

Source

題意:

基礎知識:

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

const double eps = 1e-8;
int sgn(double x)
{
    if(fabs(x) < eps)return 0;
    if(x < 0)return -1;
    else return 1;
}
struct Point
{
    double x,y;
    Point(){}
    Point(double _x,double _y)
    {
        x = _x;y = _y;
    }
    Point operator -(const Point &b)const
    {
        return Point(x - b.x,y - b.y);
    }
    //叉積
    double operator ^(const Point &b)const
    {
        return x*b.y - y*b.x;
    }
    //點積
    double operator *(const Point &b)const
    {
        return x*b.x + y*b.y;
    }
};
struct Line
{
    Point s,e;
    Line(){}
    Line(Point _s,Point _e)
    {
        s = _s;e = _e;
    }
    //兩直線相交求交點
    //第一個值為0表示直線重合,為1表示平行,為0表示相交,為2是相交
    //只有第一個值為2時,交點才有意義
    pair<int,Point> operator &(const Line &b)const
    {
        Point res = s;
        if(sgn((s-e)^(b.s-b.e)) == 0)
        {
            if(sgn((s-b.e)^(b.s-b.e)) == 0)
                return make_pair(0,res);//重合
            else return make_pair(1,res);//平行
        }
        double t = ((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));
        res.x += (e.x-s.x)*t;
        res.y += (e.y-s.y)*t;
        return make_pair(2,res);
    }
};

//判斷線段相交
bool inter(Line l1,Line l2)
{
    return
    max(l1.s.x,l1.e.x) >= min(l2.s.x,l2.e.x) &&
    max(l2.s.x,l2.e.x) >= min(l1.s.x,l1.e.x) &&
    max(l1.s.y,l1.e.y) >= min(l2.s.y,l2.e.y) &&
    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;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        Point in[5];
        for(int i=0;i<4;i++)
            scanf("%lf%lf",&in[i].x,&in[i].y);

        Line L1=Line(in[0],in[1]);
        Line L2=Line(in[2],in[3]);

        if(sgn(L1.s.y-L1.e.y)<0)    swap(L1.s,L1.e);
        if(sgn(L2.s.y-L2.e.y)<0)    swap(L2.s,L2.e);

        if(sgn(L1.s.y-L1.e.y)==0||sgn(L2.s.y-L2.e.y)==0)
        {
            printf("0.00\n");
            continue;
        }

        if(!inter(L1,L2))
        {
            printf("0.00\n");
            continue;
        }

        Line L1y=Line(L1.s,Point(L1.s.x,1e5));
        if(inter(L1y,L2))
        {
            printf("0.00\n");
            continue;
        }

        Line L2y=Line(L2.s,Point(L2.s.x,1e5));
        if(inter(L1,L2y))
        {
            printf("0.00\n");
            continue;
        }

        pair<int,Point> pr=L1&L2;
        Point p=pr.second;

        pr=L1&Line(Point(1e5,L2.s.y),L2.s);
        Point p1=pr.second;
        double ans1=fabs((L2.s-p)^(p1-p))/2.0;

        pr=L2&Line(Point(1e5,L1.s.y),L1.s);
        Point p2=pr.second;
        double ans2=fabs((L1.s-p)^(p2-p))/2.0;
        printf("%.2f\n",min(ans1,ans2));
    }
}