"Point-Segment" 距离:这段代码不应该使用范数而不是范数平方吗?

"Point-Segment" distance: shouldn't this code use the norm instead of the norm squared?

我正在使用我在 Internet (here) 上找到的一段代码来计算点和线段之间的距离。这是代码:

float
dist_Point_to_Segment( Point P, Segment S)
{
    Vector v = S.P1 - S.P0;
    Vector w = P - S.P0;

    double c1 = dot(w,v);
    if ( c1 <= 0 )
        return d(P, S.P0);

    double c2 = dot(v,v);
    if ( c2 <= c1 )
        return d(P, S.P1);

    double b = c1 / c2;
    Point Pb = S.P0 + b * v;
    return d(P, Pb);
}

当计算double b = c1 / c2; c2是dot(v, v)(所以,v平方的范数).我们不应该使用 norm(v) 吗?这不是一个向量在另一个向量上的投影的正确定义吗?

谢谢。

其实定义是用norm(v)平方的。所以 dot(v, v) 是正确的。

这里有一个简短的解释: http://math.oregonstate.edu/home/programs/undergrad/CalculusQuestStudyGuides/vcalc/dotprod/dotprod.html

如果v归一化,则投影长度为w.v,投影向量为(w.v) v.

由于v出现了两次,非标准化向量的公式为

(w.(v/|v|)) v/|v| = (w.v/|v|²) v

这节省了一个平方根。