Intereting Posts

Determining holomorphicity
On some propreties of orthogonal complements
Why this two surfaces have one end?
When does a polynomial take all possible residues modulo any integer?
Which step in this process allows me to erroneously conclude that $i = 1$
Proving a function is an open map
There is no norm in $C^\infty ()$, which makes it a Banach space.
Intersection and union of simply connected domain
Does every Banach space admit a continuous injection to a non-closed subspace of another Banach space?
Probability event with $70$% success rate occurs three consecutive times for sample size $n$
Perron's formula (Passing a limit under the integral)
a.s. Convergence and Convergence in Probability
Inverse Laplace transformation using reside method of transfer function that contains time delay
Plane intersecting line segment
If the dual spaces are isometrically isomorphic are the spaces isomorphic?

Imagine I have a line segment defined by endpoints $p_1$ and $p_2$, and some 3-space coordinate $q$.

Is there a robust (in the sense of never giving divide-by-zero errors) way to quickly determine the distance between the point and line segment?

Update – The neat answer provided by julien seems to provide the distance to a line, not a line segment as specified in the problem description.

- Volume of n-dimensional convex hull
- Point reflection over a line
- filling an occluded plane with the smallest number of rectangles
- How to find the intersection of the area of multiple triangles
- Calculating volume of convex polytopes generated by inequalities
- Can a polygon with minimal perimeter self-intersect?

- Equivalence of geometric and algebraic definitions of conic sections
- Methods for showing three points in $\mathbb{R}^2$ are colinear (or not)
- Geometrical Probability
- Why are second order linear PDEs classified as either elliptic, hyperbolic or parabolic?
- Implementation of Monotone Cubic Interpolation
- Average distance between two random points in a square
- Why isn't the volume formula for a cone $\pi r^2h$?
- Unique perpendicular line
- Parabola and a line
- If 5 points are necessary to determine a conic, why are only 3 necessary to determine a parabola?

1) I will first show how to compute the distance between $p$ and the line $(p_1,p_2)$.

Let $u$ be the vector $\vec{p_1p_2}$ and let $v$ be the vector $\vec{p_1q}$.

You want to find the orthogonal projection $p$ of $q$ on the line.

This is given by the formula

$$

p=p_1+\frac{(u,v)}{\|u\|^2}u.

$$

Once you have $p$, you distance is simply the distance between $q$ and $p$, namely

$$

d(q,p)=\|\vec{qp}\|.

$$

Note: $(u,v)$ denotes the Euclidean inner-product and $\|u\|=\sqrt{(u,u)}$ the Euclidean norm.

2) Now let us consider the distance to the segment $[p_1,p_2]$. Recall that $p$ is the orthogonal projection of $q$ on the line.

There are three cases:

a) The projection $p$ belongs to $[p_1,p_2]$, then your distance is $d(q,p)=\|\vec{qp}\|$.

b) The projection $p$ belongs to $(-\infty,p_1)$, the infinite portion of the line which starts at $p_1$ excluded and does not contain $p_2$. Then your distance is $d(q,p_1)=\|\vec{qp_1}\|$.

c) The projection $p$ belongs to $(p_2,+\infty)$, the infinite portion of the line which starts at $p_2$ excluded and does not contain $p_1$. In this case, it is $d(q,p_2)=\|\vec{qp_2}\|$.

3) How to make this an algorithm.

3.1) Compute

$$

\frac{(u,v)}{\|u\|^2}.

$$

3.2) If this is in $[0,1]$, you are in case a), so compute $p$ and return $d(q,p)=\|\vec{qp}\|$.

3.3)If this is negative, you are in case b), so return $d(q,p_1)=\|\vec{qp_1}\|$.

3.4) If this is greater than $1$, you are in case c), so the answer is $d(q,p_2)=\|\vec{qp_2}\|$.

I believe this is robust, since this never leads to a division by $0$.

Well for a segment you have to consider three situations. If the solution p is in the segment then it is the projection. Otherwise the closest point is either p_1 or p_2 depending on which is closer to p.

- Sum of tangent functions where arguments are in specific arithmetic series
- References for relations between and classification of different set systems?
- Compactness of an Integral operator
- Algebraic Integers in a Cyclotomic Field
- Meaning of floor symbols in $f(x) = \dfrac{1}{2 \lfloor x \rfloor – x + 1}$
- Evaluation of $\sqrt{\frac12+\sqrt{\frac14+\sqrt{\frac18+\cdots+\sqrt{\frac{1}{2^n}}}}}$
- How many permutations of a word do not contain consecutive vowels?
- Cardinality of equivalence relations in $\mathbb{N}$
- Compact subspace of a covering space
- Why does “convex function” mean “concave *up*”?
- Cube stack problem
- Chance of meeting in a bar
- bijection between $\mathbb{N}$ and $\mathbb{N}\times\mathbb{N}$
- $ \int_{0}^{ \infty} \int_{0}^{ \infty} \frac { e^{-(x+y)}}{x+y} dx dy $
- EGF of rooted minimal directed acylic graph