Sub topic 12
Distance from a Point to a Line
Prerequisites and goal
This chapter assumes you can write a line in standard form (Ax + By + C = 0), compute slopes, find a perpendicular line through a point, and solve a pair of linear equations. Fraction arithmetic and square roots are used throughout.
By the end you will be able to compute the shortest distance from a point to a line in two ways. The first way is the step-by-step construction. The second is the closed-form formula. You will also know the edge cases where each method is easiest to get wrong.
What "distance to a line" means
A line has infinitely many points, so "the distance from a point to a line" needs a precise definition. It is the length of the shortest segment from the point to any point on the line.
That shortest segment is always perpendicular to the line. This is why the topic belongs in a chapter about perpendicular lines. Any other segment from the point to the line is the hypotenuse of a right triangle whose leg is the perpendicular segment, so it must be longer.
The point where the perpendicular meets the line is called the foot of the perpendicular. The distance is the length from P to this foot.
Method 1: build the perpendicular and find the foot
This method uses only skills from earlier chapters. It is slower, but it shows where the formula comes from.
- Find the slope of the given line.
- Write the line through P with the negative reciprocal slope.
- Solve the two line equations together to get the foot F.
- Use the distance formula between P and F.
Worked example
Find the distance from P = (1, 5) to the line y = 2x − 3.
- The slope is 2.
- The perpendicular slope is −1/2. Through (1, 5): b = 5 − (−1/2)(1) = 11/2, so y = −(1/2)x + 11/2.
- Set the two expressions for y equal: 2x − 3 = −(1/2)x + 11/2. Multiply by 2: 4x − 6 = −x + 11, so 5x = 17 and x = 17/5. Then y = 2(17/5) − 3 = 19/5. The foot is F = (17/5, 19/5).
- The differences are 17/5 − 1 = 12/5 and 19/5 − 5 = −6/5. So d = √((12/5)² + (6/5)²) = √(180/25) = (6√5)/5, which is about 2.683.
Keep exact fractions until the last step. Rounding the foot's coordinates early is the most common source of small errors in this method.
Method 2: the formula
Write the line in standard form, Ax + By + C = 0. The distance from (x₀, y₀) to the line is:
d = |Ax₀ + By₀ + C| / √(A² + B²)
Worked example, same problem
Rewrite y = 2x − 3 as 2x − y − 3 = 0, so A = 2, B = −1, C = −3. For P = (1, 5):
- Numerator: |2(1) + (−1)(5) − 3| = |−6| = 6
- Denominator: √(4 + 1) = √5
- d = 6/√5 = (6√5)/5
This matches Method 1.
Why the formula works
The vector (A, B) is perpendicular to the line Ax + By + C = 0. Dividing by √(A² + B²) turns it into a unit direction. The expression Ax₀ + By₀ + C measures how far P is from the line in units of that scaled normal. The absolute value removes the sign, which only tells you which side of the line P is on.
A useful consequence: the sign of Ax₀ + By₀ + C (before taking the absolute value) tells you which side of the line the point lies on. Two points give the same sign when they are on the same side.
Computing it in code
The function below applies the formula. math.hypot(A, B) computes √(A² + B²).
import math
def point_line_distance(A, B, C, point):
# distance from (x0, y0) to the line Ax + By + C = 0
x0, y0 = point
return abs(A * x0 + B * y0 + C) / math.hypot(A, B)
print(point_line_distance(3, 4, -10, (0, 0))) # 2.0
print(point_line_distance(2, -1, -3, (1, 5))) # about 2.683The first call checks the line 3x + 4y − 10 = 0. Here √(9 + 16) = 5, so the distance from the origin is 10/5 = 2.
Converting other forms to standard form
The formula needs Ax + By + C = 0. Convert before substituting.
| Given form | Example | Standard form | A, B, C |
|---|---|---|---|
| Slope-intercept | y = 2x − 3 | 2x − y − 3 = 0 | 2, −1, −3 |
| Point-slope | y − 1 = (1/3)(x − 4) | x − 3y − 1 = 0 | 1, −3, −1 |
| Vertical line | x = 7 | x + 0y − 7 = 0 | 1, 0, −7 |
| Horizontal line | y = −2 | 0x + y + 2 = 0 | 0, 1, 2 |
For the point-slope row: multiply both sides by 3 to get 3y − 3 = x − 4, then move everything to one side to get x − 3y − 1 = 0.
You can also multiply the whole equation by any nonzero constant. The distance does not change, because the constant cancels between numerator and denominator. This lets you clear fractions first.
Special cases
Vertical and horizontal lines need no special handling in the formula, which is one of its advantages. Method 1 breaks down for vertical lines because the slope is undefined.
- Vertical line x = k. Here A = 1, B = 0, C = −k, so d = |x₀ − k|. This is the horizontal gap between the point and the line.
- Horizontal line y = k. Here A = 0, B = 1, C = −k, so d = |y₀ − k|. This is the vertical gap.
- Point on the line. The numerator is 0, so d = 0.
Common errors
- Using slope-intercept coefficients directly. For y = 2x − 3, the numbers 2 and −3 are not A and C. You must rearrange to 2x − y − 3 = 0, which gives B = −1.
- Sign errors in C. Moving a term across the equals sign changes its sign. Check by substituting a point you know is on the line, and confirm the numerator is 0.
- Rounding too early. Keep radicals or fractions exact until the final answer.
- Using the formula on a line not in the form Ax + By + C = 0. If the right side is not 0, move everything to the left first.
- Mixing up the two points. The formula uses the coordinates of the point in the numerator and the line's coefficients throughout. It is not the distance between two points.
Choosing between the methods
| Question | Method 1 (foot of perpendicular) | Method 2 (formula) |
|---|---|---|
| Need only the distance | Slower | Faster |
| Need the closest point on the line | Gives it directly | Does not give it |
| Vertical line | Needs separate handling | Works unchanged |
| Risk of arithmetic error | More steps | Fewer steps |
Use the formula when you only need the distance. Use Method 1 when you need the foot itself, for example when reflecting a point across a line or finding the closest point.
Connection to the next chapter
If two lines are parallel, every point on one line is the same distance from the other line. So the distance between parallel lines reduces to a point-to-line distance: pick any convenient point on one line and apply the formula to the other. The next chapter develops this and gives a shortcut when both lines share the same A and B.
Practice
Exercise
Find the distance from the point (−2, 3) to the line 5x − 12y + 4 = 0.
Show answer
Numerator: |5(−2) − 12(3) + 4| = |−10 − 36 + 4| = |−42| = 42. Denominator: √(25 + 144) = √169 = 13. So d = 42/13, about 3.23.
Exercise
Find the distance from (6, 1) to the line y = −(3/4)x + 2 using the formula. Convert to standard form first.
Show answer
Multiply y = −(3/4)x + 2 by 4: 4y = −3x + 8, so 3x + 4y − 8 = 0. Numerator: |3(6) + 4(1) − 8| = |14| = 14. Denominator: √(9 + 16) = 5. So d = 14/5 = 2.8.
Exercise
Use Method 1 to find the foot of the perpendicular from (0, 0) to the line 3x + 4y − 10 = 0, then confirm the distance of 2 from the code example.
Show answer
The line has slope −3/4, so the perpendicular through the origin has slope 4/3: y = (4/3)x. Substitute into the line: 3x + 4(4/3)x = 10, so 3x + (16/3)x = 10, so (25/3)x = 10 and x = 6/5. Then y = (4/3)(6/5) = 8/5. The foot is (6/5, 8/5). The distance is √((6/5)² + (8/5)²) = √(100/25) = 2.