Distance From a Point to a Straight Line

On this page we'll derive an engaging formula for the distance from a point to a straight line. If a line L is given by its general equation


Ax + By + C = 0

and a point P = (u, v) is given in the plane, then the distance dist(P, L) from the point to the line is determined by


dist(P, L) = |Au + Bv + C| / ||n||,

where n = (A, B) is the vector of the coefficients, normal to the line. ||n|| is the norm of the vector. If the equation is normalized, i.e., if A2 + B2 = 1, (2) is simplified:


dist(P, L) = |Au + Bv + C|.

To see why this is so, observe that (1) tells us that a straight line is a level curve f(x, y) = 0 of the linear function


f(x, y) = Ax + By + C.

(For any function f of any number of arguments, a level surface is the locus of points where f takes on the same value f = c, say. For functions of two arguments, the locii are naturally called level curves.)

For the function (3), the level curves

Ax + By + C = c

are all straight lines parallel to the one given by (1) because all of them are perpendicular to the same normal vector n = (A, B).

(In the applet below, a straight line is defined by two points, each of which can be dragged independently causing a rotation around the other point. The line can also be dragged parallel to itself at any other point. There is an additional point with the distance to the line indicated in blue. The point can also be dragged. Note: the applet gives you a precision of 0 to 6 digits. The equalities therefore are not quite exact.)

If you are reading this, your browser is not set to run Java applets. Try IE11 or Safari and declare the site https://www.cut-the-knot.org as trusted in the Java setup.

distance to line

What if applet does not run?

The distance from a point to a line (or to any set, for that matter) is defined as the minimum distance between the given point and the points on the line:

dist(P, L) = min(dist(P, r)),

over all r = (x, y) belong to line L. The minimum always exists for a straight line, but may not exist for other sets. For a straight line, the minimum is achieved at the foot of the perpendicular from the given point to the line. It is noteworthy that all points on one of the level lines have the same distance to any other level line, because the lines are parallel and the distances are measured via the perpendiculars. This is how the normal to the line comes into the play.

The function does not change its value along the level lines. All the change comes along the direction of the normal n = (A, B)! Assume m = n/||n|| is the unit vector normal to the level lines of the given function:

m = (A/||n||, B/||n||).

Pick a point (x0, y0) and define (x1, y1) = (x0, y0) + m. What is the difference between f(x1, y1) and f(x0, y0)? Since the function f is linear we have

f(x1, y1) - f(x0, y0)= (Ax1 + By1 + C) - (Ax0 + By0 + C)
 = A·A/||n|| + B·B/||n||
 = A2/||n|| + B2/||n||
 = (A2 + B2)/||n||
 = ||n||2/ ||n||
 = ||n||.

That is, every step by one unit in the direction normal to the line change the value of the function by the norm of its normal vector? This is true for any linear function and for any form of its equation. In particular, if the equation is normalized to start with, a unit step in the direction normal to the line changes its value by exactly 1!. With every unit step, the values on the level lines grow by 1 in the direction of the normal, but they decrease by 1 in the opposite direction. The line (1) divides the plane into two halves. In one, which is pointed to by the normal n = (A, B), f takes on positive values; in the other f is negative throughout.

Once this simple behavior has been observed, let d = dist(P, L).

Define now


(x, y) = (u, v) - d(±m).

where we choose the sign "+" if P lies in the "positive half-plane". We choose "-" otherwise. Since d is the distance from P = (u, v) to the line, and the distance from a point to the line is measured along the normal, i.e. m, the point (x, y) in (4) lies on the line: f(x, y) = 0. This entails a sequence of steps:

0= f(x, y)
 = A(u - d·(±A/||n||)) + B(v - d·(±B/||n||)) + C
 = (Au + Bv + C) - d·[±(A2/||n|| + B2/||n||)]
 = f(u, v) - [±d||n||].

In other words,


d = ± f(u, v) / ||n||,

where the sign is chosen so as to make ± f(u, v) positive. If d is considered the signed distance, i.e., if d comes with a sign minus if f(u, v) is negative, the formula is simplified to


d = f(u, v) / ||n||.

In any event, we can conclude that (2) is indeed true.

If a straight line is given by an implicit equation n·(r - r0) = 0, we can similarly set

f(r) = n·(r - r0)

for r not necessarily on the line. Then the distance d from a point P to the line can be found from

d = | n·(P - r0)| / ||n|| = | f(P)/||n|| |.

Related material

  • Cavalieri's Principle
  • Derivative of Sine and Cosine
  • Estimating Circumference of a Circle
  • Maximum Volume of a Cut Off Box
  • Mistrust Intuition of the Infinite
  • Naturally Discontinuous Functions
  • Rolle's and The Mean Value Theorems
  • Function, Derivative and Integral
  • Area of a Circle by Rabbi Abraham bar Hiyya Hanasi
  • Schwarz Lantern
  • Two Circles and a Limit
  • Deceptive Appearances
  • Problem 4010 from Crux Mathematicorum

  • |Contact| |Front page| |Contents| |Algebra|

    Copyright © 1996-2018 Alexander Bogomolny