计算几何——poj1410,线段不规范交

直接用kuangbin的板子,能判不规范,规范和不交

另外线段在矩形内也可以,判断方式是比较线段的端点和矩形四个角

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>

using namespace std;
const double eps = 1e-18;
int sgn(double x){if (fabs(x)<eps) return 0; return (x>0)?1:-1;}
struct Point{
    double x,y;
    Point(int _x = 0,int _y = 0):x(_x),y(_y){}
    bool operator == (Point b)const{
        return sgn(x - b.x) == 0 && sgn(y - b.y) == 0;
    } 
    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;
    }
    Point operator +(const Point &b)const{
        return Point(x+b.x,y+b.y);
    }
    Point operator *(const double &k)const{
        return Point(x*k,y*k);
    }
    Point operator /(const double &k)const{
        return Point(x/k,y/k);
    }
};

struct Line{
    Point s,e;
    int segcrossseg(Line v){
        int d1 = sgn((e - s)^(v.s - s));
        int d2 = sgn((e - s)^(v.e - s));
        int d3 = sgn((v.e - v.s)^(s - v.s));
        int d4 = sgn((v.e - v.s)^(e - v.s));
        if( (d1^d2)== - 2 && (d3^d4)== - 2 )
            return 2;
        return (d1==0 && sgn((v.s - s)*(v.s - e))<=0) ||
               (d2==0 && sgn((v.e - s)*(v.e - e))<=0) ||
               (d3==0 && sgn((s - v.s)*(s - v.e))<=0) ||
               (d4==0 && sgn((e - v.s)*(e - v.e))<=0);
    }
};

int n;
Line a,b,c,d,line;
double x1,Y1,x2,y2,x3,y3,x4,y4;
int main(){
    scanf("%d",&n);
    while(n--){
        scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&x1,&Y1,&x2,&y2,&x3,&y3,&x4,&y4);
        line.s = Point{x1,Y1}; line.e = Point{x2,y2};
        a.s = Point{x3,y3}; a.e = Point{x3,y4};
        b.s = Point{x3,y4}; b.e = Point{x4,y4};
        c.s = Point{x4,y4}; c.e = Point{x4,y3};
        d.s = Point{x4,y3}; d.e = Point{x3,y3};
        if (line.segcrossseg(a) || line.segcrossseg(b)
            || line.segcrossseg(c) || line.segcrossseg(d)
            || (sgn(fabs(x1-x3)+fabs(x1-x4)-fabs(x3-x4))==0 
                && sgn(fabs(Y1-y3)+fabs(Y1-y4)-fabs(y3-y4))==0)) puts("T");
        else puts("F");
    }
}

猜你喜欢

转载自www.cnblogs.com/zsben991126/p/10924709.html