🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Closest Point on a line segment for different distance metrics (i.e L1 norm, max norm)

Started by
5 comments, last by raigan 4 years, 4 months ago

Does anyone have a good reference/suggestions for how to find the point on lineseg ab closest to some query point p, for non-euclidian distance metrics? (Specifically I'd like to know L1 norm and max norm).

(Let me know if this is trivial, I just can't find anything about distance-to-segment queries that isn't L2-norm. tbh I don't super-understand the concept of distance metrics well enough to intuit anything.)


For reference, here's the normal Euclidian-distance aka L2-norm version of what I'm after (a closest-point-on-lineseg query), from: https://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm )

vec2 cpSegment( in vec2 p, in vec2 a, in vec2 b )
{
    vec2 pa = p-a, ba = b-a;
    float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
    return (a + ba*h);
}

I've been having a really hard time finding anything about this online… please let me know what terms I should be googling! ?

Advertisement

(duplicate, deleted)

https://en.wikipedia.org/wiki/Vector_projection

You need to know this. to understand that code

I understand that code – but that gives me the Euclidian distance ("L2 norm"). I should have called it cpSegmentL2().

I want to know how to find the closest point on a line segment when using a different distance metric, specifically “L1 norm” and “max norm aka infinity norm”.

This is what I'm talking about wrt “norms”: https://en.wikipedia.org/wiki/Norm_(mathematics)#Properties

I'm looking for, ideally, a code sample for hypothetical functions cpSegmentL1() and cpSegmentLMax() – as far as I can tell I can't use the L2-norm version to generate either of these.

Firstly, the reason this method works is that the dot product is an inner product satisfying

\(<u,u> = ||u||^2\)

where \(||u||\) is the Euclidean norm on vectors. Unfortunately there is no inner product which satisfies the above equation for the 1-norm or maxnorm so I do not believe there is a simple vector-based solution. Also for the same reason you can have point+line segment combinations that do not have unique closest points (for example some horizontal or vertical line segments for the max norm case).

Ah, okay – that makes sense!

I guess in that case I'd be happy with signed-distance (ie if the closest point isn't unique, the distance to the closest point should still be well-defined). But I don't know how to determine signed distance without a closest point.. I haven't been able to find anything on google for “distance to lineseg under L1/max norm” though.

This topic is closed to new replies.

Advertisement