POJ 2653 线段相交


code : 


#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <algorithm>
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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值