题意:
给出一些线段判断哪些在最上面。
分析:
从而从下到上枚举即可.
#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
*/