POJ 2653 线段相交


code : 


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

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

int n;

struct Point{
    ld x, y;
    Point(ld _x = 0, ld _y = 0):x(_x), y(_y){};
    Point operator - (const Point &rhs) const{
        return Point(x - rhs.x, y - rhs.y);
    }
    ld operator * (const Point &rhs) const{
        return x * rhs.y - y * rhs.x;
    }
};

struct Line{
    Point s, e;
   // Line(Point _s, Point _e):s(_s), e(_e){};
}R[N];

int ok(ld x){
    if(fabs(x) < eps) return 0;
    if(x > 0) return 1;
    return -1;
}

bool check(Line a, Line b){
    if(max(a.s.x, a.e.x) < min(b.s.x, b.e.x)) return false;
    if(max(a.s.y, a.e.y) < min(b.s.y, b.e.y)) return false;
    if(max(b.s.x, b.e.x) < min(a.s.x, a.e.x)) return false;
    if(max(b.s.y, b.e.y) < min(a.s.y, a.e.y)) return false;
    ld l = (a.s - b.s) * (a.e - b.s), r = (a.s - b.e) * (a.e - b.e);
    ld u = (b.s - a.s) * (b.e - a.s), v = (b.s - a.e) * (b.e - a.e);
    return ok(l) * ok(r) <= 0 && ok(u) * ok(v) <= 0;
}
int vis[N];

int main(){
    //freopen("in.txt", "r", stdin);
    while(~scanf("%d", &n), n){
        memset(vis, 0, sizeof vis);
        ld sx, sy, ex, ey;
        for(int i = 1; i <= n; ++i){
            scanf("%lf%lf%lf%lf", &sx, &sy, &ex, &ey);
            R[i].s = Point(sx, sy);
            R[i].e = Point(ex, ey);
        }
        for(int i = 1; i <= n; ++i)
            for(int j = i + 1; j <= n; ++j)
                if(check(R[i], R[j])){
                    vis[i] = 1;
                    break;
                }
        bool f = 0;
        printf("Top sticks: ");
        for(int i = 1; i <= n; ++i) if(!vis[i]) {
            if(f) printf(", %d", i);
            else printf("%d", i), f = 1;
        }
        puts(".");
    }
    return 0;
}


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