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(x1,y1)P(x_1, y_1) on a 2D plane in cartesian coordinates from a given line L={(x,y):Ax+By+C=0}L = \{(x,y): Ax+By+C=0\}, where AA and BB are not both zero.

Solution

Distance is given by the following formula.

Ax1+By1+CA2+B2\frac{|Ax_1+By_1+C|}{\sqrt{A^2+B^2}}

Proof by Using Simple Algebra

We need to consider three cases: when line LL 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. LL' denotes a line perpendicular to LL in the rest of the proof.

Steps

For each case we need to find orthographic projection of point PP to the line LL, let’s call it point SS, and then calculate the distance between PP and SS by using the Euclidean metric dist(P1,P2)=(x1x2)2+(y1y2)2\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 LL' perpendicular to LL which goes through point PP.
  2. Find intersection SS between LL and LL'.
  3. Calculate distance between SS and PP.

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, A0B=0A \neq 0 \land B = 0 which means the equation for LL can be written more simply in the form x=CAx = -\frac{C}{A}. Notice that LL has undefined slope.

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

The two lines LL and LL' meet only at one point S(CA,y1)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 SS and PP.

d=(x1(CA))2+(y1y1)2=(Ax1+C)2A2=Ax1+CA2d = \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=0B0A = 0 \land B \neq 0. The equation for LL becomes y=CBy=-\frac{C}{B}. Although LL now has defined slope (equal to 00), LL' has not, which is why this case needs to be done seperately.

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

d=(x1x1)2+(y1(CB))2=(By1+C)2B2=By1+CB2.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 A0B0A \neq 0 \land B \neq 0 which is the most general case. Now both LL and LL' have defined slopes.

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

Recall that two lines with defined slopes are perpendicular if and only if mLmL=1m_L m_{L'} = -1. Therefore the slope of LL' is mL=BAm_{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 mL=ham_L = \frac{-h}{a} from the definition of slope as change in ychange in x\frac{\text{change in y}}{\text{change in x}} and mL=ahm_{L'} = \frac{a}{h} from definition of the slope as tanθ\tan \theta.

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

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

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

ABx0CB=BAx0+y1BAx1-\frac{A}{B}x_0-\frac{C}{B} = \frac{B}{A}x_0+y_1-\frac{B}{A}x_1

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

x0(BA+AB)=CBy1+BAx1x_0 (\frac{B}{A}+\frac{A}{B}) = -\frac{C}{B}-y_1+\frac{B}{A}x_1

Multiply both sides by ABAB.

x0(A2+B2)=ACABy1+B2x1x_0(A^2+B^2)=-AC-AB y_1+B^2 x_1

Finally, divide both sides by A2+B2A^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 A0A \neq 0 and B0B \neq 0, so A2+B20A^2 + B^2 \neq 0.

x0=B2x1ABy1ACA2+B2x_0 = \frac{B^2 x_1-AB y_1-AC}{A^2+B^2}

In order to find y0y_0 we just plug in x0x_0 in either yLy_L or yLy_{L'}. yLy_L looks a little bit more friendly so I’ll use this one.

y0=AB(B2x1ABy1AC)1A2+B2CBy_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 CB\frac{C}{B} to have the denominator A2+B2A^2+B^2.

y0=ABx1+A2y1+A2CBCB(A2+B2)A2+B2y_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

y0=ABx1+A2y1+A2CCA2CB2BA2+B2y_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

y0=ABx1+A2y1CBA2+B2y_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=(x0x1)2+(y0y1)2d=\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 x0x1x_0-x_1

x0x1=B2x1ABy1ACA2+B2x1x_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 x1x_1 to the numerator. Fortunately, something cancels out. Notice how all our expressions look awfully similar to the final formula.

x0x1=A2x1ABy1ACA2+B2x_0-x_1=\frac{-A^2 x_1-AB y_1-AC}{A^2+B^2}

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

x0x1=AAx1+By1+CA2+B2x_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.

(x0x1)2=A2(Ax1+By1+CA2+B2)2(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 (y0y1)2(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.

(y0y1)2=B2(Ax1+By1+CA2+B2)2(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 B2B^2 in front of it instead of A2A^2. Now the final steps should be quite easy, we can factor out the (Ax1+By1+CA2+B2)2\left( \frac{A x_1+B y_1+C}{A^2+B^2} \right)^2 from their sum

(x0x1)2+(y0y1)2=(Ax1+By1+CA2+B2)2(A2+B2)(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)

A2+B2A^2+B^2 cancels out with the square in denominator so we get

(x0x1)2+(y0y1)2=(Ax1+By1+C)2A2+B2(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 Ax1+By1+CA x_1+B y_1+C is positive so we have to take absolute value.

(x0x1)2+(y0y1)2=Ax1+By1+CA2+B2\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 PRnP \in \mathbb{R}^n from a given hyperplane L={(x1,x2,,xn):A1x1+A2x2++Anxn+B=0}L = \{(x_1,x_2,\ldots, x_n): A_1 x_1 + A_2 x_2+ \cdots + A_n x_n + B=0\}, where A12+A22++An2>0A_1^2 + A_2^2 + \cdots + A_n^2 > 0.

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

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

Projection of PP onto hyperplane LL.

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

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

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

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

A1(P1+tA1)+A2(P2+tA2)++An(Pn+tAn)+B=0A1P1+A2P2+AnPn+t(A12+A22++An2)+B=0nP+tn2+B=0t=nP+Bn2\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 PPP - P'.

PP=PP+nP+Bn2n=nP+Bn2n==nP+Bn2n=nP+Bn==A1P1+A2P2++AnPn+BA12+A22++An2\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.