【问题标题】:Line segments intersection, numerically stable test线段相交,数值稳定测试
【发布时间】:2013-02-07 16:36:14
【问题描述】:

我需要对 2D 中的 2 条线段相交进行精确且数值稳定的测试。有一种可能的解决方案检测 4 个位置,请参见下面的代码。

getInters ( double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, double & x_int, double & y_int  )
{
    3: Intersect in two end points,
    2: Intersect in one end point,
    1: Intersect (but not in end points)
    0: Do not intersect 

unsigned short code = 2;

//Initialize intersections
x_int = 0, y_int = 0;

//Compute denominator
    double denom =  x1 * ( y4 - y3 ) + x2 * ( y3 - y4 ) + x4 * ( y2 - y1 ) + x3 * ( y1 - y2 ) ;

    //Segments are parallel
if ( fabs ( denom ) < eps)
    {
            //Will be solved later
    }

//Compute numerators
    double numer1 =     x1 * ( y4 - y3 ) + x3 * ( y1 - y4 ) + x4 * ( y3 - y1 );
double numer2 = - ( x1 * ( y3 - y2 ) + x2 * ( y1 - y3 ) + x3 * ( y2 - y1 ) );

//Compute parameters s,t
    double s = numer1 / denom;
    double t = numer2 / denom;

    //Both segments intersect in 2 end points: numerically more accurate than using s, t
if ( ( fabs (numer1) < eps)  && ( fabs (numer2) < eps) || 
     ( fabs (numer1) < eps)  && ( fabs (numer2 - denom) < eps) ||
     ( fabs (numer1 - denom)  < eps)  && ( fabs (numer2) < eps) || 
     ( fabs (numer1 - denom) < eps) &&  ( fabs (numer2 - denom) < eps) )
    {
            code =  3;
    }

//Segments do not intersect: do not compute any intersection
    else if ( ( s < 0.0 ) || ( s > 1 ) || 
      ( t < 0.0 ) || ( t > 1 ) )
    {
            return  0;
    }

    //Segments intersect, but not in end points
    else if ( ( s > 0 ) && ( s < 1 ) && ( t > 0 ) && ( t < 1 ) )
    {
            code =  1;
    }

//Compute intersection
x_int = x1 + s * ( x2 - x1 );
y_int = y1 + s * ( y2 - y1 );

//Segments intersect in one end point
return code;
 }

我不确定是否所有建议的条件都设计正确(以避免圆度错误)。

将参数 s、t 用于测试或仅用于交叉点的计算是否有意义?

恐怕位置2(段相交在一个端点)可能无法正确检测到(最后剩下的情况没有任何条件)...

【问题讨论】:

  • 想法:第一次检查退化案例(平行、事件或不相交)。第二次计算交点。第三次检查交叉点是否在任一段上,如果是,在哪里。如果你能负担得起使用有理数而不是实数,你甚至可以得到一个精确的答案。

标签: c++ testing intersection numerical


【解决方案1】:

这似乎是一个非常常见的数学问题。有一个很好的教程,上面有关于 topcoder 的公式可以回答你的问题,并且可以很容易地用你想要的任何编程语言来实现基础知识:Line Intersection Tutorial

问候, 叶夫根尼亚

【讨论】:

    【解决方案2】:
    if(fabs(denom) < eps){
        if((fabs(len(x2, y2, x3, y3) + len(x2, y2, x4, y4) - len(x3, y3, x4, y4)) < eps) || (fabs(len(x1, y1, x3, y3) + len(x1, y1, x4, y4) - len(x3, y3, x4, y4)) < eps) || (fabs(len(x3, y3, x1, y1) + len(x3, y3, x2, y2) - len(x1, y1, x2, y2)) < eps) || (fabs(len(x4, y4, x1, y1) + len(x4, y4, x2, y2) - len(x1, y1, x2, y2)) < eps)){
          return 1;
        }else{
          return 0;
        }
    }
    

    在哪里len = sqrt(sqr(c - a) + sqr(d - b))

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-31
      • 2011-01-04
      • 1970-01-01
      • 1970-01-01
      • 2012-09-02
      • 2014-11-27
      • 2016-01-25
      相关资源
      最近更新 更多