Suppose there are two parallel lines: $w_1x_1+w_2x_2=c_1$ (Line 1) and $w_1x_1+w_2x_2=c_2$ (Line 2). What is the distance between them (the shortest distance between any two points)?

I know the answer is $d=\frac{|c_1-c_2|}{||w||}$ where $||w||=\sqrt{w_1^2+w_2^2}$.

The method I was going to calculate is as follows:

1) find any point on Line 1 $(x_1^0,x_2^0)$ such that $w_1x_1^0+w_2x_2^0=c_1$

2) calculate the perpendicular line (Line 3) to Line 1 and passing through $(x_1^0,x_2^0)$

3) find the point $(x_1^1,x_2^1)$ where Line 3 intersects Line 2

4) calculate the distance between $(x_1^0,x_2^0)$ and $(x_1^1,x_2^1)$

However I couldn't figure out the algebra of this method. Can someone show me the steps of the above calculation? Or is there any simpler way to calculate this? Thanks.

  • $\begingroup$ I would say sipler way to calculate distance between two parallel lines you wrote in the introduction of the query. $\endgroup$
    – georg
    Aug 11, 2019 at 19:12
  • 1
    $\begingroup$ Line 3 given by $w_2x_1-w_1x_2=0$ is through the origin and perpendicular to Line 1 and Line 2; can you find the distance from the origin to the intersection of Line 1 (and Line 2) with Line 3 ? $\endgroup$ Aug 11, 2019 at 19:15
  • 1
    $\begingroup$ Which step are you having trouble with? Perpendicular is of form $w_2x_1-w_1x_2=c$,.where $c=w_2x_1^0-w_1x_2^0$. $\endgroup$ Aug 11, 2019 at 19:16
  • $\begingroup$ Since you're doing linear algebra, the simplest method is to realize that $w_1x_1+w_2x_2$ is simply the inner product (aka dot product) of $(w_1,w_2)$ with $(x_1,x_2)$ and that the vector $(w_1,w_2)$ is orthogonal to both lines. Also, the inner product of $(w_1,w_2)$ with any point's position vector gives you the (scaled) distance from the line $w_1x_2+w_2x_2=0,$ scaled by $\|w\|.$ $\endgroup$
    – David K
    Aug 11, 2019 at 19:59
  • $\begingroup$ Does this answer your question? Proving formula for distance between 2 parallel lines $\endgroup$ Oct 27, 2021 at 12:41

4 Answers 4


Another possible way: Let $(a_1, a_2)$ be a point on $w_1x_1+w_2x_2-c_1=0$. Then we want to minimalize $$f(x)=(x_1-a_1)^2+(x_2-a_2)^2$$ With the constraint $$g(x)=w_1x_1+w_2x_2-c_2=0$$ Using Lagrange multipliers: $$\nabla f + 2\lambda\nabla g = 0$$ The derivatives are $$\nabla f=[2(x_1-a_1), 2(x_2-a_2)]$$ $$\nabla g = [w_1, w_2]$$ Which means that $$2(x_1-a_1)+2\lambda w_1=0$$ $$2(x_2-a_2)+2\lambda w_2=0$$ So the system of equations we need to solve is $$x_1=-\lambda w_1+a_1$$ $$x_2=-\lambda w_2+a_2$$ $$w_1x_1+w_2x_2=c_2$$ Which has the following solution: $$x_1=\frac{-a_2w_1w_2+a_1w_2^2+c_2w_1}{w_1^2+w_2^2}$$ $$x_2=\frac{-a_1w_1w_2+a_2w_1^2+c_2w_2}{w_1^2+w_2^2}$$ $$\lambda=\frac{a_1w_1+a_2w_2-c_2}{w_1^2+w_2^2}$$ So the minimal distance squared is \begin{align} d^2 &=\left(\frac{-a_2w_1w_2+a_1w_2^2+c_2w_1}{w_1^2+w_2^2}-a_1\right)^2+\left(\frac{-a_1w_1w_2+a_2w_1^2+c_2w_2}{w_1^2+w_2^2}-a_2\right)^2\\ &=\left(\frac{-a_2w_1w_2+a_1w_2^2+c_2w_1}{w_1^2+w_2^2}-\frac{a_1w_1^2+a_1w_2^2}{w_1^2+w_2^2}\right)^2+\left(\frac{-a_1w_1w_2+a_2w_1^2+c_2w_2}{w_1^2+w_2^2}-\frac{a_2w_1^2+a_2w_2^2}{w_1^2+w_2^2}\right)^2\\ &=\left(\frac{-a_2w_1w_2-a_1w_1^2+c_2w_1}{w_1^2+w_2^2}\right)^2+\left(\frac{-a_1w_1w_2-a_2w_2^2+c_2w_2}{w_1^2+w_2^2}\right)^2\\ &=\left(\frac{-c_1w_1+c_2w_1}{w_1^2+w_2^2}\right)^2+\left(\frac{-c_1w_2+c_2w_2^2}{w_1^2+w_2}\right)^2\\ &=\frac{(-c_1w_1+c_2w_1)^2+(-c_1w_2+c_2w_2)^2}{(w_1^2+w_2^2)^2}\\ &= \frac{(c_2-c_1)^2}{w_1^2+w_2^2} \end{align} As we expected.

  • $\begingroup$ Thanks for this excellent answer. I changed the selected answer to this one because I was looking for a solution that started closer from first principals. $\endgroup$ Aug 15, 2019 at 14:31
  • $\begingroup$ @Catiger3331 thank you :) To be honest, this answer is a bit far away from linear algebra and geometry, and more calculation-heavy; But I like this method because of my geometry skills are not so great :) $\endgroup$
    – Botond
    Aug 15, 2019 at 14:52
  • $\begingroup$ I like this and this is similar to en.wikipedia.org/wiki/Distance_between_two_straight_lines $\endgroup$
    – David Wu
    Oct 30, 2019 at 20:17

There is an easier way to find the distance using dot product.

Pick the point $A=(x_1,y_1)$ on the fist line and $B=(x_2,y_2)$ on the second line.

The distance between the lines is the length of the projection of $AB$ on the normal vector to the parallel lines

$$d=\frac {|AB.N|}{||N||}=\frac {|c_2-c_1|}{\sqrt {w_1^2+w_2^2}}$$

You may fill in the details of simplifying the dot product and the norm in the above fraction.


It is easier to figure out the distance from the right triangle formed by one of the lines, the vertical axis and the distance-line itself, as shown in the graph.

The intersections of the vertical axis with the two lines are $-c_1/w_2$ and $-c_2/w_2$, respectively. And the tangent of the two line is $\tan\theta=-w_1/w_2$. According to the right triangle in the graph. the distance $d$ is simply,


where $\cos\theta=1/\sqrt{1+\tan^2\theta}$ is used.

enter image description here


The distance between two parallel lines $a x+b y+c_{1}=0$ and $$ a x+b y+c_{2}=0 \text { is }=\frac{\left|c_{1}-c_{2}\right|}{\sqrt{a^{2}+b^{2}}} $$ (Note: The coefficients of $x \& y$ in both equations should be same)


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .