# Distance between point and a line - problems simplifying the minimised distance equation

Someone asked how to prove the distance between a point $(x_1,y_1)$ and a line $Ax + By + C = 0$ is:$$\text{Distance} = \frac{\left | Ax_{1} + By_{1} + C\right |}{\sqrt{A^2 + B^2} }$$ The currently accepted answer shows that a point on the line can be expressed as $(x, {-Ax-C\over B})$, then explains an approach using calculus to find the value of $x$ when the square distance is minimised, and then plug it into the distance formula: $$\tag{1}D=\sqrt{ (x_1-x)^2 + {\Bigl(y_1- { \textstyle{-Ax-C\over B}}\Bigr)^2 }}$$ and simplify.

I understand the approach. My problem is with the simplification part. I found $x$ to be minimised when $x = \dfrac{B^2x_1 - A(By_1 + C)}{A^2 + B^2}$. How would you plug this into $(1)$ and simplify effectively?

My first approach was to plug $x$ into the two places it appears in the $(1)$ but expanding $(x_1 - \frac{B^2x_1 - A(By_1 + C)}{A^2 + B^2})^2$ (let alone the rest of it) made me give up as it just seemed to get even more complex. I tried some substitutions:

but this route didn't seem to be getting me closer to the formula to be proven.

I understand there are other far simpler ways to arrive at the formula (see other answers to the linked question), but it irked me that my algebraic manipulation skills are too weak to follow through this one. What I'm wondering is how an expert would approach this. How would you break the problem down and proceed?

I was deliberating over whether to post this question because part of me is telling me I'm lazy for not just trying to plough on through it.

• If you're asking about the way of dealing with this specific calculation, you don't try to minimize $D$, but rather $D^2$; this is adequate because $D$ is a non-negative quantity. You avoid working with a radical that way. When you multiply out your binomial-squares, you just have a quadratic function in $x$ representing an "upward-opening" parabola. The vertex of that parabola gives you the value of $x$ at which the minimal value of $D^2$ occurs, and the y-coordinate is the minimal value of $D^2$ . – colormegone May 11 '13 at 13:12
• I understood the bit about minimising $D^2$. The problem I have is the algebraic manipulation/simplification required after plugging the expression for $x$ into the equation for $D$ to arrive at the distance formula. So I get the calculus bit, it's the algebra bit I'm struggling with :) – PeteUK May 11 '13 at 13:24

Since you wanted to see how this is done algebraically, we'll need to look at a particular property of quadratic functions. As I've said in a comment, if $r > 0$, then $f(x) = rx^2 + sx + t \$ represents an "upward-opening" parabola. By completing the square, we can write this as

$$f(x) \ = \ r \cdot (x^2 \ + \ \frac{s}{r} \cdot x \ + \ \frac{s^2}{4r^2}) \ + \ (t \ - \ r \cdot \frac{s^2}{4r^2}) \ = \ r \cdot ( \ x + \frac{s}{2r} \ )^2 \ + \ ( \ t - \frac{s^2}{4r} \ ) \ .$$

The vertex of this parabola lies at $\ x = -\frac{s}{2r} \$ , with the y-coordinate of the vertex being the minimum value of the function $\ f(-\frac{s}{2r}) \ = \ t - \frac{s^2}{4r} .$ We will exploit this to reduce the amount of writing we will need to do.

We will label the closest point on the line to $\ (x_1 , y_1) \$ as $\ (X,Y) \$ . The distance-squared function is

$$D^2 \ = \ ( X - x_1 )^2 \ + \ (Y - y_1)^2 \ = \ ( X - x_1 )^2 \ + \ ( \ [-\frac{AX + C}{B} \ ] - y_1)^2 \$$

$$= \ ( X - x_1 )^2 \ + \ ( \frac{A}{B} \cdot X \ + \ \frac{C}{B} + \ y_1)^2 \ = \ ( X - x_1 )^2 \ + \ ( \frac{A}{B} \cdot X \ + \ K \ )^2 \ ,$$

where we will make a temporary substitution, $\ K = \frac{C+By_1}{B} \$ , to further save some writing. (This is often a good idea when working out complicated algebraic expressions, as it reduces the risk of embarrassing, time-wasting errors.) We will see that we can hold out for some time before needing to replace $K$ .

From here, we have

$$D^2 \ = \ ( \ X^2 \ - \ 2x_1X \ + \ x_1^2 \ ) \ + \ ( \ \frac{A^2}{B^2} \cdot X^2 \ + \ 2\frac{A}{B} K \cdot X \ + \ K^2 \ ) \$$

$$= \ ( \ 1 + \frac{A^2}{B^2} \ ) \cdot X^2 \ + \ 2 ( \ \frac{A}{B} K - x_1 \ ) \cdot X \ + \ ( \ K^2 \ + x_1^2 \ ) ,$$

which now has the "general form" for a quadratic function. Since we already know how to convert this to "standard form" by completing the square, we can simply use the results we discussed back at the start. The vertex of the parabola described by this equation lies at

$$X \ = \ -\frac{s}{2r} \ = \ -\frac{2 ( \ \frac{A}{B} K - x_1 \ )}{2( \ 1 + \frac{A^2}{B^2} \ )} \cdot \frac{B^2}{B^2} \ = \ \frac{ B^2 x_1 \ - \ AB K}{A^2 \ + \ B^2 \ }$$

$$= \ \frac{ B^2 x_1 \ - \ AB \cdot (\frac{C+By_1}{B})}{A^2 \ + \ B^2 \ } \ = \ = \ \frac{ B^2 x_1 \ - \ A \cdot (C \ + \ By_1)}{A^2 \ + \ B^2 \ } \ .$$

So here is the x-coordinate of the point on the line that is closest to $(x_1,y_1)$, confirming the stated result without the use of calculus.

We do not bother substituting this into either the quadratic function or the equation for the line, since we know from the standard form of the quadratic function that its minimal value is

$$D^2_{min} \ = \ t - \frac{s^2}{4r} \ = \ ( \ K^2 \ + \ x_1^2 \ ) \ - \ \frac{[ \ 2 ( \ \frac{A}{B} K - x_1 \ ) \ ]^2}{4( \ 1 + \frac{A^2}{B^2} \ )} \cdot \frac{B^2}{B^2}$$

$$= \ ( \ K^2 \ + \ x_1^2 \ ) \ - \ \frac{ ( \ A K \ - \ Bx_1 \ )^2}{ A^2 + B^2} \ = \ \frac{( \ K^2 \ + \ x_1^2 \ )(A^2 + B^2) \ - \ ( \ A K - Bx_1 \ )^2}{ A^2 + B^2}$$

$$= \ \frac{( \ A^2K^2 \ + \ B^2K^2 \ + \ A^2x_1^2 \ + \ B^2x_1^2 \ ) \ - \ ( \ A^2K^2 \ - \ 2ABKx_1 \ + \ B^2x_1^2 \ )}{ A^2 + B^2}$$

$$= \ \frac{ A^2x_1^2 \ + \ 2ABKx_1 \ + \ B^2K^2 \ }{ A^2 + B^2} \ = \ \frac{ ( \ Ax_1 \ + \ BK \ )^2 }{ A^2 + B^2}$$

$$= \ \frac{ ( \ Ax_1 \ + \ B \cdot [\frac{C+By_1}{B}] \ )^2 }{ A^2 + B^2} \ = \frac{ ( \ Ax_1 \ + \ By_1 \ + C \ )^2 }{ A^2 + B^2} \ .$$

Since $\ \sqrt{x^2} = |x| \ ,$ we at last have the relation for the minimum distance,

$$D_{min} \ = \ \frac{ | \ Ax_1 \ + \ By_1 \ + C \ | }{ \sqrt{A^2 + B^2}} \ .$$

It took me a few goes to find a reasonably clean way to develop this by algebra alone. The straight answer to your question about how "an expert" would set this up and proceed is that they wouldn't, but instead would use a more efficient method (such as the ones I linked to in my other answer). That may also explain why one doesn't have much luck finding an approach like this by looking around the internet. In any event, I hope this is what you were inquiring about.

• This is exactly what I was looking for. I had done exercises before on the "shortcut" way to minimise a quadratic by putting it in vertex form, but had forgotten about that and got there through calculus. I appreciate my memory being refreshed about that. The bit I was really stuck on was plugging $x$ back into the distance formula $D=\sqrt{ (x_1-x)^2 + {\Bigl(y_1- { \textstyle{-Ax-C\over B}}\Bigr)^2 }}$. Continued... – PeteUK May 12 '13 at 14:41
• What you did is still sinking in, but it seems although you found an expression for minimised $x$, you didn't need to do expend effort doing that, let alone plug it in anywhere, as you knew the square distance was minimised at $t - \dfrac{s^2}{4r}$. Is that right? – PeteUK May 12 '13 at 14:44
• That's correct, but this happens to be convenient for quadratic functions because they have only one absolute minimum or maximum (depending upon which way "the parabola is opening"). If we work with the standard form $y = a(x - h)^2 + k$ for a parabola, the vertex is at $(h, k)$, so setting $x = h$ makes the quadratic term zero, hence the value of $y$ is equal to $k$ and the orientation of the parabola ("upward" for $a > 0$, "downward" for $a < 0$) indicates whether $k$ is minimal or maximal. So showing that "k" equals $t - \frac{s^2}{4r}$ allows us to simply use it as the minimal $D^2$. – colormegone May 13 '13 at 19:39
• There is nothing "wrong" with inserting your value of $x$ into the distance formula itself (or even into $D^2$ by removing the radical) -- it just creates a considerable amount of additional manipulation (I know: that was one thing I tried...). But these derivations are about more than "pushing symbols around": it can be important to draw in other insights about the situation, as they may offer ways to reduce the amount of "wrestling" with the calculation. Such insights can also make the meaning of the result clearer. – colormegone May 13 '13 at 19:44