Equations of Lines
A straight line can be written in several equivalent forms, and each one is convenient for a different job: slope-intercept form for graphing, point-slope form for building a line from what you know, and standard form for systems of equations. This lesson covers all three, parallel and perpendicular lines, and linear interpolation, the line formula hiding inside every animation and slider.
The three forms
| Form | Equation | You can read off | Best for |
|---|---|---|---|
| slope-intercept | slope , -intercept | graphing, computing from | |
| point-slope | slope , one point | writing a line from a point and a slope | |
| standard | both intercepts quickly | systems of equations, vertical lines |
All three describe the same line; you move between them with ordinary algebra. Standard form is the only one that can write a vertical line ( is ).
Building a line
From a point and a slope. The line through with slope 3:
Point-slope form is just the slope formula rearranged: , multiplied by .
From two points. Through and : first the slope, then point-slope with either point.
from fractions import Fraction as F
def line_forms(p, q):
(x1, y1), (x2, y2) = p, q
if x1 == x2:
return f"vertical line x = {x1}"
m = F(y2 - y1, x2 - x1)
b = y1 - m * x1
# standard form with whole numbers: multiply y = mx + b by m's denominator
A, B, C = -m.numerator, m.denominator, b * m.denominator
return f"y = {m}x + {b} | {A}x + {B}y = {C}"
print(line_forms((-1, 4), (3, -4)))
print(line_forms((0, 1), (4, 3)))
print(line_forms((5, 0), (5, 9)))y = -2x + 2 | 2x + 1y = 2
y = 1/2x + 1 | -1x + 2y = 2
vertical line x = 5
The second line, , becomes in standard form (multiply by 2). Many teachers prefer a positive , so is the same line written the conventional way.
Parallel and perpendicular lines
| Relationship | Slopes | Example |
|---|---|---|
| parallel | equal: | and |
| perpendicular | negative reciprocals: | and |
Parallel lines rise at the same rate and never meet (that is the "no solution" system from Systems of Equations). For perpendicular lines, rotating a line by 90° turns a run-rise step into , whose slope is .
- The line parallel to through : slope 2, so , which is .
- The line perpendicular to it through : slope , so , which is .
Horizontal and vertical lines are perpendicular too, the one pair the product rule cannot express (0 times "undefined").
In software: linear interpolation (lerp)
Suppose you know two points of a line and want the value in between. The two-point line, written slightly differently, is linear interpolation:
The fraction says how far is along the way from to (0 at the start, 1 at the end). With it the formula becomes the famous one-liner every game engine and UI framework ships:
def lerp(a, b, t):
return a + (b - a) * t
def inverse_lerp(a, b, v):
return (v - a) / (b - a)
def remap(v, a, b, c, d):
return lerp(c, d, inverse_lerp(a, b, v))
# a keyframe animation: x = 100 px at 0 s, x = 400 px at 2 s
for t in [0.0, 0.5, 1.0, 2.0]:
print(f"at {t} s: x = {remap(t, 0, 2, 100, 400)} px")
# a health bar: 0..250 HP drawn as 0..120 px
print("180 HP ->", round(remap(180, 0, 250, 0, 120), 2), "px")
# Celsius to Fahrenheit is the line through (0, 32) and (100, 212)
print("37 °C ->", round(remap(37, 0, 100, 32, 212), 1), "°F")at 0.0 s: x = 100.0 px
at 0.5 s: x = 175.0 px
at 1.0 s: x = 250.0 px
at 2.0 s: x = 400.0 px
180 HP -> 86.4 px
37 °C -> 98.6 °F
Each remap call is the equation of the line through two known points, evaluated at a new . Animation systems interpolate between keyframes this way, colour pickers blend colours channel by channel, and chart code maps data values to pixels (the screen mapping in Coordinate Systems is a remap). For curved motion, is first bent by an easing function; see Lerp, Inverse Lerp and Remap and Easing Curves.
Note: Interpolation ( between and ) is safe. Extrapolation ( outside that range, or ) still follows the line but may leave the range you intended, such as a health bar wider than its frame. Clamp to when that matters.
Exercises
Exercise 1: Point and slope
Write the line with slope 4 through in all three forms.
Answer
; ; .
Solution
Exercise 2: Two points
Find the equation of the line through and in slope-intercept and standard form.
Answer
; .
Solution
Exercise 3: Read the slope
Rewrite in slope-intercept form. What are the slope and the -intercept?
Answer
: slope , -intercept .
Solution
Exercise 4: Perpendicular
Find the line perpendicular to that passes through .
Answer
(standard form ).
Solution
from fractions import Fraction as F
m1, m2 = F(2, 5), F(-5, 2)
print("product of slopes:", m1 * m2, " y at x=2:", m2 * 2 + 4)product of slopes: -1 y at x=2: -1
Exercise 5: A volume slider
A volume slider goes from 0 to 100 and sets a gain from −60 dB to 0 dB along a straight line. What gain does slider position 75 give, and which slider position gives −42 dB? Use remap.
Answer
75 gives −15 dB; −42 dB is at position 30.
Solution
def lerp(a, b, t): return a + (b - a) * t
def inverse_lerp(a, b, v): return (v - a) / (b - a)
def remap(v, a, b, c, d): return lerp(c, d, inverse_lerp(a, b, v))
print(remap(75, 0, 100, -60, 0), remap(-42, -60, 0, 0, 100))-15.0 30.0
Test yourself
Which line is parallel to ?
What is the slope of a line perpendicular to ?
lerp(10, 30, 0.25) equals…
- 15
- 20
- 12.5
- 7.5