# Calculating the Distance of Point from Line

I find it enlightening to figure out by myself the proofs of formulas taught in school. It's not only a nice review of the material learned in school but also an oppurtunity for a more rigourous approach I learned at University, filling holes in knowledge and improving understanding. In this post I'd like to present my proof of the formula which should not differ much from the proofs you've already seen.

## Problem

Find the distance of a given point $P(x_1, y_1)$ on a 2D plane in cartesian coordinates from a given line $L = \{(x,y): Ax+By+C=0\}$, where $A$ and $B$ are not both zero.

## Solution

Distance is given by the following formula.

$$\frac{|Ax_1+By_1+C|}{\sqrt{A^2+B^2}}$$

## Proof

We need to consider three cases: when line $L$ is horizontal, vertical or neither. We'll first solve the problem in the simple cases and later see why the general case doesn't cover neither horizontal nor vertical line. $L'$ denotes a line perpendicular to $L$ in the rest of the proof.

### Steps

For each case we need to find ortographic projection of point $P$ to the line $L$, let's call it point $S$, and then calculate the distance between $P$ and $S$ by using the Euclidean metric $\text{dist}(P_1, P_2) = \sqrt{(x_1-x_2)^2+(y_1-y_2)^2}$. The structure of each following subproofs is as follows:

- Find the line $L'$ perpendicular to $L$ which goes through point $P$.
- Find intersection $S$ between $L$ and $L'$.
- Calculate distance between $S$ and $P$.

These steps are quite intuitive and easy to remember so if you don't remember the formula then you should be able to figure out the steps above to solve your particular problem.

### Case 1: Vertical Line

In other words, $A \neq 0 \land B = 0$ which means the equation for $L$ can be written more simply in the form $x = -\frac{C}{A}$. Notice that $L$ has undefined slope.

Since $L$ is vertical it means the angle between x-axis and $L$ measures $\frac{\pi}{2}$ radians. Therefore the perpendicular line to $L'$ must form an angle of $0$ radians with the x-axis which means it's horizontal and has equation $y' = b$ for some $b$. Since we want $L'$ to go through point $P$, we choose $b = y_1$.

The two lines $L$ and $L'$ meet only at one point $S(-\frac{C}{A}, y_1)$ (the x coordinate must match the equation of vertical line and the y coordinate must match the equation of the horizontal line).

We can now calculate the distance between $S$ and $P$.

$$d = \sqrt{\left( x_1-\left( -\frac{C}{A} \right) \right)^2+(y_1-y_1)^2}=\sqrt{\frac{(Ax_1+C)^2}{A^2}}=\frac{|Ax_1+C|}{\sqrt{A^2}}$$

Which is what we wanted to prove.

## Case 2: Horizontal Line

This time $A = 0 \land B \neq 0$. The equation for $L$ becomes $y=-\frac{C}{B}$. Although $L$ now has defined slope (equal to $0$), $L'$ has not, which is why this case needs to be done seperately.

The reasoning is quite similar to vertical line case. Since $L$ is perpendicular to the x-axis, $L'$ must be parallel to x-axis so it's vertical with equation $x = c$. In order for $L'$ to go through $P$, $c$ must be $x_1$. The point where $L$ and $L'$ meet is $S(x_1, -\frac{C}{B})$. Therefore

$$d = \sqrt{(x_1-x_1)^2+\left( y_1-\left( -\frac{C}{B}\right) \right)^2} = \sqrt{\frac{(By_1+C)^2}{B^2}} = \frac{|By_1+C|}{\sqrt{B^2}}$$.

## Case 3: General

The last case is when $A \neq 0 \land B \neq 0$ which is the most general case. Now both $L$ and $L'$ have defined slopes.

$L$ can be written as slope-intercept equation now: $y=-\frac{A}{B}x-\frac{C}{B}$. It immediately follows that $m_L = -\frac{A}{B}$ is the slope of $L$.

Recall that two lines with defined slopes are perpendicular if and only if $m_L m_{L'} = -1$. Therefore the slope of $L'$ is $m_{L'} = \frac{B}{A}$.

If you don't remember this fact, then forget about the distance for a moment and look at the above picture. It is clear by looking at it that $m_L = \frac{-h}{a}$ from the definition of slope as $\frac{\text{change in y}}{\text{change in x}}$ and $m_{L'} = \frac{a}{h}$ from definition of the slope as $\tan \theta$.

Back to the problem at hand, the equation for $L'$ is $y=\frac{B}{A}x+b$ for some $b$ which we must find. We want this line to go through point $P$ so we can just plug in $x_1$ and $y_1$ in the equation of $L'$ and solve for $b$. This gives us the final (and quite complicated) equation for $L'$:

$$y=\frac{B}{A}x_0+y_1-\frac{B}{A}x_1$$

Now we need to find the intersection $S = (x_0, y_0)$ between $L$ and $L'$. The two lines meet when $y_L = y_{L'} = y_0$ which gives us equation

$$-\frac{A}{B}x_0-\frac{C}{B} = \frac{B}{A}x_0+y_1-\frac{B}{A}x_1$$

Where only unknown is $x_0$. First, add $\frac{A}{B}x_0$ to both sides and use the distributive property to obtain

$$x_0 (\frac{B}{A}+\frac{A}{B}) = -\frac{C}{B}-y_1+\frac{B}{A}x_1$$

Multiply both sides by $AB$.

$$x_0(A^2+B^2)=-AC-AB y_1+B^2 x_1$$

Finally, divide both sides by $A^2+B^2$ and reorder some terms to get the final result. We can do the division since our initial assumption in this case is that $A \neq 0$ and $B \neq 0$, so $A^2 + B^2 \neq 0$.

$$x_0 = \frac{B^2 x_1-AB y_1-AC}{A^2+B^2}$$

In order to find $y_0$ we just plug in $x_0$ in either $y_L$ or $y_{L'}$. $y_L$ looks a little bit more friendly so I'll use this one.

$$y_0 = -\frac{A}{B}(B^2 x_1-AB y_1-AC)\frac{1}{A^2+B^2}-\frac{C}{B}$$

Let's just multiply it out and get $\frac{C}{B}$ to have the denominator $A^2+B^2$.

$$y_0 = \frac{-ABx_1+A^2y_1+\frac{A^2 C}{B}-\frac{C}{B}(A^2+B^2)}{A^2+B^2}$$

Multiply what we can and we get

$$y_0 = \frac{-ABx_1+A^2y_1+\frac{A^2 C-CA^2-CB^2}{B}}{A^2+B^2}$$

Some terms now cancel out and we get the final formula

$$y_0 = \frac{-ABx_1+A^2y_1-CB}{A^2+B^2}$$

Well that was tiring! Unfortunately we're not done yet, we still have to calculate the distance $d=\sqrt{(x_0-x_1)^2+(y_0-y_1)^2}$. Let's split it up a little in order to not get lost in all those multiplications. So let's first see what is $x_0-x_1$

$$x_0-x_1=\frac{B^2 x_1-AB y_1-AC}{A^2+B^2}-x_1$$

Seems like the only thing that makes sense here is to get the $x_1$ to the numerator. Fortunately, something cancels out. Notice how all our expressions look awfully similar to the final formula.

$$x_0-x_1=\frac{-A^2 x_1-AB y_1-AC}{A^2+B^2}$$

We should get $-A$ out from there by using the wonderful distributive property, I can't imagine how algebra would work without it.

$$x_0-x_1=-A\frac{A x_1+B y_1+C}{A^2+B^2}$$

Hey, the multiplicand really starts to resemble what we want. Let's square it while we're at it.

$$(x_0-x_1)^2 = A^2 \left( \frac{A x_1+B y_1+C}{A^2+B^2} \right)^2$$

In order to save space and time I'm not going to show here explicitly how to calculate $(y_0-y_1)^2$ but it looks pretty much the same so you should be able to verify the following result without problems. Actually, you may guess what it'll be without even calculating it.

$$(y_0-y_1)^2 = B^2 \left( \frac{A x_1+B y_1+C}{A^2+B^2} \right)^2$$

Yeah, it's the same except there's $B^2$ in front of it instead of $A^2$. Now the final steps should be quite easy, we can factor out the $\left( \frac{A x_1+B y_1+C}{A^2+B^2} \right)^2$ from their sum

$$(x_0-x_1)^2+(y_0-y_1)^2=\left( \frac{A x_1+B y_1+C}{A^2+B^2} \right)^2 (A^2+B^2)$$

$A^2+B^2$ cancels out with the square in denominator so we get

$$(x_0-x_1)^2+(y_0-y_1)^2 = \frac{(A x_1+B y_1+C)^2}{A^2+B^2}$$

And finally, take the square root (obviously both sides are positive, by the way, so it's legal). However, remember that whatever is under square root must be positive and we have no guarantee that $A x_1+B y_1+C$ is positive so we have to take absolute value.

$$\sqrt{(x_0-x_1)^2+(y_0-y_1)^2} = \frac{|A x_1+B y_1+C|}{\sqrt{A^2+B^2}}$$

Which ends this long proof. $\blacksquare$

The above proof uses analytical geometry. It's not the only way to prove the formula so I may present another one in the future.

What I find interesting is that this proof only follows the algorithm presented at the beginning of the post. It just deals with general cases which involve more work algebraically. Ultimately, the algorithm might be easier to remember but executing it will be more work than just plugging in the numbers in the formula. So generalizing gave us long-term benefit as we can solve this type of problems quite easily now thanks to the formula.

Implementing a function $\text{dist2D(Point }P\text{, Line }L\text{)}$ in a program which needs to do such a calculation is now only matter of simple arithmetic. In some cases (for example when comparing two distances) we don't even need to calculate the square root and absolute value as all we need is square of the distance. This is important because usually multiplication is less expensive than calculating square root.

## Comments