白书p258
struct point
{
double x,y;
point(double a=0,double b=0):x(a),y(b)
{
}
};
struct line
{
point a,b;
int id;
line()
{
}
line(point c,point d):a(c),b(d)
{
}
};
const double eps=1e-10;
int dcmp(double x)
{
if(fabs(x)<eps)
return 0;
else
return x<0?-1:1;
}
bool operator ==(point a,point b)
{
return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;
}
point operator -(point a,point b)
{
return point(a.x-b.x,a.y-b.y);
}
point operator +(point a,point b)
{
return point(a.x+b.x,a.y+b.y);
}
double cross(point a,point b)
{
return a.x*b.y-a.y*b.x;
}
double dot(point a,point b)
{
return a.x*b.x+a.y*b.y;
}
bool onsegment(point p,point a1,point a2)
{
return dcmp(cross(a1-p,a2-p))==0&&dcmp(dot(a1-p,a2-p))<0;
}
bool segmentintersec(point a1,point a2,point b1,point b2)
{
double c1=cross(a2-a1,b1-a1),c2=cross(a2-a1,b2-a1),c3=cross(b2-b1,a1-b1),c4=cross(b2-b1,a2-b1);
if(dcmp(c1)*dcmp(c2)<0&&dcmp(c3)*dcmp(c4)<0)
{
return true;
}
//以上是规范相交 不规范相交需加入下列代码 还需要判断是否端点是重合的
//如果是重合说明也是非规范相交
if(dcmp(c1)==0&&onsegment(b1,a1,a2))
{
return true;
}
if(dcmp(c2)==0&&onsegment(b2,a1,a2))
{
return true;
}
if(dcmp(c3)==0&&onsegment(a1,b1,b2))
{
return true;
}
if(dcmp(c4)==0&&onsegment(a2,b1,b2))
{
return true;
}
return false;
}