Distance from point to line segment

Started by
3 comments, last by Cornstalks 15 years, 9 months ago
I'm trying to get the distance from a point to a line segment. Just to clear up any confusion, I mean line segment, not an infinite line. Is it possible to find such a distance without an if/else test? All the examples I've seen find a parametric value (for the infinite line) that corresponds to the distance, and then the parametric value is clamped to [0, 1] (which clamps it to the line segment's end points) and the distance adjusted. The problem with that is that it requires you to test if the parametric value is outside the range [0, 1]. Basically I'm wondering if there is an equation that can find the distance from a point to a line without an if/else test. I understand if one doesn't exist, I doubt one would, but I figured people here might be able to tell me for sure. Thanks in advance!
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Advertisement
That depends on what you consider "an equation". If you are allowed to use max(a,b) in the equation, sure you can find one. But it probably won't have any advantages over using if/else. What's your problem with using if/else? Optimizing prematurely, are we?

Quote:Original post by alvaro
That depends on what you consider "an equation". If you are allowed to use max(a,b) in the equation, sure you can find one. But it probably won't have any advantages over using if/else. What's your problem with using if/else? Optimizing prematurely, are we?


No, I'm trying to do a swept sphere vs line segment intersection test (it's 2D, so more like swept circle). I've been going through this paper to do it, but I'm not quite sure how to implement section 3.5 (actually he loses me in section 3.2 when he starts using projections and denotes things with "prime" but I'm not quite sure how C'(t) is different from C(t)). I've got section 3.1 working, but 3.1 deals with swept sphere vs infinite lines, which would totally screw my game up since I need to test segments, not infinite lines. I figured that since that doesn't work for me, I'd try to figure it out myself (after hours of googling, of course). The idea is rather simple, just find the two t's for which the distance between the sphere's center (C(t)) and the line segment equals the sphere's radius. Unfortunately, I can't find or think of a general equation that wouldn't require me to do the test I mentioned above, which totally kills the possibility of such a simple solution.

I could do a line vs capsule test, but that wouldn't work for me since I need to be able to reposition the moving sphere to the point where the intersection begins.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Why do you think that a solution with a test or that use a function like min or max isn’t a simple solution?

In your case the circle can intersect the segment in the vertices or in a point between them. To calculate the correct distance you can simply calculate the three distances and then take the minimum of them. A really doubt an equation like the one you want exists.
Quote:Original post by apatriarca
Why do you think that a solution with a test or that use a function like min or max isn’t a simple solution?

In your case the circle can intersect the segment in the vertices or in a point between them. To calculate the correct distance you can simply calculate the three distances and then take the minimum of them. A really doubt an equation like the one you want exists.


That's what I thought, but I wasn't sure. I'm not saying using min/max isn't necessarily simple. You just can't really write it in an abstract way. Think of it like a regular function vs a piecewise function. Even if the regular function contains more complex junk inside it, the piecewise function requires special treatment because of its multiple sections.

Thanks for the help, I've started my work on implementing a test that checks both endpoints, then the infinite line, and then makes whatever decision is right based on the results of those three tests.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

This topic is closed to new replies.

Advertisement