Search code examples
c++computational-geometry

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


I am using a piece of code I have found on the internet (here) to compute the distance between a point and a segment. Here is the code:

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);
}

When computing double b = c1 / c2; c2 is dot(v, v) (so, the norm of v squared). Shouldn't we use norm(v)? Isn't that the proper definition of the projection of a vector on another one?

Thanks.


Solution

  • Actually the definition is with norm(v) squared. So dot(v, v) is correct.

    Here's a nice and short explanation: http://math.oregonstate.edu/home/programs/undergrad/CalculusQuestStudyGuides/vcalc/dotprod/dotprod.html