两线段相交的判断(跨立实验法)

来源:互联网 发布:联想手机截图软件 编辑:程序博客网 时间:2024/06/06 00:51

精度的控制

第一种方法: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)//说明两线段相交}


 

原创粉丝点击