Calculating the Distance of a Point From a 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 opportunity for a more rigorous 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 by Using Simple Algebra

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 orthographic 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:

1. Find the line $L'$ perpendicular to $L$ which goes through point $P$.
2. Find intersection $S$ between $L$ and $L'$.
3. 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$

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.

Proof by Using Linear Algebra

The proof above is not very elegant and short. Here’s where the wonders of the linear algebra come in. We’ll even prove a more general statement with less work!

Find the distance of a given point $P \in \mathbb{R}^n$ from a given hyperplane $L = \{(x_1,x_2,\ldots, x_n): A_1 x_1 + A_2 x_2+ \cdots + A_n x_n + B=0\}$, where $A_1^2 + A_2^2 + \cdots + A_n^2 > 0$.

Hyperplane for $n = 2$ is a fancy name for a line, and for $n = 3$ it’s a plane.

Our strategy is similar to the one used in the proof above: project the point $P$ onto the hyperplane $L$ and calculate the distance between $P$ and the projection.

Recall that the vector $\vec{n} = (A_1, A_2, \ldots, A_n)$ is orthogonal to the hyperplane. This can be verified by checking that $\vec{n} \bullet \vec{u} = 0$, where $\bullet$ means dot product and $\vec{u}$ is a direction vector of the hyperplane. $\vec{u}$ can be easily found by choosing any two different points $P_1, P_2$ on the hyperplane and calculating the difference $P_2 - P_1 = \vec{u}$.

Hence, $\vec{n}$ gives us a line $L'$ perpendicular to the hyperplane $L$. We’re interested in only one of such lines, the one intersecting $P$. This line can be easily writtten in a parametric form.

$L' = \{P + t \vec{n}: t \in \mathbb{R} \}$

The intersection of $L$ and $L'$ is the point $P' = P + t \vec{n}$, the projection of $P$, we are looking for. Since $P'$ lies on $L$ it has to satisfy the hyperplane equation $A_1 x_1 + \cdots + A_n x_n + B = 0$. Notice that the only missing variable is $t$ so it’s only a matter of solving a single equation.

$\begin{gathered} A_1 (P_1 + t A_1) + A_2 (P_2 + t A_2) + \cdots + A_n (P_n + t A_n) + B = 0 \\ A_1 P_1 + A_2 P_2 \cdots + A_n P_n + t (A_1^2 + A_2^2 + \cdots + A_n^2) + B = 0 \\ \vec{n} \bullet P + t \lVert \vec{n} \rVert^2 + B = 0 \\ t = - \frac{\vec{n} \bullet P + B}{\lVert \vec{n} \rVert^2} \end{gathered}$

Now to find the distance we only have to calculate the length of the vector $P - P'$.

$\begin{gathered} \lVert P - P' \rVert = \left\lVert P - P + \frac{\vec{n} \bullet P + B}{\lVert \vec{n} \rVert^2} \vec{n} \right\rVert = \left\lVert \frac{\vec{n} \bullet P + B}{\lVert \vec{n} \rVert^2} \vec{n} \right\rVert = \\ = \frac{\lvert \vec{n} \bullet P + B \rvert}{\lVert \vec{n} \rVert^2} \lVert \vec{n} \rVert = \frac{\lvert \vec{n} \bullet P + B \rvert}{\lVert \vec{n} \rVert} = \\ = \frac{\lvert A_1 P_1 + A_2 P_2 + \cdots + A_n P_n + B \rvert}{\sqrt{A_1^2 + A_2^2 + \cdots + A_n^2}} \end{gathered}$

Which ends this proof. $\blacksquare$

Implementation Note

Implementing a function dist2D(Point P, Line L) in a program which needs to do such a calculation is now only matter of simple arithmetic. In some cases (for example when you only need to compare 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.