# Distance to a Line

I‘d almost always prefer to solve a problem using what I already know—if that can be done—than learning something I don’t know in order to solve the problem. After that, I’m happy to see how the new learning relates to what I already know. That’s what I’ll do here. There is a way to use the dot product efficiently to determine the distance of a point to a line, but we already know enough to get at it another way, so let’s start there.

So, suppose we know this information about the diagram at the right: $\mathtt{p=}\begin{bmatrix}\mathtt{4}\\\mathtt{2}\end{bmatrix}, \,\,\,\mathtt{x=}\begin{bmatrix}\mathtt{2}\\\mathtt{1}\end{bmatrix}, \,\,\,\mathtt{r=}\begin{bmatrix}\mathtt{-1}\\\mathtt{-3}\end{bmatrix}$ And we want to know the distance $$\mathtt{r}$$ is from the line.

An equation for the distance of $$\mathtt{r}$$ to the line, then—a symbolic way to identify this distance—might be given in words as follows: go to point $$\mathtt{p}$$, then scale to some point on the line. From that point, scale to some point on the vector that is perpendicular to the line until you get to point $$\mathtt{r}$$. In symbols, that could be written as: $\begin{bmatrix}\mathtt{4}\\\mathtt{2}\end{bmatrix}\mathtt{+\,\,\,\, j}\begin{bmatrix}\mathtt{2}\\\mathtt{1}\end{bmatrix}\mathtt{+\,\,\,\,k}\begin{bmatrix}\mathtt{-1}\\\mathtt{\,\,\,\,\,2}\end{bmatrix}\mathtt{\,\,=\,\,}\begin{bmatrix}\mathtt{-1}\\\mathtt{-3}\end{bmatrix}$ With the vector and scalar names, we could write this as $$\mathtt{p + j(p – x) + ka = r}$$. The distance to the line depends on our figuring out what $$\mathtt{k}$$ is. Once we have that, then the distance is just $$\mathtt{\sqrt{(ka_1)^2 + (ka_2)^2}}$$.

We can subtract vectors from both sides of an equation just like we do with scalar values. Subtracting the vector (4, 2) from both sides, we get an equation which can be rewritten as a system of two equations \mathtt{j}\begin{bmatrix}\mathtt{2}\\\mathtt{1}\end{bmatrix}\mathtt{+\,\,\,\,k}\begin{bmatrix}\mathtt{-1}\\\mathtt{\,\,\,\,\,2}\end{bmatrix}\mathtt{\,\,=\,\,}\begin{bmatrix}\mathtt{-5}\\\mathtt{-5}\end{bmatrix} \rightarrow \left\{\begin{align*}\mathtt{2j – k = -5} \\ \mathtt{j + 2k = -5}\end{align*}\right.

Solving that system gives us $$\mathtt{j = -3}$$ and $$\mathtt{k = -1}$$. So, the distance of $$\mathtt{r}$$ to the line is $$\mathtt{\sqrt{5}.}$$

Can We Get to the Dot Product?

Maybe we can get to the dot product. I’m not sure at this point. But there are some interesting things to point out about what we’ve already done. First, we can see that the vector $$\mathtt{j(p-x)}$$ is a scaling of vector $$\mathtt{(p-x)}$$ along the line, which, when added to $$\mathtt{p}$$, brings us to the right point on the line where some scaling of the perpendicular $$\mathtt{a}$$ can intersect to give us the distance. The scalar $$\mathtt{j=-3}$$ tells us to reverse the vector (2, 1) and stretch it by a factor of 3. Adding to $$\mathtt{p}$$ means that all of that happens starting at point $$\mathtt{p}$$.

Then the scalar $$\mathtt{k=-1}$$ reverses the direction of $$\mathtt{a}$$ to take us to $$\mathtt{r}$$.

We can then use this diagram to at least show how the dot product gets us there. We modify it a little to include the parts we will need and talk about.

Okay, here we go. Let’s consider the dot product $$\mathtt{-a \cdot (r – p)}$$. We know that since $$\mathtt{-a}$$ and $$\mathtt{x-p}$$ are perpendicular, their dot product is 0, but this is $$\mathtt{r-p}$$, not $$\mathtt{x-p}$$. So, $$\mathtt{-a \cdot (r – p)}$$ will likely have some nonzero value. Their dot product is this $\mathtt{a \cdot (r – p) = |-a||r-p|\textrm{cos}(θ)}$ We got this by rearranging the formula we saw here.

We also know, however, that we can use the cosine of the same angle in representing the distance, d: $\mathtt{d=|r-p|\textrm{cos}(θ)}$

Putting those two equations together, we get $$\mathtt{d = \frac{a \cdot (r – p)}{|a|}}$$.

We can forget about the negative in front of $$\mathtt{a}$$. But you may want to play around with it to convince yourself of that. A nice feature of determining the distance this way is that the distance is signed. It is negative below the line and positive above it.