# The distance to a point from an arbitrary line

### An Exercise in Making Things Easier for Yourself

The problem: you have a point, and a line, and want to find the distance from the point to the line, specifically the shortest distance that meets that requirement.

Given: the point $(a_x,a_y)$ and the line $y=mx+b$ as in the picture, we want to find the point of intersection $(p_x,p_y)$ so we can then find the distance from $a$ to $p$.

### Don’t reinvent the wheel (use what you already know)

Things we remember1 (hopefully) that could be useful but we won’t prove or explain here:

1. The shortest distance from a point to a line is along the line that is perpendicular to the given line, and passes through the given point. This is a pretty good guess based on the picture, and conveniently enough happens to be true.
2. Perpendicular lines have negative reciprocal slopes. This one isn’t so obvious, and maybe I’ll have an entry for it someday, but not today.

If we do nothing to make the problem easier, then things will get ugly. Therefore, that’s exactly what we will do! After all, whoever learned something by watching someone else do it the easiest way first?

### The plan

A little planning first: we’ll find the equation of the red line, then use that to find the point of intersection, then use the Pythagorean Theorem to find the distance, and last of all try to clean things up a little bit. Also, relax! This is a first draft, so with a little luck we will be able to clean it up a bit.

### The Red Line

Let’s find the equation of the red line. We know that it has slope $-\frac{1}{m}$ (the negative reciprocal of $m$) and goes through the point $(a_x,a_y)$, so let’s do a little math and find an equation that fits. Find $\color{purple}b$, which is the $y$-intercept of the red line:

\begin{align*} y&=-\frac{1}{m}x+\color{purple}b \\ a_y&=-\frac{1}{m}a_x+\color{purple}b \\ a_y&=-\frac{a_x}{m}+\color{purple}b \\ a_y+\frac{a_x}{m}&=\color{purple}b \\ \end{align*}

We start with the equation of some line having slope $-\frac{1}{m}$, then put in the point $(a_x,a_y)$ because we know that has values of $a_x$ and $a_y$ that make the equation true. The key here is to recognize that when we know what everything is except for $b$, that tells us we should probably solve for $b$. Now we can write the equation of our line with slope $-\frac{1}{m}$ and passing through point $(a_x,a_y)$ as:

\begin{align} y=-\frac{1}{m}x + \frac{a_x}{m}+a_y \label{a1} \end{align}

### Point of Intersection

Great! Now all we have to do is find the point of intersection and then use the Pythagorean Theorem to find the distance between the two points. There are lots of ways to find the intersection of two lines, we’ll just use one of them. According to $\eqref{a1}$, another way to write $y$ is as $-\frac{1}{m}x + \frac{a_x}{m}+a_y$, so let’s do a little substitution:

\begin{align} {\color{blue}y}&={\color{blue}-\frac{1}{m}x + \frac{a_x}{m}+a_y} \notag \\ {\color{blue}y}&=mx+b \label{a2} \\ {\color{blue}-\frac{1}{m}x + \frac{a_x}{m}+a_y}&=mx+b \notag \\ \end{align}

Then all we need to do is to solve for $\color{red}x$:

\begin{align*} \def\x{{\color{red}x}} -\frac{1}{m}\x + \frac{a_x}{m}+a_y&=m\x +b \\ \frac{a_x}{m} + a_y - b &= m\x + \frac{1}{m}\x \\ \frac{a_x}{m} + \frac{ma_y}{m} - \frac{mb}{m} &= \x (m+\frac{1}{m}) \\ \frac{a_x + ma_y - mb}{m} &= \x (m\cdot\frac{m}{m}+\frac{1}{m}) \\ \frac{a_x + ma_y - mb}{m} &= \x (\frac{m^2+1}{m}) \\ \frac{a_x + ma_y - mb}{m}\cdot\frac{m}{m^2+1}&=\x \\ \frac{a_x + ma_y - mb}{m^2+1}&=\x \\ \end{align*}

Now we know what $x$ must be…brilliant! Since we know what $\color{red}x$ is now, let’s just substitute that into $\eqref{a2}$ and see what we get for $y$:

\begin{align*} y&=m{\color{red}x} +b \\ y&=m{\color{red}\frac{a_x + ma_y - mb}{m^2+1}}+b \\ y&=\frac{ma_x + m^2a_y - m^2b}{m^2+1}+b \\ \end{align*}

The point of intersection is a little messy:

$\left(\frac{a_x + ma_y - mb}{m^2+1}, \frac{ma_x + m^2a_y - m^2b}{m^2+1}+b\right)$

### Last step: Pythagorean Theorem

Well, no one ever said this was going to be easy, but we’re a little crazy so let’s just keep going. One Pythagorean Theorem coming right up! (To be fair, I have a bad feeling about this…)

\begin{align} d^2&=(\Delta x)^2 + (\Delta y)^2 \notag \\ d^2&=\left( \frac{a_x + ma_y - mb}{m^2+1} - a_x \right)^2 + \left(\frac{ma_x + m^2a_y - m^2b}{m^2+1}+b-a_y \right)^2 \notag \\ d^2&=\left( \frac{a_x + ma_y - mb}{m^2+1} - \frac{a_x(m^2+1)}{m^2+1} \right)^2 + \left(\frac{ma_x + m^2a_y - m^2b}{m^2+1}+\frac{(b-a_y)(m^2+1)}{m^2+1} \right)^2 \label{d1} \\ d^2&=\left( \frac{a_x + ma_y - mb - a_x(m^2+1)}{m^2+1} \right)^2 + \left(\frac{ma_x + m^2a_y - m^2b - (b-a_y)(m^2+1)}{m^2+1} \right)^2 \notag \\ (m^2+1)^2\cdot d^2&=( a_x + ma_y - mb - a_x(m^2+1) )^2 + (ma_x + m^2a_y - m^2b + (b-a_y)(m^2+1) )^2 \notag \\ (m^2+1)^2\cdot d^2&=( a_x + ma_y - mb - m^2a_x - a_x )^2 + (ma_x + m^2a_y - m^2b + m^2b + b - m^2a_y - a_y )^2 \notag \\ (m^2+1)^2\cdot d^2&=( ma_y - mb - m^2a_x )^2 + (ma_x + b - a_y )^2 \label{d2} \\ (m^2+1)^2\cdot d^2&=( -m(-a_y + b + ma_x ) )^2 + (ma_x + b - a_y )^2 \notag \\ (m^2+1)^2\cdot d^2&= (-m)^2 (- a_y + b + ma_x )^2 + (ma_x + b - a_y )^2 \notag \\ (m^2+1)^2\cdot d^2&= m^2 (ma_x - a_y + b)^2 + (ma_x + b - a_y )^2 \label{d3} \\ (m^2+1)^2\cdot d^2&= (m^2+1) (ma_x - a_y + b)^2 \notag \\ (m^2+1)\cdot d^2&= (ma_x - a_y + b)^2 \notag \\ d^2&= \frac{(ma_x - a_y + b)^2}{m^2+1} \notag \\ d&= \frac{ma_x - a_y + b}{\sqrt{m^2+1}} \label{d4} \\ \end{align}

…and there’s our answer. Wow, that was really ugly. The answer isn’t too bad, but getting from the third line through about the sixth really took being careful.

### Reflection: What went well and what didn’t

End of first draft. Looking back, what could we have done better? To be fair, up on line three, I considered multiplying everything out to make it even tougher, but I’m too lazy for that. Leaving it as it was probably helped, considering that the $m^2+1$ ended up factoring out and then canceling out, but the bigger prize was the other factor: $ma_x – a_y + b$. If I had multiplied out the squares at $\eqref{d1}$, I’m not sure I would have found the factors in $\eqref{d2}$ and $\eqref{d3}$.

What could we have done differently? The math was mostly straightforward: we used a few well-known formulas, did some substitution, and solved for a variable. The biggest obstacle was that it was a little more involved than solving $2x+7 = 5x-2$ for $x$. If I could find a way to reduce the number of variables involved, that would really help.

### Using Zero!

The best way I know of to make something disappear is to define it as being ZERO. Since the origin of a graph is defined as being $(0,0)$, why not take advantage of that? Moving the origin won’t change much, and it may reduce what we need to do.

Whoops! I don’t think that line has the same equation now. It still has the same slope, but the $y$-intercept has changed. When we moved the origin from $(0,0)$ to $(1,4)$, we effectively subtracted 4 from every $y$ value and 1 from every $x$ value. Let’s do a little colorful line graphing to figure out how to fix our equation. We know we want the blue line to pass through (on this graph) $(2,-3)$ and we want to use the 1 and 4 to get there.

To get the purple line, subtract 1 from the $x$-intercept (it sounds a little odd, but bear with me). How much did the $y$-intercept change? It moved up from -1 to $-\frac{1}{3}$, a change of $+\frac{2}{3}$. Coincidence? As one of my students once said, “Nah…there are no coincidences in math.” That looks suspiciously like we added $ma_x$ to $b$. Then to get the green line, we subtract 4 from the $y$-intercept. That change is pretty straightforward.

So to fix the equation of our line, we do the same:

\begin{align*} y&=mx+b \\ y&=mx+b \,{\color{purple}+\,ma_x}\, {\color{green}-\,a_y} \\ \end{align*}

Referring to my student above, is it really coincidence that our new $b$ value is $ma_x – a_y + b$, the same expression that shows up in $\eqref{d4}$?

### Shorter math is easier

Now that we have a new equation, and some convenient zeroes, let’s do this again, with $p$ being a point that just happens to be on both lines:

\begin{align*} p_y&=-\frac{1}{m}p_x \qquad && \text{(red line)} \\ p_y&=mp_x + (b+ma_x-a_y) &&\text{(blue line)} \\ -\frac{1}{m}p_x &= mp_x + (b+ma_x-a_y) \\ -(b+ma_x-a_y) &= mp_x+\frac{1}{m}p_x \\ a_y-b-ma_x &= p_x\left(m+\frac{1}{m}\right) \\ a_y-b-ma_x &= \frac{m^2+1}{m}p_x \\ p_x&=\frac{m(a_y-b-ma_x)}{m^2+1} &&\text{(x)} \\ p_y&=-\frac{1}{m}p_x \\ p_y&=-\frac{1}{m}\frac{m(a_y-b-ma_x)}{m^2+1} \\ p_y&=\frac{b+ma_x-a_y}{m^2+1} &&\text{(y)} \\ d^2&=\left(\frac{m(a_y-b-ma_x)}{m^2+1}\right)^2 + \left(\frac{b+ma_x-a_y}{m^2+1}\right)^2 \\ (m^2+1)^2\cdot d^2&=(m(a_y-b-ma_x))^2 + (b+ma_x-a_y)^2 \\ \end{align*}

…and we are back to $\eqref{d3}$. That was much easier!

(moral of the story: zero is your friend!)

(Visited 80 times, 1 visits today)