Find perpendicular distance from point to line in 3D?

I have a Line going through points B and C; how do I find the perpendicular distance to A?

$$A= (4,2,1)$$ $$B= (1,0,1)$$ $$C = (1,2,0)$$

enter image description here


Solution 1:

Intuitively, you want the distance between the point A and the point on the line BC that is closest to A. And the point on the line that you are looking for is exactly the projection of A on the line. The projection can be computed using the dot product (which is sometimes referred to as "projection product").

So you can compute the direction vector $\mathbb{d}$ of the line $BC$. This is the difference of $B$ and $C$, divided by their distance:

$$\mathbb{d} = (C-B) / ||C-B||$$

Then you can define a vector from $B$ to $A$:

$$\mathbb{v} = A - B$$

Computing the dot product between this vector and the direction vector will give you the the distance between $B$ and the projection of $A$ on $BC$:

$$ t = \mathbb{v} \cdot \mathbb{d}$$

The actual projection $P$ of $A$ on $BC$ is then given as

$$P = B + t \cdot \mathbb{d}$$

And finally, the distance that you have been looking for is

$$|| P - A||$$

Perpendicular

Of course, this could be written in a somewhat shorter form. It has the advantages of giving you exactly the closest point on the line (which may be a nice add-on to computing only the distance), and it can be implemented easily. Some pseudocode:

double computeDistance(vec3 A, vec3 B, vec3 C) {
    vec3 d = (C - B) / C.distance(B);
    vec3 v = A - B;
    double t = v.dot(d);
    vec3 P = B + t * d;
    return P.distance(A);
}

Solution 2:

If you are familiar with cross-product, you can get the required distance by calculating $$\frac{|\overrightarrow{BA}\times\overrightarrow{BC}|}{|\overrightarrow{BC}|}$$

Solution 3:

You can parameterize the line (and you don't even need to worry about the fact that it’s a segment):

$$B-C=\langle 0,-2,1 \rangle$$ so the line is $$\langle 1,0,1 \rangle+ t\ \langle 0,-2,1 \rangle$$

So for some value of $t$, call it $k$, the vector from $A$ to $\langle 1,-2k,1+k \rangle$ is orthogonal to $\langle 0,-2,1 \rangle$. Thus

$$(\langle 1,-2k,1+k \rangle-\langle 4,2,1\rangle)\cdot \langle 0,-2,1\rangle=0$$

$$\langle -3,-2k-2,k \rangle\cdot \langle 0,-2,1\rangle=0$$

$$4k+4+k=0$$

$$k=-\frac{4}{5}$$

so now you should be able to find the point.

Solution 4:

Hint: A point on the line BC is described by $t B + (1-t)C$, $t\in {\Bbb R}$. Try to minimize its distance to $A$

Solution 5:

Arbitrary point on the line: $(1, 2t , -t)$ for $t \in \mathbb R$. The line is parallel to the vector $\vec{BC} = <0,2,-1>$.

If $D(1, 2t_0,-t_0)$ is the point on the line closest to $A$, we must have $\vec{AD} \bullet \vec{BC} =0$ (because $\vec{AD}$ must be perpendicular to the line)