title: Distance from a point to a line
text: # Distance from a point to a line
  
  In Euclidean geometry, the distance from a point to a line is the shortest distance from a given point to any point on an infinite straight line. It is the perpendicular distance of the point to the line, the length of the line segment which joins the point to nearest point on the line. The algebraic expression for calculating it can be derived and expressed in several ways.
  
  Knowing the distance from a point to a line can be useful in various situations—for example, finding the shortest distance to reach a road, quantifying the scatter on a graph, etc. In Deming regression, a type of linear curve fitting, if the dependent and independent variables have equal variance this results in orthogonal regression in which the degree of imperfection of the fit is measured for each data point as the perpendicular distance of the point from the regression line.
  
  ## Line defined by an equation
  
  In the case of a line in the plane given by the equation ax + by + c = 0, where a, b and c are real constants with a and b not both zero, the distance from the line to a point $(x_0,$ $y_0)$ $is^{:p.14}$
  
      $\operatorname {distance} (ax+by+c=0,(x_{0},y_{0}))={\frac {|ax_{0}+by_{0}+c|}{\sqrt {a^{2}+b^{2}}}}.$
  
  The point on this line which is closest to $(x_0,$ $y_0)$ has coordinates:
  
      $x={\frac {b(bx_{0}-ay_{0})-ac}{a^{2}+b^{2}}}{\text{ and }}y={\frac {a(-bx_{0}+ay_{0})-bc}{a^{2}+b^{2}}}.$
  
  Horizontal and vertical lines
  
  In the general equation of a line, ax + by + c = 0, a and b cannot both be zero unless c is also zero, in which case the equation does not define a line. If a = 0 and b ≠ 0, the line is horizontal and has equation y = −c/b. The distance from $(x_0,$ $y_0)$ to this line is measured along a vertical line segment of length $|y_0$ − (−c/b)| = $|by_0$ + c|/|b| in accordance with the formula. Similarly, for vertical lines (b = 0) the distance between the same point and the line is $|ax_0$ + c|/|a|, as measured along a horizontal line segment.
  
  ## Line defined by two points
  
  If the line passes through two points $P_1$ = $(x_1,$ $y_1)$ and $P_2$ = $(x_2,$ $y_2)$ then the distance of $(x_0,$ $y_0)$ from the line is:
  
      $\operatorname {distance} (P_{1},P_{2},(x_{0},y_{0}))={\frac {|(x_{2}-x_{1})(y_{1}-y_{0})-(x_{1}-x_{0})(y_{2}-y_{1})|}{\sqrt {(x_{2}-x_{1})^{2}+(y_{2}-y_{1})^{2}}}}.$
  
  The denominator of this expression is the distance between $P_1$ and $P_2.$ The numerator is twice the area of the triangle with its vertices at the three points, $(x_0,$ $y_0),$ $P_1$ and $P_2.$ See: Area of a triangle § Using coordinates. The expression is equivalent to h = 2A/b, which can be obtained by rearranging the standard formula for the area of a triangle: A = 1/2 bh, where b is the length of a side, and h is the perpendicular height from the opposite vertex.
  
  ## Line defined by point and angle
  
  If the line passes through the point P = $(P_x,$ $P_y)$ with angle θ, then the distance of some point $(x_0,$ $y_0)$ to the line is
  
      $\operatorname {distance} (P,\theta ,(x_{0},y_{0}))=|\cos(\theta )(P_{y}-y_{0})-\sin(\theta )(P_{x}-x_{0})|$
  
  ## Proofs
  
  ### An algebraic proof
  
  This proof is valid only if the line is neither vertical nor horizontal, that is, we assume that neither a nor b in the equation of the line is zero.
  
  The line with equation ax + by + c = 0 has slope −a/b, so any line perpendicular to it will have slope b/a (the negative reciprocal). Let (m, n) be the point of intersection of the line ax + by + c = 0 and the line perpendicular to it which passes through the point $(x_0,$ $y_0).$ The line through these two points is perpendicular to the original line, so
  
      ${\frac {y_{0}-n}{x_{0}-m}}={\frac {b}{a}}.$
  
  Thus, $a(y_{0}-n)-b(x_{0}-m)=0,$ and by squaring this equation we obtain:
  
      $a^{2}(y_{0}-n)^{2}+b^{2}(x_{0}-m)^{2}=2ab(y_{0}-n)(x_{0}-m).$
  
  Now consider,
  
      ${\begin{aligned}(a(x_{0}-m)+b(y_{0}-n))^{2}&=a^{2}(x_{0}-m)^{2}+2ab(y_{0}-n)(x_{0}-m)+b^{2}(y_{0}-n)^{2}\\&=\left(a^{2}+b^{2}\right)\left((x_{0}-m)^{2}+(y_{0}-n)^{2}\right)\end{aligned}}$
  
  using the above squared equation. But we also have,
  
      $(a(x_{0}-m)+b(y_{0}-n))^{2}=(ax_{0}+by_{0}-am-bn)^{2}=(ax_{0}+by_{0}+c)^{2}$
  
  since (m, n) is on ax + by + c = 0. Thus,
  
      $\left(a^{2}+b^{2}\right)\left((x_{0}-m)^{2}+(y_{0}-n)^{2}\right)=(ax_{0}+by_{0}+c)^{2}$
  
  and we obtain the length of the line segment determined by these two points,
  
      $d={\sqrt {(x_{0}-m)^{2}+(y_{0}-n)^{2}}}={\frac {|ax_{0}+by_{0}+c|}{\sqrt {a^{2}+b^{2}}}}.$
  
  ### A geometric proof
  
  This proof is valid only if the line is not horizontal or vertical.
  
  Drop a perpendicular from the point P with coordinates $(x_0,$ $y_0)$ to the line with equation Ax + By + C = 0. Label the foot of the perpendicular R. Draw the vertical line through P and label its intersection with the given line S. At any point T on the line, draw a right triangle TVU whose sides are horizontal and vertical line segments with hypotenuse TU on the given line and horizontal side of length |B| (see diagram). The vertical side of ∆TVU will have length |A| since the line has slope -A/B.
  
  ∆PRS and ∆TVU are similar triangles, since they are both right triangles and ∠PSR ≅ ∠TUV since they are corresponding angles of a transversal to the parallel lines PS and UV (both are vertical lines). Corresponding sides of these triangles are in the same ratio, so:
  
      ${\frac {|{\overline {PR}}|}{|{\overline {PS}}|}}={\frac {|{\overline {TV}}|}{|{\overline {TU}}|}}.$
  
  If point S has coordinates $(x_0,m)$ then |PS| = $|y_0$ - m| and the distance from P to the line is:
  
      $|{\overline {PR}}|={\frac {|y_{0}-m||B|}{\sqrt {A^{2}+B^{2}}}}.$
  
  Since S is on the line, we can find the value of m,
  
      $m={\frac {-Ax_{0}-C}{B}},$
  
  and finally obtain:
  
      $|{\overline {PR}}|={\frac {|Ax_{0}+By_{0}+C|}{\sqrt {A^{2}+B^{2}}}}.$
  
  A variation of this proof is to place V at P and compute the area of the triangle ∆UVT two ways to obtain that $D|{\overline {TU}}|=|{\overline {VU}}||{\overline {VT}}|$ where D is the altitude of ∆UVT drawn to the hypotenuse of ∆UVT from P. The distance formula can then used to express $|{\overline {TU}}|$, $|{\overline {VU}}|$, and $|{\overline {VT}}|$in terms of the coordinates of P and the coefficients of the equation of the line to get the indicated formula.
  
  ### A vector projection proof
  
  Let P be the point with coordinates $(x_0,$ $y_0)$ and let the given line have equation ax + by + c = 0. Also, let Q = $(x_1,$ $y_1)$ be any point on this line and n the vector (a, b) starting at point Q. The vector n is perpendicular to the line, and the distance d from point P to the line is equal to the length of the orthogonal projection of ${\overrightarrow {QP}}$ on n. The length of this projection is given by:
  
      $d={\frac {|{\overrightarrow {QP}}\cdot \mathbf {n} |}{\|\mathbf {n} \|}}.$
  
  Now,
  
      ${\overrightarrow {QP}}=(x_{0}-x_{1},y_{0}-y_{1}),$ so ${\overrightarrow {QP}}\cdot \mathbf {n} =a(x_{0}-x_{1})+b(y_{0}-y_{1})$ and $\|\mathbf {n} \|={\sqrt {a^{2}+b^{2}}},$
  
  thus
  
      $d={\frac {|a(x_{0}-x_{1})+b(y_{0}-y_{1})|}{\sqrt {a^{2}+b^{2}}}}.$
  
  Since Q is a point on the line, $c=-ax_{1}-by_{1}$, and so,
  
      $d={\frac {|ax_{0}+by_{0}+c|}{\sqrt {a^{2}+b^{2}}}}.$
  
  Although the distance is given as a modulus, the sign can be useful to determine which side of the line the point is on, in a sense determined by the direction of normal vector (a,b)
  
  ## Another formula
  
  It is possible to produce another expression to find the shortest distance of a point to a line. This derivation also requires that the line be not vertical or horizontal.
  
  The point P is given with coordinates ($x_{0},y_{0}$). The equation of a line is given by $y=mx+k$. The equation of the normal of that line which passes through the point P is given $y={\frac {x_{0}-x}{m}}+y_{0}$.
  
  The point at which these two lines intersect is the closest point on the original line to the point P. Hence:
  
      $mx+k={\frac {x_{0}-x}{m}}+y_{0}.$
  
  We can solve this equation for x,
  
      $x={\frac {x_{0}+my_{0}-mk}{m^{2}+1}}.$
  
  The y coordinate of the point of intersection can be found by substituting this value of x into the equation of the original line,
  
      $y=m{\frac {(x_{0}+my_{0}-mk)}{m^{2}+1}}+k.$
  
  Using the equation for finding the distance between 2 points, $d={\sqrt {(X_{2}-X_{1})^{2}+(Y_{2}-Y_{1})^{2}}}$, we can deduce that the formula to find the shortest distance between a line and a point is the following:
  
      $d={\sqrt {\left({{\frac {x_{0}+my_{0}-mk}{m^{2}+1}}-x_{0}}\right)^{2}+\left({m{\frac {x_{0}+my_{0}-mk}{m^{2}+1}}+k-y_{0}}\right)^{2}}}={\frac {|k+mx_{0}-y_{0}|}{\sqrt {1+m^{2}}}}.$
  
  Recalling that m = -a/b and k = - c/b for the line with equation ax + by + c = 0, a little algebraic simplification reduces this to the standard expression.
  
  ## Vector formulation
  
  The equation of a line can be given in vector form:
  
      $\mathbf {x} =\mathbf {a} +t\mathbf {n}$
  
  Here a is a point on the line, and n is a unit vector in the direction of the line. Then as scalar t varies, x gives the locus of the line.
  
  The distance of an arbitrary point p to this line is given by
  
      $\operatorname {distance} (\mathbf {x} =\mathbf {a} +t\mathbf {n} ,\mathbf {p} )=\|(\mathbf {p} -\mathbf {a} )-((\mathbf {p} -\mathbf {a} )\cdot \mathbf {n} )\mathbf {n} \|.$
  
  This formula can be derived as follows: $\mathbf {p} -\mathbf {a}$ is a vector from a to the point p. Then $(\mathbf {p} -\mathbf {a} )\cdot \mathbf {n}$ is the projected length onto the line and so
  
      $\mathbf {a} +((\mathbf {p} -\mathbf {a} )\cdot \mathbf {n} )\mathbf {n}$
  
  is a vector that is the projection of $\mathbf {p} -\mathbf {a}$ onto the line and represents the point on the line closest to $\mathbf {p}$. Thus
  
      $(\mathbf {p} -\mathbf {a} )-((\mathbf {p} -\mathbf {a} )\cdot \mathbf {n} )\mathbf {n}$
  
  is the component of $\mathbf {p} -\mathbf {a}$ perpendicular to the line. The distance from the point to the line is then just the norm of that vector. This more general formula is not restricted to two dimensions.
  
  ## Another vector formulation
  
  If the vector space is orthonormal and if the line goes through point a and has a direction vector n, the distance between point p and the line is
  
      $\operatorname {distance} (\mathbf {x} =\mathbf {a} +t\mathbf {n} ,\mathbf {p} )={\frac {\left\|(\mathbf {p} -\mathbf {a} )\times \mathbf {n} \right\|}{\|\mathbf {n} \|}}.$
  
  Note that cross products only exist in dimensions 3 and 7.
  
  