线段相交模板

时间:2020-05-20 14:38:48   收藏:0   阅读:45
const double eps=1e-8;
struct point{
    double x,y;
    void input(){
      scanf("%lf%lf",&x,&y);
    }
};
struct segment{
    point a,b;
    void input(){
       a.input();
       b.input();
    }
    double direction(point p1,point p2,point p3){
        return (p2.x-p1.x)*(p3.y-p2.y) - (p3.x - p2.x)*(p2.y - p1.y);
    }
    bool judge(segment u){
        if(min(a.y,b.y)>max(u.a.y,u.b.y))return 0;
        if(max(a.y,b.y)<min(u.a.y,u.b.y))return 0;
        if(min(a.x,b.x)>max(u.a.x,u.b.x))return 0;
        if(max(a.x,b.x)<min(u.a.x,u.b.x))return 0;
        double d1 = direction(a,b,u.a);
        double d2 = direction(a,b,u.b);
        double d3 = direction(u.a,u.b,a);
        double d4 = direction(u.a,u.b,b);
        return (d1*d2<eps&&d3*d4<=eps);
    }
}

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!