Sub topic 13
Distance between parallel lines
This chapter finishes the sequence on parallel and perpendicular lines. It uses the point-to-line distance formula from the previous chapter. It assumes you can rewrite a line equation in standard form Ax + By + C = 0, apply the point-to-line distance formula, and know that parallel lines share a slope. You don't need new machinery, only one observation that turns a two-line problem into a one-point problem.
What "distance between lines" means
Two lines that cross have distance 0, because they share a point. Two distinct parallel lines never meet, so the question has a real answer. The distance between them is the length of the shortest segment joining a point on one line to a point on the other.
That shortest segment is always perpendicular to both lines. Any slanted segment between the lines is the hypotenuse of a right triangle whose leg is the perpendicular segment, so it must be longer.
Because the lines are parallel, the perpendicular segment has the same length wherever you draw it. You can therefore choose any convenient point on one line and measure from it.
Deriving the formula
Take two parallel lines in standard form with identical coefficients A and B:
- Line 1:
Ax + By + C1 = 0 - Line 2:
Ax + By + C2 = 0
Pick any point (x0, y0) on line 1. It satisfies the equation of line 1, so:
A·x0 + B·y0 = -C1
The distance from (x0, y0) to line 2 is
|A·x0 + B·y0 + C2| / √(A² + B²)
Substitute A·x0 + B·y0 = -C1 into the numerator:
|-C1 + C2| / √(A² + B²) = |C1 - C2| / √(A² + B²)
The point (x0, y0) dropped out. That is the algebraic reason the distance doesn't depend on which point you pick.
Distance between Ax + By + C1 = 0 and Ax + By + C2 = 0:
d = |C1 − C2| / √(A² + B²)
A worked example in standard form
Find the distance between 3x + 4y − 10 = 0 and 3x + 4y + 5 = 0.
- The coefficients
A = 3andB = 4already match. C1 = −10andC2 = 5, so|C1 − C2| = 15.√(3² + 4²) = 5.d = 15 / 5 = 3.
You can check this by hand with a point. On line 1, (0, 2.5) works, since 4(2.5) − 10 = 0. Its distance to line 2 is |0 + 10 + 5| / 5 = 3. Both methods agree.
Starting from slope-intercept form
Suppose the lines are y = mx + b1 and y = mx + b2. Rewrite each as mx − y + b = 0. Then A = m, B = −1, C1 = b1, C2 = b2, and the formula becomes:
d = |b1 − b2| / √(1 + m²)
Read this as a geometric statement. |b1 − b2| is the vertical gap between the lines, measured along the y-axis. A tilted line makes the vertical gap longer than the perpendicular gap, and the factor √(1 + m²) corrects for that tilt. For m = 0 (horizontal lines) the factor is 1, and the vertical gap is the distance.
Example: y = 2x + 1 and y = 2x − 5. The vertical gap is |1 − (−5)| = 6, and √(1 + 4) = √5. So d = 6/√5 ≈ 2.683.
Cross-check with the perpendicular-segment method
You can also build the shortest segment explicitly, using the earlier chapters.
- Pick the point
(0, 1)ony = 2x + 1. - The perpendicular slope is
−1/2, so the perpendicular through that point isy = −x/2 + 1. - Intersect with
y = 2x − 5:2x − 5 = −x/2 + 1, so2.5x = 6andx = 2.4. Theny = −0.2. - The distance from
(0, 1)to(2.4, −0.2)is√(2.4² + 1.2²) = √7.2 ≈ 2.683.
The result matches 6/√5, because √7.2 = 6/√5. This method takes more steps, so use it as a check or when you need the foot of the perpendicular itself.
Vertical and horizontal lines
The formula covers the special cases from earlier in the tutorial without any exceptions.
| Lines | Standard form values | Distance | Column | Column |
|---|---|---|---|---|
| `y = c` and `y = d` (horizontal) | `A = 0`, `B = 1` | `\ | c − d\ | ` |
| `x = a` and `x = b` (vertical) | `A = 1`, `B = 0` | `\ | a − b\ | ` |
For vertical lines, x − a = 0 gives C = −a, so d = |−a − (−b)| / √1 = |a − b|. Here the slope form y = mx + b fails because a vertical line has no slope. The standard-form formula still works.
Computing it in code
The function below accepts each line as a coefficient triple (A, B, C). It does not require the two triples to be scaled alike. It checks that the lines are parallel, picks a point on the first line, and measures from that point to the second line. This avoids the scaling mistake described in the next section.
import math
def distance_between_parallel(line1, line2):
# each line is (A, B, C) meaning Ax + By + C = 0
A1, B1, C1 = line1
A2, B2, C2 = line2
if (A1 == 0 and B1 == 0) or (A2 == 0 and B2 == 0):
raise ValueError("A and B cannot both be zero")
if A1 * B2 - A2 * B1 != 0:
raise ValueError("lines are not parallel")
# a point on line 1
if B1 != 0:
x0, y0 = 0, -C1 / B1
else:
x0, y0 = -C1 / A1, 0
# distance from that point to line 2
return abs(A2 * x0 + B2 * y0 + C2) / math.hypot(A2, B2)
print(distance_between_parallel((3, 4, -10), (3, 4, 5))) # 3.0
print(distance_between_parallel((3, 4, -10), (6, 8, 10))) # 3.0 (same second line, doubled)
print(round(distance_between_parallel((2, -1, 1), (2, -1, -5)), 4)) # 2.6833
print(distance_between_parallel((1, 0, -2), (1, 0, -7))) # 5.0 (x = 2 and x = 7)The test A1*B2 − A2*B1 == 0 is the standard-form version of "equal slopes". It also handles vertical lines, where B = 0 and slopes are undefined. With floating-point coefficients, compare with a tolerance such as math.isclose, or use Fraction for exact arithmetic.
Common errors
Other mistakes to watch for:
- Using the wrong form. An equation like
2x + 3y = 6must be moved to2x + 3y − 6 = 0before readingC. HereC = −6, not6. - Dropping the absolute value. A distance is never negative. The sign of
C1 − C2only tells you which line is on which side. - Using the vertical gap as the distance. In slope-intercept form,
|b1 − b2|is only the distance when the lines are horizontal. Divide by√(1 + m²)otherwise. - Assuming the lines are parallel. If
A1·B2 ≠ A2·B1, the lines cross and the formula doesn't apply. Check the slopes first. - Coincident lines. If after scaling
C1 = C2, the two equations describe the same line, and the formula returns 0. That is correct, but it means the lines were not distinct.
Where this shows up
A distance between parallel lines is a width. It gives the width of a strip bounded by two parallel lines, the gap between two rails or walls in a coordinate model, or the margin in a classifier. In a support vector machine, the margin between two parallel boundary lines is computed with this exact formula. Setting the distance equal to a target value and solving for C2 also lets you construct a parallel line a given distance away, as the third exercise shows.
Practice
Exercise
Find the distance between y = −3x + 2 and y = −3x − 8.
Show answer
Rewrite as 3x + y − 2 = 0 and 3x + y + 8 = 0. Then A = 3, B = 1, |C1 − C2| = |−2 − 8| = 10, and √(9 + 1) = √10. So d = 10/√10 = √10 ≈ 3.162. The slope-form shortcut gives the same result: |2 − (−8)| / √(1 + 9) = 10/√10.
Exercise
Find the distance between 2x + 3y = 6 and 4x + 6y = −8.
Show answer
Move everything to one side: 2x + 3y − 6 = 0 and 4x + 6y + 8 = 0. Divide the second by 2 to match coefficients: 2x + 3y + 4 = 0. Then |C1 − C2| = |−6 − 4| = 10 and √(4 + 9) = √13. So d = 10/√13 ≈ 2.774.
Exercise
Write the equations of all lines parallel to 3x + 4y = 0 that are exactly 2 units away from it.
Show answer
Parallel lines have the form 3x + 4y + C = 0. The original line has C1 = 0, so the distance is |0 − C| / √(9 + 16) = |C| / 5. Setting |C| / 5 = 2 gives C = ±10. The two lines are 3x + 4y + 10 = 0 and 3x + 4y − 10 = 0, one on each side of the original.
Exercise
What is the distance between the vertical lines x = −4 and x = 9? Explain why the formula still works even though vertical lines have no slope.
Show answer
The distance is |−4 − 9| = 13. Writing the lines as x + 4 = 0 and x − 9 = 0 gives A = 1, B = 0, C1 = 4, C2 = −9, so d = |4 − (−9)| / √1 = 13. The standard form Ax + By + C = 0 never divides by B, so a vertical line causes no problem.