UVA - 10902 Pick-up Sticks 判断两直线相交

题意:

给出一些线段判断哪些在最上面。

分析:

 

 从而从下到上枚举即可.

#include<bits/stdc++.h>

using namespace std;

const double epsi=1e-8;

const int maxn=1e5+10;

inline int sign(const double &x){
    if(x>epsi) return 1;
    if(x<-epsi) return -1;
    return 0;
}

struct Point{
    double x,y;
    Point(double x=0,double y=0):x(x),y(y){}
    Point operator - (const Point &op2) const {
        return Point(x-op2.x,y-op2.y);
    }
    double operator ^ (const Point &op2) const{
        return x*op2.y-y*op2.x;
    }
};

inline double mul(const Point &p1,const Point &p2,const Point &p3){
    return (p3-p1)^(p2-p1);
}

inline int cross(const Point &p1,const Point &p2,const Point &p3,const Point &p4){
    double a1=mul(p1,p2,p3),a2=mul(p1,p2,p4);
    if(sign(a1)==0&&sign(a1)==0) return 2;      //重合。
    if(sign(a1)==sign(a2)) return 0;
    return 1;
}

Point p1[maxn],p2[maxn];

int main(){
    int n;
    while(scanf("%d",&n),n){
        for(int i=1;i<=n;i++){
            scanf("%lf%lf%lf%lf",&p1[i].x,&p1[i].y,&p2[i].x,&p2[i].y);
        }
        printf("Top sticks:");
        bool f1=false;      //首根能看见棍子的标志。
        for(int i=1;i<=n;i++){
            bool flag=false;
            for(int j=i+1;j<=n;j++){
                if(cross(p1[i],p2[i],p1[j],p2[j])&&cross(p1[j],p2[j],p1[i],p2[i])){
                    flag=true;
                    break;
                }
            }
            if(flag==false&&f1==true) printf(",");
            if(flag==false){
                printf(" %d",i);
                f1=true;
            }
        }
        printf(".\n");
    }
    return 0;
}
/*
5
1 1 4 2
2 3 3 1
1 -2.0 8 4
1 4 8 2
3 3 6 -2.0
3
0 0 1 1
1 0 2 1
2 0 3 1
0
*/

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值