2019.2.12更新,以前第二条判断完全是错的。。。怎么就过了。。。
第一步:先判断一条线段所在直线与另外一条线段相交
第二步:再判断另外一条线段所在直线与此线段相交
一下代码只能说明有重合部分,并未求出线段交点
#include<cstdio>
#include<cstring>
#include<cmath>
#define maxl 100010
#define eps 1e-8
using namespace std;
inline int sgn(double x)
{
if(fabs(x)<eps) return 0;
if(x<0)
return -1;
else
return 1;
}
struct point
{
double x,y;
point(double a=0,double b=0)
{
x=a;y=b;
}
point operator - (const point &b)const
{
return point(x-b.x,y-b.y);
}
//按照x坐标排序
bool operator < (const point &b)const
{
return x<b.x-eps;
}
bool operator == (const point &b)const
{
return sgn(x-b.x)==0 && sgn(y-b.y)==0;
}
friend point operator * (const point &a,const double &b) //数乘
{
return point(b*a.x,b*a.y);
}
friend point operator * (const double &a,const point &b)
{
return point(a*b.x,a*b.y);
}
void transxy(double &sinb,double &cosb)
{ //逆时针旋转,给出sin,cos
double tx=x,ty=y;
x=tx*cosb-ty*sinb;
y=tx*sinb+ty*cosb;
}
void transxy(double &b)//逆时针旋转b弧度
{
double tx=x,ty=y;
x=tx*cos(b)-ty*sin(b);
y=tx*sin(b)+ty*cos(b);
}
double norm()
{
return sqrt(x*x+y*y);
}
};
inline double det(const point &a,const point &b)
{ //叉积
return a.x*b.y-a.y*b.x;
}
inline double dot(const point &a,const point &b)
{ //点积
return a.x*b.x+a.y*b.y;
}
inline double dist(const point &a,const point &b)
{
return (a-b).norm();
}
struct line
{
point s,e;
line(point a=point(),point b=point())
{
s=a;e=b;
}
};
int n;
line a[maxl];
int ans[maxl];
bool top[maxl];
inline void prework()
{
point p1,p2;
for(int i=1;i<=n;i++)
{
scanf("%lf%lf%lf%lf",&p1.x,&p1.y,&p2.x,&p2.y);
a[i].s=p1;a[i].e=p2;
top[i]=true;
}
}
inline bool parallel(line &l1,line &l2)
{
return !sgn(det(l1.e-l1.s,l2.e-l2.s));
}
inline bool point_on_seg(point &p,line &a)
{
return sgn(det(p-a.s,p-a.e))==0 && sgn(dot(p-a.s,p-a.e))<=0;
}
inline bool cross(line l1,line l2)
{
if(parallel(l1,l2))//覆盖的情况
return point_on_seg(l1.s,l2) || point_on_seg(l1.e,l2)
|| point_on_seg(l2.s,l1) || point_on_seg(l2.e,l1);
line vec=line(l1.s,l1.e-l1.s);
if(det(l2.s-vec.s,vec.e)*det(vec.e,l2.e-vec.s)<0-eps)
return false; //经过端点是0,算相交
vec=line(l2.s,l2.e-l2.s);
if(det(l1.s-vec.s,vec.e)*det(vec.e,l1.e-vec.s)<0+eps)
return false;
return true;
}
inline void mainwork()
{
ans[0]=0;
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
if(cross(a[i],a[j]))
{
top[i]=false;
break;
}
if(top[i])
ans[++ans[0]]=i;
}
}
inline void print()
{
printf("Top sticks:");
for(int i=1;i<=ans[0];i++)
printf(" %d%c",ans[i],(i==ans[0])?'.':',');
puts("");
}
int main()
{
while(~scanf("%d",&n) && n)
{
prework();
mainwork();
print();
}
return 0;
}