线段相交(规范+不规范)

白书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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值