poj2826—An Easy Problem?

来源:互联网 发布:陈奕迅不要说话知乎 编辑:程序博客网 时间:2024/05/22 04:31

题目链接:传送门

An Easy Problem?!
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 13629 Accepted: 2093

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, x1y1x2y2x3y3x4y4. (x1y1), (x2y2) are the endpoints of one board, and (x3y3), (x4y4) 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

20 1 1 01 0 2 10 1 2 11 0 1 2

Sample Output

1.000.00


解题思路:各种判断

1.线段相交

2.线段共线

3.某条线段是水平的

4.某条线段覆盖了另一条线段

上述情况都是不可行的


#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <string>#include <stack>#include <queue>using namespace std;typedef long long ll;const int N = 100010;const int M = 1000;const int INF = 0x3fffffff;const double eps = 1e-8;const double PI = acos(-1.0);struct Point{    double x,y;    Point(){}    Point(double _x,double _y){        x = _x; y = _y;    }    Point operator - (const Point&a)const{        return Point(x-a.x,y-a.y);    }    double operator ^ (const Point&a)const{        return x*a.y-y*a.x;    }    double operator * (const Point&a)const{        return x*a.x+y*a.y;    }    void input(){        scanf("%lf%lf",&x,&y);    }};struct Line{    Point s,e;    Line(){}    Line(Point _s,Point _e){        s = _s; e = _e;    }    void getLine(Point _s,Point _e){        s = _s; e = _e;    }};int sgn(double x){    if(fabs(x)<eps) return 0;    if(x < 0) return -1;    else return 1;}bool inter(Line l1,Line l2){    return    max(l1.s.x,l1.e.x) >= min(l2.s.x,l2.e.x) &&    max(l1.s.y,l1.e.y) >= min(l2.s.y,l2.e.y) &&    max(l2.s.x,l2.e.x) >= min(l1.s.x,l1.e.x) &&    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;}//返回线段的交点Point SegInter(Line l1,Line l2){    Point res = l1.s;    double t = ((l1.s-l2.e)^(l2.s-l2.e))/((l1.s-l1.e)^(l2.s-l2.e));    res.x += t*(l1.e.x-l1.s.x);    res.y += t*(l1.e.y-l1.s.y);    return res;}//给定一条直线和x值,返回一个直线上的点Point getPointX(Line L,int x){    double a,b,c;    a = L.s.y-L.e.y;    b = L.e.x-L.s.x;    c = L.s.x*L.e.y-L.e.x*L.s.y;    if(sgn(b) == 0) return Point(x,0.0);    else return Point(x,(-c-a*x)/b);}//给定一条直线和y值,返回一个直线上的点Point getPointY(Line L,int y){    double a,b,c;    a = L.s.y-L.e.y;    b = L.e.x-L.s.x;    c = L.s.x*L.e.y-L.e.x*L.s.y;    if(sgn(a) == 0) return Point(0.0,y);    else return Point((-c-b*y)/a,y);}//得到直线的斜率//返回值://1:斜率为正,0:斜率为0,-1:斜率为负,INF:斜率不存在int getK(Line L){    if( L.s.x == L.e.x ) return INF;    double k = (L.s.y-L.e.y)/(L.s.x-L.e.x);    if( sgn(k) > 0 ) return 1;    if( sgn(k) < 0 ) return -1;    if( sgn(k) == 0 ) return 0;}double getCap(Line l1,Line l2){    //线段不相交    if( !inter(l1,l2) ) return 0.0;    //线段共线    if( sgn((l1.s-l1.e)^(l2.s-l2.e)) == 0 ) return 0.0;    //有一根线段是平的    if( sgn(l1.s.y-l1.e.y) == 0 || sgn(l2.s.y-l2.e.y) == 0) return 0.0;    Point p2 = SegInter(l1,l2);    int flag = sgn((l1.s-p2)^(l2.s-p2));    //某根线段覆盖了另一根线段    if( getK(l1) == 1 && getK(l2) == 1 && flag < 0 && l1.s.x >= l2.s.x ) return 0.0;    if( getK(l1) == -1 && getK(l2) == -1 && flag > 0 && l1.s.x <= l2.s.x ) return 0.0;    Point p1 = getPointY(l1,l2.s.y);    return fabs((l2.s-p2)^(p1-p2))/2.0;}int main(){    int T;    scanf("%d",&T);    while( T-- ){        Point p1,p2;        Line l1,l2;        p1.input(); p2.input();        if( p1.y < p2.y ) swap(p1,p2);        //点l1.s为上面的点        l1.getLine(p1,p2);        p1.input(); p2.input();        if( p1.y < p2.y ) swap(p1,p2);        l2.getLine(p1,p2);        //l1在l2的上面        if( l1.s.y < l2.s.y ) swap(l1,l2);        printf("%.2lf\n",getCap(l1,l2));    }    return 0;}


原创粉丝点击