# Understanding the distance between a line and a point in 3D space

I know that there are quicker ways to do what I am about to present. But I want to understand why my approach does not work.

Let the point $$P = (-6, 3, 3)$$ and the line $$L=(-2t,-6t,t)$$.

I am trying to find the shortest distance between the point and the line. From my observation, I believe the line passes through the origin because it can be written as $$L=\begin{bmatrix}0 \\ 0 \\ 0 \end{bmatrix} +t \begin{bmatrix}-2 \\ -6 \\ 1 \end{bmatrix}$$.

Let $$Q$$ denoted $$(a, b, c)$$ be a point on $$L$$ such that $$\vec{QP}$$ is the shortest distance between $$L$$ and $$P$$. Note that $$\vec{QP}$$ is normal to $$L$$.

Therefore, I need to find $$\vec{QP}$$ which is $$\vec{P}-\vec{Q}$$.

$$\vec{QP} = (-6 - a, 3 - b, 3 -c)$$

We know that $$\vec{QP}$$ and $$L$$ are perpendicular so the dot product is 0.

$$-2(-6 - a) - 6(3 - b) + (3 - c) = 0$$

Simplifying gives us $$2a + 6b - c -3 = 0$$

let $$a=0$$, $$b=1$$, then by solving we know that $$c=3$$.

From my understanding, we should have found $$Q$$ which intersects $$L$$ and $$\vec{QP}$$. Unfortunately, it seems $$||\vec{QP}||$$ is not the correct answer. I think that the way I managed to pull out the $$a$$, $$b$$ and $$c$$ is the culprit, however I just don't understand what I did wrong.

You have ignored completely the constraint $$Q$$ being a point on $$L$$. That imposes $$b=3a$$, for example, which your proposed $$a=0,b=1$$ does not satisfy.

You need to find the point $$Q$$ on $$L$$ minimizing the distance from $$P$$. Since $$Q=(a,b,c)$$ lies in $$L$$, $$a,b,c$$ must satisfy the relations $$a=-2c$$ and $$b=-6c$$. Further, $$QP$$ must be orthogonal to $$L$$, so $$(a+6,b-3,c-3) \cdot (-2,-6,1)=0$$.

Let $$v = (-6,3,3), u = (-2,-6,1)$$

$$v - \frac {u\cdot v}{\|u\|^2} u$$

Describes a vector from a the line defined by $$(0,0,0) + ut$$ to the point $$v$$ that is orthogonal to $$u.$$

$$\|v - \frac {u\cdot v}{\|u\|^2} u\|$$ will be your distance.

$$\big(v - \frac {u\cdot v}{\|u\|^2} u\big)\cdot\big(v - \frac {u\cdot v}{\|u\|^2} u\big) = \|v\|^2 - \frac {(u\cdot v)^2}{\|u\|^2}$$

Alternatively.

$$d^2 = (-6+2t)^2 + (3+6t^2) + (3-t)^2\\ d^2 = 54 +6t +41t^2\\ d^2 = 41(t + \frac {3}{41})^2 - \frac {9}{41} + 54$$

Distance is minimized when $$t = -\frac {3}{41}$$

and $$d^2 = 54 - \frac 9{41}\\ d = \sqrt {54 - \frac 9{41}}$$

It is worth noting that

$$54 = (-6,3,3)\cdot(-6,3,3) = \|v\|^2\\ -3 = (-6,3,3)\cdot(-2,-6,1) = u\cdot v\\$$

and $$41 = (-2,-6,1)\cdot(-2,-6,1) = \|u\|^2$$

You want to minimize the distance $$D$$, but it is easier to minimize $$D^2 = (2t-6)^2+(6t+3)^2+(t-3)^2$$

Differentiate to get $$4(2t-6)+12(3+6t)+2(t-3)=0$$ Solve for $$t$$ to get $$t= \frac {-3}{41}$$

That gives you the point on line $$Q=(\frac {6}{41},\frac {18}{41}, \frac {-3}{41})$$