# Easiest way to find the (shortest) distance between a point and a line in $3$-space

I have tried doing some research on this and am looking for the easiest way to compute this distance. For example, Let $l$ be the line determined by $x=y=z$. Find the shortest distance from this line to the point $(a, b, c)$. What is the easiest way to approach this?

Edit: Form is not important, assume we know the direction vector $\vec v$ and a point on the line $P$.

• Do you only care about lines of the form $dx=ey=fz$, or was your line $l$ only an example? If so, what form will the line be specified in, as I suspect that will affect which method is fastest? – Fengyang Wang Aug 10 '14 at 2:37
• The form is not relevant to me. It could be in vector, scalar, or symmetric form. – MathMajor Aug 10 '14 at 2:47

First, draw a picture. Let $Q$ be the point $(a,b,c)$ and let $\theta$ be the angle between the vectors $\vec{v}$ and $\vec{PQ}$. Then, the point $R$ on the line $\ell$ which is closest to $Q$ is the point such that $\angle QRP = 90^{\circ}$, i.e. $QR$ is perpendicular to $\ell$. Then, the shortest distance from $Q$ to $\ell$ is $\|\vec{QR}\| = \|\vec{PQ}\|\sin\theta$. Using the formula $\|\vec{PQ} \times v\| = \|\vec{PQ}\|\|\vec{v}\|\sin\theta$, we get $\|\vec{QR}\| = \|\vec{PQ}\|\sin\theta = \dfrac{\|\vec{PQ} \times v\| }{\|v\|}$.

Alternatively, Google "distance between a point and a line" and click on the Wikipedia article.

• Would this be equivalent to the scalar projection $\operatorname{comp}_\vec{v}\vec{PQ}$? – MathMajor Aug 10 '14 at 3:03
• Did you mean $\text{comp}_{\vec{v}}\vec{PQ}$? That would give $\|\vec{PR}\|$ not $\|\vec{QR}\|$. – JimmyK4542 Aug 10 '14 at 3:03
• Yes, would that work? – MathMajor Aug 10 '14 at 3:07

Here's another way to do this:

Every line can be parametrized as $\alpha(t) = \mathbf{v} + \mathbf{w}t$ for some vectors $\mathbf{v}, \mathbf{w} \in \mathbb{R}^3$.

From here, the square of the distance from a given point $P$ to some point on the line is given by the function $f(t) = (P_1 - v_1 - w_1t)^2 + (P_2 - v_2 - w_2t)^2 + (P_3 - v_3 - w_3t)^2$.

Simply take the derivative of $f$ and set it equal to $0$ to find the $t$ that minimizes $f$. Indeed, that value will minimize the square of the distance between the point of the line, and thus will also minimize the distance itself.

Note that letting $f$ be the square of the distance eliminates having to deal with yucky square roots while preserving the correct answer.

Footnote: The line $x = y = z$ would be parametrized as $\alpha(t) = \langle t, t, t \rangle$.

Suppose the line passes through the point $\mathbf{P}$ and is in the direction of the unit vector $\mathbf{U}$. Denote the given point by $\mathbf{Q}$.

The point $\mathbf{N}$ on the line that's nearest to $\mathbf{Q}$ is given by $$\mathbf{N} = \mathbf{P} + \big[(\mathbf{Q} - \mathbf{P}) \cdot \mathbf{U}\big] \mathbf{U}$$

You can confirm this just by checking that $(\mathbf{N} - \mathbf{Q}) \cdot \mathbf{U} = 0$, which means that the vector $\mathbf{N} - \mathbf{Q}$ is perpendicular to the line.

Then the distance $d$ from $\mathbf{Q}$ to the line is just the length of the vector $\mathbf{N} - \mathbf{Q}$. This can also be calculated as $$d = (\mathbf{Q} - \mathbf{P}) \times \mathbf{U}$$