判直线和线段相交——poj3304

/*
线段和直线非严格相交:线段两点和直线的叉积 
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
#define N 405
#define db double

const db eps=1e-8;
int sign(db k){
    if (k>eps) return 1; else if (k<-eps) return -1; return 0;
}
int cmp(db k1,db k2){return sign(k1-k2);}

struct point{
    db x,y;
    point operator + (const point &k1) const{return (point){k1.x+x,k1.y+y};}
    point operator - (const point &k1) const{return (point){x-k1.x,y-k1.y};}
    point operator * (db k1) const{return (point){x*k1,y*k1};}
    point operator / (db k1) const{return (point){x/k1,y/k1};}
    int operator == (const point &k1) const{return cmp(x,k1.x)==0&&cmp(y,k1.y)==0;}
    // 逆时针旋转 
    point turn(db k1){return (point){x*cos(k1)-y*sin(k1),x*sin(k1)+y*cos(k1)};}
    point turn90(){return (point){-y,x};}
    bool operator < (const point k1) const{
        int a=cmp(x,k1.x);
        if (a==-1) return 1; else if (a==1) return 0; else return cmp(y,k1.y)==-1;
    }
    db abs(){return sqrt(x*x+y*y);}
    db abs2(){return x*x+y*y;}
    db dis(point k1){return ((*this)-k1).abs();}
    point unit(){db w=abs(); return (point){x/w,y/w};}
    void scan(){double k1,k2; scanf("%lf%lf",&k1,&k2); x=k1; y=k2;}
    void print(){printf("%.11lf %.11lf
",x,y);}
    db getw(){return atan2(y,x);} 
    point getdel(){if (sign(x)==-1||(sign(x)==0&&sign(y)==-1)) return (*this)*(-1); else return (*this);}
    int getP() const{return sign(y)==1||(sign(y)==0&&sign(x)==-1);}
};
struct line{
    point p[2];
    line(){}
    line(point k1,point k2){p[0]=k1; p[1]=k2;}
    point & operator [] (int k){return p[k];}
};
db cross(point k1,point k2){return k1.x*k2.y-k1.y*k2.x;}
db dot(point k1,point k2){return k1.x*k2.x+k1.y*k2.y;}

point p[N];
line lines[N];
int n;

int checkLS(point k1,point k2,point k3,point k4){//判 L(k1,k2) 和 S(k3,k4)交点 
    return sign(cross(k3-k1,k2-k1))*sign(cross(k4-k1,k2-k1))<=0;
}

int check(point k1,point k2){
    if(sign(k1.dis(k2))==0)return false;
    for(int i=1;i<=n;i++)
        if(!checkLS(k1,k2,lines[i][0],lines[i][1]))return 0;
    return 1;
}

int main(){
    int t;cin>>t;
    while(t--){
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            point k1,k2;
            scanf("%lf%lf%lf%lf",&k1.x,&k1.y,&k2.x,&k2.y);
            lines[i]=line(k1,k2);
        }
        
        int flag=0;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++){
                if(check(lines[i][0],lines[j][0]) || check(lines[i][0],lines[j][1]) || 
                    check(lines[i][1],lines[j][1]) || check(lines[i][1],lines[j][0]))
                    flag=1;
            }
        
        if(flag)puts("Yes!");
        else puts("No!"); 
    }
}
/*
2
3
0 0 2 0
0 1 2 1
1 2 1 1
*/
原文地址:https://www.cnblogs.com/zsben991126/p/12318533.html