# Given a line and a point in 3D, how to find the closest point on the line?

I have a point given by $P = (P_x, P_y, P_z)$, and a line give by the two points $Q = (Q_x, Q_y, Q_z)$ and $R = (R_x, R_y, R_z)$. I'd like to know a general formula to figure out the closest point to $P$ that resides on the line $\overleftrightarrow{QR}$. I know that this involves finding a perpendicular line to $\overleftrightarrow{QR}$, but I don't know where to go from there.

Edit: Based on Emilio's answer, I came up with this formula to derive the point; $$\hat{t}=(R-Q)\cdot(Q-P)/(R-Q)\cdot(R-Q)$$ Where $\hat{t}$ is a scalar magnitude that can be used in the formula $G=Q+\hat{t}(R-Q)$ to derive the point $G$ on the line that is closest to point $P$.

• In your second equation, I think you want Q - t(R - Q). – Bob Oct 5 '18 at 19:22

Hint:

find the plane orthogonal to the given line and passing thorough the point $P$. The intersection of this plane with the line is the searched point.

• Ah, thanks! That makes quite a bit of sense. – Maurdekye Nov 9 '15 at 18:07

What you can do is: 1.Take any general point on the given line in terms of any parameter. Let it be S(x,y,z). (Basically write the equation in cartesian form and then take x,y,z in the form of any parameter (let it be 'l'))

2.Now you have the vector PS.

3.As PS is perpendicular to the given line, it must be perpendicular to the direction ratios of the vector that the given line is parallel to.

4.Take the dot product of vector PS with the direction ratios of the given line and equate it to 0.(Dot product of non-zero perpendicular vectors is 0).

5.Now you have the parameter 'l' in terms of which you wrote the co-ordinates of S.

6.You now have the co-ordinates of S, so simply use distance formula to find the reqd. distance.