Intereting Posts

Show that the number or $r$ combinations of $X$ which contain no consecutive integers is given by $\binom{n-r+1}{r}$
Existence of group of order $p$ in group of order $pq$, $p>q$
Is $f(z)=\exp (-\frac{1}{z^4})$ holomorphic?
Motivation for triangle inequality
Show $\mathbb{E} \geq \mathbb{E}\mathbb{E}$ for $f,g$ bounded, nondecreasing
Derivative of the $f(x,y)=\min(x,y)$
Fredholm Alternative as seen in PDEs, part 2
Singular points
How fast does the function $\displaystyle f(x)=\lim_{\epsilon\to0}\int_\epsilon^{\infty} \dfrac{e^{xt}}{t^t} \, dt $ grow?
sum of binomial coefficients involving $n,p,q,r$
limit of integral $n\int_{0}^{1} x^n f(x) \text{d}x$ as $n\rightarrow \infty$
Is it true that $f(x,y)=\frac{x^2+y^2}{xy-t}$ has only finitely many distinct positive integer values with $x$, $y$ positive integers?
Is there a symbol to mean 'this is undefined'?
Two definitions of Taylor polynomials
Prove: $ \frac{1}{\sin 2x} + \frac{1}{\sin 4x } + \cdots + \frac{1 }{\sin 2^n x} = \cot x – \cot 2^n x $

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.

- Circle Packing Algorithm
- Did I write the right “expressions”?
- Properties of quadrilaterals resulting from perspective projection of rectangle of known aspect ratio
- Is it possible to solve any Euclidean geometry problem using a computer?
- How to find the third coordinate of a right triangle given 2 coordinates and lengths of each side
- Point reflection over a line

- Strong equidistribution of points on the n-sphere
- What is the difference between a variety and a manifold?
- volume of a truncated cone that is not a frustum
- Is Euclid's Fourth Postulate Redundant?
- Area of the field that the cow can graze.
- Circle on sphere
- Can someone give me the spherical equation for a 26 point star?
- Why are second order linear PDEs classified as either elliptic, hyperbolic or parabolic?
- Finding a diagonal in a trapezoid given the other diagonal and three sides
- Two Circles and Tangents from Their Centers Problem

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.

- Calculating an improper integral as a limit of a sum.
- Proving $\lim \limits_{n\to +\infty } \left(1+\frac{x}{n}\right)^n=\text{e}^x$.
- Show that $f$ is differentiable at point $x \not= (0,0)$ – $h(x) = (\sin ||x||)^p \cos \frac{1}{||x||}$
- $|G|=12$ and it is isomorphic to $A_4$?
- Problem of Graph connectivity with degree sequences
- the numer of partial function between two sets in combinatoric way
- Two theories proving each others' consistency, take 2
- Proof: A loop is null homotopic iff it can be extended to a function of the disk
- Favourite applications of the Nakayama Lemma
- Evaluating $\int^1_0 \frac{\log(1+x)\log(1-x) \log(x)}{x}\, \mathrm dx$
- Is every function $f : \mathbb N \to \mathbb N$ a composition $f = g\circ g$?
- A problem on skew-symmetric matrix
- Every integer vector in $\mathbb R^n$ with integer length is part of an orthogonal basis of $\mathbb R^n$
- What is the value of $\prod_{i=1}^\infty 1-\frac{1}{2^i}$?
- Proving the Schwarz Inequality for Complex Numbers using Induction