本文主要是介绍两线段相交的判断(跨立实验法),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
精度的控制
第一种方法:
int dblcmp(double x)
{if(fabs(x)<eps)return 0;return x>0?1:-1;
}
第二种方法:
int dblcmp(double x)
{if(x>eps)return 1;else if(x<-eps)return -1;else return 0;
}
叉积的求解
double det(double x1,double y1,double x2,double y2)
{ //求叉积return x1*y2-x2*y1;
}
向量的求解
double cross(Point a,Point b,Point c)
{return det(a.x-c.x,a.y-c.y,b.x-c.x,b.y-c.y);
}
线段是否相交的判断
bool segcross(Point a,Point b,Point c,Point d)
{int d1,d2,d3,d4;d1=dblcmp(cross(a,b,c));d2=dblcmp(cross(a,b,d));d3=dblcmp(cross(c,d,a));d4=dblcmp(cross(c,d,b));if((d1^d2)==-2&&(d3^d4)==-2)return true;return false;
}
线段先交判断方法二:
double cross(Point a,Point b,Point c)
{return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x);
}
bool judge(Point a,Point b,Point c,Point d)
{if(cross(a,b,c)*cross(a,b,d)<esp)//说明两线段相交
}
double cross(Point a,Point b,Point c)
{return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x);
}
bool judge(Point a,Point b,Point c,Point d)
{if(cross(a,b,c)*cross(a,b,d)<esp)//说明两线段相交
}
这篇关于两线段相交的判断(跨立实验法)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!