# How to find a point on a line closest to another given point?

Given the line $x+2=\frac{y+4}{2}=\frac{z-5}{-2}$
I want to find the closest point on this line to $(1,1,1)$

I suppose the details here don't matter but in general how is this done? We need a vector perpendicular to the line but that also reaches $(1,1,1)$.

-
A line and a point not on that line determine a plane, so you need to find the line perpendicular to your line on that plane. – Harry Stern Dec 5 '10 at 23:47
So I can pick any other line that does not intersect with the above line? – fdart17 Dec 5 '10 at 23:52

One point on the line is $(-2, -4, 5)$ and another is $(0,0,1)$ (found by setting the equation to 0, then to 2). So the general point is $(-2t, -4t, 1+4t)$. You want the dot product of the vector from a point on the line to $(1, 1, 1)$ and a vector along the line to be zero. So $$(-2, -4, 4)\cdot(-2t-1, -4t-1, 4t)=0$$ $$4t+2+16t+4+16t=0$$ $$t=\frac{-1}{6}$$ And the point is $$\left(\frac{1}{3},\frac{2}{3},\frac{1}{3}\right)$$

-

One method is to parametrize the line. So $$\vec{r}(t) = (t-2)\hat{\imath} + (2t-4)\hat{\jmath} + (-2t+5)\hat{k}$$ We can then define the real-valued function $d(t)$ representing the distance from the line to $(1,1,1)$ for any value of $t$. Hence $$d(t) = \sqrt{(t-3)^2 + (2t-5)^2 + (-2t+4)^2} = \sqrt{9t^2 - 42t + 40}$$ To find the minimum, it's now only a single variable optimization problem. So by standard procedure, look at $d'(t)$ $$d'(t) = \frac{9t - 21}{\sqrt{9t^2 - 42t + 40}}$$ The critical point occurs at $t = \frac{7}{3}$, so the closest point is $\vec{r}(\frac{7}{3}) = (\frac{1}{3},\frac{2}{3},\frac{1}{3})$.

-

Using multi-variable calculus you would write a formula for distance from the point (1,1,1) to the arbitrary point (x,y,z) (this second points needs to satisfy your equation since it lies on the line). You would then apply the multi-variable optimization procedure: Find the critical points of this functions (those points where both partial derivatives equal zero or those points where either partial derivative is undefined) and examine these to find the minimum value for distance.

If you do want to use linear algebra, a vector from the point (x,y,z) (on the line) to the point (1,1,1) is $<1-x,1-y,1-z>$ Now take a directional vector from your line and compute the dot product of this with $<1-x,1-y,1-z>$. As you said, this dot product should be zero so solve for x,y, and z.

-
I don't know multi-variable calculus yet! But thanks. I was in fact thinking about optimizing but then I would have to relate this to projections and this would get super messy and probably incalculable. – fdart17 Dec 5 '10 at 23:49

First express $y$ and $z$ in terms of $x$: $y=y(x)$, $z=z(x)$. You want to find the minimum of $f(x) = (x-1)^2 + (y(x)-1)^2 + (z(x)-1)^2$. Noting that $f(x)$ is a parabola, you should find easily the (single) $x$ that minimizes $f(x)$ (global minimum). Then, you get $y(x)$ and $z(x)$ by substitution.

EDIT: Explicitly, $y(x)=2x$ and $z(x)=1-2x$. This leads to $f(x)=9x^2-6x+2$, whose minimum is obtained at $x=1/3$. The solution is then $(x,y,z)=(1/3,2/3,1/3)$, as already given in two previous answers.

-

I know this was answered a long time ago, but I had the same issue and none of the answer would be what I call "plug and use". The answers are correct, but they are not easily programmable. So here is my "plug in" solution allowing a 2 line algorithm. Sorry if the format is bad but I don't know how to do fancy equations.

For the base, I used the above answers and this video: https://www.youtube.com/watch?v=0lG53-ogF2k

Giving:

    p1  any point on the line
a   a vector representing the line
p0  any point in the world
t   a scalar
pt  the closest point on the line


The goal is to find the point pt where the vector p0pt is perpendicular from the line, represented by the a vector. This gives the following:

    a ᵒ p0x = 0
== Definition of a vector
(a.x, a.y, a.z) ᵒ (pt.x - p0.x, pt.y - p0.y, pt.z - p0.z) = 0
== Definition of scalar product
a.x * (pt.x - p0.x) + a.y * (pt.y - p0.y) + a.z * (pt.z - p0.z) = 0
== We know that pt is on the line and must respect pt = p1 + t * a
a.x * (p1.x + t * a.x - p0.x) + a.y * (p1.y + t * a.y - p0.y) + a.z * (p1.z + t * a.z - p0.z) = 0
== We pull the t parts from the parenthesis
t * a.x * a.x + a.x * (p1.x - p0.x) + t * a.y * a.y + a.y * (p1.y - p0.y) + t * a.z * a.z + a.z * (p1.z - p0.z) = 0
== We send all non t related values to the right side of the equation
t * a.x * a.x + t * a.y * a.y + t * a.z * a.z = - a.x * (p1.x - p0.x) - a.y * (p1.y - p0.y) - a.z * (p1.z - p0.z)
== We regroup the t
t * (a.x * a.x + a.y * a.y + a.z * a.z) = - a.x * (p1.x - p0.x) - a.y * (p1.y - p0.y) - a.z * (p1.z - p0.z)
== We isolate t
t = (- a.x * (p1.x - p0.x) - a.y * (p1.y - p0.y) - a.z * (p1.z - p0.z)) / (a.x * a.x + a.y * a.y + a.z * a.z)


Now all you have to do is enter the values in the last formula and you will have your t, allowing to find your point pt by plugging it in the p1 + t * a formula.

In the case of the original poster, and using the answer from Brandon Carter, we can define the line as:

    p1 = (-2, -4,  5)
a  = ( 1,  2, -2)
p0 = ( 1,  1,  1)


If we plug the numbers in the formula:

    t  = (-1 * (-2 - 1) - 2 * (-4 - 1) - -2 * (5 - 1)) / (1 * 1 + 2 * 2 + -2 * -2)
t  = (3 + 10 + 8)/(1+4+4)
t  = 21/9
t  = 7/3
pt = (-2, -4,  5) + 7/3 * ( 1,  2, -2)
pt = (1/3, 2/3, 1/3)

-