POJ 2826 An Easy Problem?! <计算几何>

来源:互联网 发布:如何做淘宝天猫客服 编辑:程序博客网 时间:2024/05/18 03:15

题目

题目大意:墙上有两个等宽的木板(宽度为一个单位),问它们能够接到多少雨水。

分析:就是求能够接到雨水的区域的截面面积,本来这道题没什么思考上的难度的,但是情况有很多种,一开始我就忽略掉了几种能够接到雨水的情况。。代码写了详细的注释,我感觉自己的代码写得还是挺清晰简洁的-.-

代码

#include <iostream>#include <cmath>#include <algorithm>#include <iomanip>using namespace std;const double EPS=1e-8;struct Point;typedef Point Vec;int dblcmp(double x){    return fabs(x)<EPS?0:(x>0?1:-1);}struct Point{    double x,y;    Point(){}    Point(double xx,double yy):x(xx),y(yy){}    Vec operator -(Point p){    //两点相减得向量        return Vec(x-p.x,y-p.y);    }    double operator ^(Vec v){   //叉积        return x*v.y-y*v.x;    }};struct Segment{    Point p1,p2;    Segment(){}    Segment(Point pp1,Point pp2):p1(pp1),p2(pp2){}    bool isParallel(Segment s){ //是否共线        return dblcmp((p2-p1)^(s.p1-p1))==0&&                dblcmp((p2-p1)^(s.p2-p1))==0;    }    bool isCross(Segment s,double& s1,double &s2){  //线段相交判断,允许端点相交,但不允许延长线相交        return max(p1.x,p2.x)>=min(s.p1.x,s.p2.x)&&                max(p1.y,p2.y)>=min(s.p1.y,s.p2.y)&&                max(s.p1.x,s.p2.x)>=min(p1.x,p2.x)&&                max(s.p1.y,s.p2.y)>=min(p1.y,p2.y)&&                dblcmp(s1=((p2-p1)^(s.p1-p1)))*dblcmp(s2=((p2-p1)^(s.p2-p1)))<=0&&                dblcmp((s.p2-s.p1)^(p1-s.p1))*dblcmp((s.p2-s.p1)^(p2-s.p1))<=0;    }    Point intersectionPoint(double s1,double s2){//求交点,定点分比法        double x=(p1.x*s2-s1*p2.x)/(s2-s1);        double y=(p1.y*s2-s1*p2.y)/(s2-s1);        return Point(x,y);    }};Segment s[2];void solve(){    double s1,s2;    Point p[2];    int c=0;    if(s[0].isParallel(s[1])) cout<<"0.00"<<endl;    else if(s[0].isCross(s[1],s1,s2)){        Point o=s[1].intersectionPoint(s1,s2);        for(int i=0;i<2;++i){   //找出在交点上方的点            if(s[i].p1.y>o.y) p[c++]=s[i].p1;            if(s[i].p2.y>o.y) p[c++]=s[i].p2;        }        if(c!=2) cout<<"0.00"<<endl; //交点上方不是两个点,一定不能接到雨水        else{            Vec v1=p[0]-o,v2=p[1]-o;            int top=0;            if(p[1].y>p[0].y) top=1;            Point pl=p[top^1],pt=p[top]; //pt是较高的点            int flag1=dblcmp(fabs(pt.x-o.x)-fabs(pl.x-o.x));//水平方向上,较高点是否离交点更远            int flag2=dblcmp((pt-o)^(pl-o)); //较高点与交点形成的木板,与较低点与交点形成的木板之间的相对位置(逆时针还是顺时针)            int flag3=dblcmp(pt.x-o.x); //当两点位于交点同侧是,是在右侧还是左侧            int flag=dblcmp(flag1*flag2*flag3); //综合影响。画一画图就会发现,当两点位于交点同侧时,以上三个判断的结果相乘大于0才能接到雨水            if(dblcmp(v1.x*v2.x)==1&&flag<=0) //只有当两点位于交点同侧时,才需判断flag                cout<<"0.00"<<endl;            else{                double yg=pt.y,yl=pl.y;                double r=(yl-o.y)/(yg-o.y); //能接到雨水的区域所占的比例                cout<<fixed<<setprecision(2)<<fabs((v1^v2)*r/2.0)<<endl;            }        }    }    else cout<<"0.00"<<endl; //没有交点}int main(){    ios::sync_with_stdio(false);    int n;    cin>>n;    while(n--){        double x1,y1,x2,y2;        for(int i=0;i<2;++i){            cin>>x1>>y1>>x2>>y2;            s[i]=Segment(Point(x1,y1),Point(x2,y2));        }        solve();    }    return 0;}
原创粉丝点击