Find point on line closest to another given point. Find the point on the line $x=[1,1,1]+t[1,2,3],\ t \in \mathbb{R}$, that is closest to the point $[0,0,1]$.
How do you find this point?
Thank you very much.
 A: Denote that point as $A(x_1,x_2,x_3) = [1+t, 1+2t, 1+3t]$, so 
$$
d^2 = (t+1)^2+(2t+1)^2 + 9t^2
$$
should have vanishing first derivative.
$$
\left ( d^2 \right )' = 2(t+1) + 4(2t+1) + 18t = 28t + 6 = 0 \implies t = -\frac 3{14}
$$
Since second derivative is positive everywhere, that point is minimum.
$$
A = \left [ 1 - \frac 3{14}, 1 - \frac 37, 1 - \frac 9{14} \right ] = \frac 1{14}\left [11, 8, 5 \right ]
$$
A: I discuss the details elsewhere [ http://recklessreckoner.blogspot.com/2013/02/perpendicular-distance-ii-its-all.html ] , but it can be shown that the perpendicular distance from an external point to a point on the line is the shortest distance to the line (the number of dimensions involved is immaterial).   So the vector from the closest point on the line $ \  (1,1,1) \ + t \ < 1  ,  2 ,  3 > \  $  to the point $ \ (0, 0, 1 ) \ $  is perpendicular  to the direction of the line $ \  < 1, 2, 3 > \ $ .  So we can solve the dot-product equation
$$ < 1 , 2 , 3 > \ \cdot \ < (1 + t) - 0 \ , \ (1 + 2t) - 0 \ , \ (1 + 3t) - 1 > \ = \ 0   $$
$$ \Rightarrow  \ ( 1 + t ) + ( 2 + 4t ) + 9t  \ =  0  \ , $$
which leads to the same result for  $ \ t \ $  and for the coordinates of the closest point as found by  Kaster , $ \ ( \ \frac{11}{14} \ , \ \frac{8}{14} \ , \ \frac{5}{14} \ ) \ $ . 
A: This question already has some perfectly valid answers, however, I was looking for something that can easily be translated into code and should work for any line and point combination that is unknown at compile time.
A different way to view this problem is the intersection of a plane and a line since the minimal distance is always perpendicular to the line and all lines perpendicular to a line in 3D space form a plane.
This means you take the direction of the line and use it as the plane normal of a plane going through the point for which you want to find the closest point on the line.
In the following, $p_0$ is the point for which we want to find the closest point to the line given by its origin $l_0$ and its direction $l$ (and $\cdot$ is the dot product).  
$$x=l_0 + t * l \quad \text{with} \quad t \in \mathbb{R}$$
We form a plane where the set of points $p$ on the plane fulfills
$$(p-p_0) \cdot l = 0$$
Now, we can simply find the closest point by finding the point where the line and the plane cross which is a single point because the line is orthogonal to the plane by definition. This is done by first finding the distance of the line's origin to the plane:
$$t = \frac{(p_0 - l_0) \cdot l}{l \cdot l}$$
Inserting t into the line equation will give us the closest point.
Example with the given values:
$$
l_0 = \begin{bmatrix}1 \\ 1 \\ 1\end{bmatrix} \quad
l = \begin{bmatrix}1 \\ 2 \\ 3\end{bmatrix} \quad
p_0 = \begin{bmatrix}0 \\ 0 \\ 1\end{bmatrix} \quad
$$
$$
t_{closest} = \frac{
(\begin{bmatrix}0 \\ 0 \\ 1\end{bmatrix} - \begin{bmatrix}1 \\ 1 \\ 1\end{bmatrix}) \cdot \begin{bmatrix}1 \\ 2 \\ 3\end{bmatrix}}
{\begin{bmatrix}1 \\ 2 \\ 3\end{bmatrix} \cdot \begin{bmatrix}1 \\ 2 \\ 3\end{bmatrix}}
= \frac{(-1)*1+(-1)*2+0*3}{1*1+2*2+3*3} = \frac{-3}{14}\\
x_{closest} =
\begin{bmatrix}1 \\ 1 \\ 1\end{bmatrix} +
\frac{-3}{14} * \begin{bmatrix}1 \\ 2 \\ 3\end{bmatrix} = 
\begin{bmatrix}\frac{11}{14} \\ \frac{8}{14} \\ \frac{5}{14}\end{bmatrix}
$$
For more on this see the wikipedia page on line plane intersection
