POJ 3304 直线与线段相交判断

code:

#include 
#include 
#include 
using namespace std;
typedef double ld;

const ld eps = 1e-8;
const int N = 110;

struct line{
    ld p[2][2];
}l[N];

struct vec{
    ld x, y;
    vec(ld _x, ld _y):x(_x), y(_y){};
    ld operator * (const vec &rhs) const{
        return x * rhs.y - y * rhs.x;
    }/**叉积*/
};

int n;
bool f;

bool check(ld sx, ld sy, ld ex, ld ey, int ii, int ij){
    if(fabs(ex - sx) < eps && fabs(ey - sy) < eps) return false;/**重点*/
    for(int i = 1; i <= n; ++i){
        if(i == ii || i == ij) continue;
        ld la = vec(l[i].p[0][0] - sx, l[i].p[0][1] - sy) * vec(ex - sx, ey - sy);
        ld ra = vec(l[i].p[1][0] - sx, l[i].p[1][1] - sy) * vec(ex - sx, ey - sy);
        if(la * ra > 0) return false;/**直线和线段相交la * ra <= 0*/
    }
    return true;
}

void solve(){
    for(int i = 1; i <= n; ++i){
        for(int ii = 0; ii < 2; ++ii)/**枚举端点*/
            for(int j = 1; j <= n; ++j){
                //if(j == i) continue;
                for(int ij = 0; ij < 2; ++ij){
                    if(check(l[i].p[ii][0], l[i].p[ii][1], l[j].p[ij][0], l[j].p[ij][1], i, j)){
                        f = 1;
                        break;
                    }
                }
            }
    }
}

int main(){

    int t;
    cin >> t;
    while(t--){
        f = 0;
        cin >> n;
        for(int i = 1; i <= n; ++i) cin >> l[i].p[0][0] >> l[i].p[0][1] >> l[i].p[1][0] >> l[i].p[1][1];
        solve();
        if(f) cout << "Yes!" << endl;
        else cout << "No!" << endl;
    }
    return 0;
}

你可能感兴趣的:(POJ,数学-计算几何)