Find perpendicular distance from point to line in 3D?
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||$$
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);
}
If you are familiar with cross-product, you can get the required distance by calculating $$\frac{|\overrightarrow{BA}\times\overrightarrow{BC}|}{|\overrightarrow{BC}|}$$
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.