Sub topic 10
Writing an Equation from Two Points
Prerequisites and goal
This chapter assumes you can compute slope from two points, read y = mx + b, and write an equation from a slope and a point. You also need signed-number and fraction arithmetic, and you need to solve a one-step equation for an unknown.
Two points determine exactly one line. This chapter turns that fact into a procedure: find the slope, then find the intercept. You will also learn to check the result, handle special cases, and avoid the common errors.
The idea: two steps, both already known
Writing an equation from two points is not a new technique. It chains two earlier ones.
- Use the slope formula on the two points to get m.
- Use m and either point to find b, exactly as in "slope and a point".
The result is y = mx + b.
Worked example 1: both points with positive coordinates
Find the line through (1, 3) and (3, 7).
Step 1: slope.
m = (7 − 3) / (3 − 1) = 4 / 2 = 2
Step 2: intercept. Substitute m = 2 and the point (1, 3) into y = mx + b:
3 = 2(1) + b 3 = 2 + b b = 1
Step 3: equation. y = 2x + 1.
Step 4: check. Substitute the point you did not use, (3, 7): 2(3) + 1 = 7. It works.
The check matters. A single arithmetic slip in m or b usually makes the second point fail.
The same procedure in code:
def line_through(x1, y1, x2, y2):
if x1 == x2:
raise ValueError("A vertical line has no slope-intercept form")
m = (y2 - y1) / (x2 - x1)
b = y1 - m * x1
return m, b
m, b = line_through(1, 3, 3, 7)
print(f"y = {m}x + {b}") # y = 2.0x + 1.0The line b = y1 - m * x1 is step 2 solved for b. Starting from y1 = m·x1 + b, subtract m·x1 from both sides.
Worked example 2: negative slope and a negative intercept
Find the line through (−2, 1) and (4, −5).
Slope.
m = (−5 − 1) / (4 − (−2)) = −6 / 6 = −1
Subtracting a negative number is the most common place to slip. Here 4 − (−2) = 6, not 2.
Intercept. Use (−2, 1):
1 = (−1)(−2) + b 1 = 2 + b b = −1
Equation. y = −x − 1.
Check with (4, −5). −(4) − 1 = −5. It works.
Worked example 3: a fractional slope
Find the line through (2, 1) and (8, 5).
m = (5 − 1) / (8 − 2) = 4/6 = 2/3
Use (2, 1):
1 = (2/3)(2) + b 1 = 4/3 + b b = 1 − 4/3 = −1/3
The equation is y = (2/3)x − 1/3.
Check with (8, 5): (2/3)(8) − 1/3 = 16/3 − 1/3 = 15/3 = 5. It works.
Fractions are exact in algebra. In code, floating-point division can introduce tiny rounding errors, so use Fraction when you want exact answers:
from fractions import Fraction
def line_through_exact(x1, y1, x2, y2):
if x1 == x2:
raise ValueError("A vertical line has no slope-intercept form")
m = Fraction(y2 - y1, x2 - x1)
b = y1 - m * x1
return m, b
m, b = line_through_exact(2, 1, 8, 5)
print(f"y = ({m})x + ({b})") # y = (2/3)x + (-1/3)Does it matter which point you use?
No. Both points lie on the line, so either gives the same b. Choose the point that makes the arithmetic easier, for example one with smaller numbers or a zero coordinate.
Take the line through (1, 3) and (3, 7) with m = 2:
| Point used | Substitution | b |
|---|---|---|
| (1, 3) | 3 = 2(1) + b | 1 |
| (3, 7) | 7 = 2(3) + b | 1 |
If the two results differ, you made an arithmetic error. Using both points is therefore a built-in check.
Order of the points does not matter, but consistency does
You may label either point as (x1, y1). The slope comes out the same:
(7 − 3) / (3 − 1) = 2 and (3 − 7) / (1 − 3) = −4 / −2 = 2
What you must not do is mix the order, such as y2 − y1 in the numerator and x1 − x2 in the denominator. That flips the sign of m.
Special cases
Horizontal lines
If y1 = y2, the numerator is 0, so m = 0. For (−3, 4) and (5, 4):
m = (4 − 4) / (5 − (−3)) = 0 / 8 = 0
Then b = 4 − 0·(−3) = 4, so y = 4. Every point has y = 4, which matches the picture of a flat line.
Vertical lines
If x1 = x2, the denominator is 0 and the slope is undefined. For (2, −1) and (2, 6), the line is x = 2. It cannot be written as y = mx + b because no single value of y goes with each x. A program should reject it or return a different form, as the code above does with ValueError.
Identical points
If both points are the same, for example (3, 3) and (3, 3), then x1 = x2 and the slope formula fails. But the problem is worse than a vertical line: infinitely many lines pass through one point. Two distinct points are needed to fix a line.
A second route: point-slope form
You can skip solving for b if you write the line directly from one point and the slope:
y − y1 = m(x − x1)
For (1, 3) and (3, 7) with m = 2, this gives y − 3 = 2(x − 1). Distribute and add 3: y = 2x − 2 + 3 = 2x + 1. The result is the same.
| Method | Strength | Weakness |
|---|---|---|
| Solve for b | Goes directly to y = mx + b | Extra substitution step |
| Point-slope form | Fewer sign slips with negative points | Needs one more rearrangement |
With negative coordinates, point-slope form is often safer, because x − x1 turns into x + 2 when x1 = −2 and the sign is explicit.
Verifying a result in code
Because a line can be checked against each input point, a small test catches errors:
def line_through(x1, y1, x2, y2):
if x1 == x2:
raise ValueError("A vertical line has no slope-intercept form")
m = (y2 - y1) / (x2 - x1)
b = y1 - m * x1
return m, b
def check(x1, y1, x2, y2):
m, b = line_through(x1, y1, x2, y2)
assert abs(m * x1 + b - y1) < 1e-9
assert abs(m * x2 + b - y2) < 1e-9
return m, b
print(check(-2, 1, 4, -5)) # (-1.0, -1.0)
print(check(2, 1, 8, 5)) # (0.6666666666666666, -0.33333333333333337)The tolerance of 1e-9 is there because of floating-point rounding. The second output shows why: −1/3 prints as −0.33333333333333337.
Common errors
- Sign slip when subtracting a negative. Write 4 − (−2) with parentheses, then convert to 4 + 2.
- Reversed order in one difference. Keep the same point first in both numerator and denominator.
- Putting x and y in the wrong places. Substitute the x-value for x and the y-value for y, not the reverse.
- Reporting only m. The task asks for an equation, so you need b as well.
- Skipping the check. Substitute the unused point every time.
- Forcing a vertical line into y = mx + b. Write x = a constant instead.
Summary of the procedure
- Label the points (x1, y1) and (x2, y2).
- If x1 = x2, the line is x = x1. Stop.
- Compute m = (y2 − y1) / (x2 − x1) and simplify.
- Substitute m and one point into y = mx + b and solve for b.
- Write y = mx + b.
- Check with the other point.
Exercise
Write the equation of the line through (−1, 5) and (2, −1). Check your answer with the point you did not use for b.
Show answer
m = (−1 − 5) / (2 − (−1)) = −6 / 3 = −2. Using (−1, 5): 5 = (−2)(−1) + b, so 5 = 2 + b and b = 3. The equation is y = −2x + 3. Check with (2, −1): −2(2) + 3 = −1, which matches.
Exercise
Find the equation of the line through (0, 4) and (6, 1). Which point makes solving for b easiest, and why?
Show answer
m = (1 − 4) / (6 − 0) = −3/6 = −1/2. The point (0, 4) is easiest because its x-value is 0, so b = y1 = 4 immediately. The equation is y = −(1/2)x + 4. Check with (6, 1): −(1/2)(6) + 4 = −3 + 4 = 1.
Exercise
A student finds the line through (3, 2) and (7, 10) and writes m = (10 − 2) / (3 − 7) = −2, so y = −2x + 8. Find the error and give the correct equation.
Show answer
The student subtracted in different orders: y2 − y1 on top but x1 − x2 on the bottom. The correct slope is (10 − 2) / (7 − 3) = 8/4 = 2. Using (3, 2): 2 = 2(3) + b, so b = −4. The equation is y = 2x − 4. Check with (7, 10): 2(7) − 4 = 10.
Exercise
What equation describes the line through (4, −3) and (4, 9), and why can't it be written as y = mx + b?
Show answer
Both points have x = 4, so the line is vertical and its equation is x = 4. The slope formula divides by 4 − 4 = 0, so the slope is undefined, and no single y-value belongs to each x-value, so the line is not a function of x.