Calculate the Equation of a Line Through Two Points
Two distinct points determine exactly one line. From their coordinates, one can work out its slope (how steeply it rises or falls), its y-intercept (where it crosses the y-axis), and its full equation — either slope-intercept form y = mx + b or general form ax + by + c = 0.
Enter the coordinates of both points: the calculator gives both forms of the equation, including the special case of a vertical line, where the slope is undefined.
Coordinates of the Two Points
An asterisk after a term points to its entry in the glossary.
Slope-intercept y = mx + b (US) / y = mx + c (UK), and the general form
Two writings, one line. The slope-intercept* form puts the gradient and the intercept on the y-axis in plain sight. US textbooks write y = mx + b. UK and GCSE papers write y = mx + c. Same equation, different letter for the intercept. This page uses both names; the calculator’s result field says “b” because that is the more widely searched symbol, but c in a UK classroom is the same number.
The general form* ax + by + c = 0 plays no favourite with the axes, and swallows a vertical line without complaint. Reach for y = mx + b when m is wanted at a glance, when sketching from the intercept, when comparing two lines by eye. Reach for ax + by + c = 0 when a vertical is possible, when the distance from a point to the line is needed, when a piece of software would rather hold three coefficients than a division.
y = m x + b (UK: y = m x + c)
a x + b y + c = 0
Conversion is mechanical. From y = mx + b into general: mx − y + b = 0 (so a = m, the coefficient of y is −1, and the constant term is the intercept). Back again, if the coefficient of y is not zero: y = (−a/b)x − c/b. If that coefficient is zero, there is nothing to divide: that is the vertical, and the intercept form does not exist. The “better” form is the one that answers today’s question, not a permanent ranking.
A vertical line is x = constant, not a slope-intercept equation
This is the case the intercept form cannot write. Two points with the same x and different y determine a vertical line. There is no gradient* m, because (y₂ − y₁)/(x₂ − x₁) divides by zero. The equation is x = k, where k is that common abscissa. In general form: x − k = 0, or any multiple of it.
y = mx + b is the graph of a function: one y for each x. A vertical line fails that test — infinitely many y for one x — so it is never a function, and never a slope-intercept equation. Calling the gradient “infinite” does not repair the algebra. Infinity is not a number to print, and substituting it into mx + b does not yield x = 3. The calculator drops the intercept form in this case and keeps the general form. That is the geometry, not a bug.
A vertical line is x = constant. It has no slope-intercept equation. “Undefined slope” is the correct report; “very large m” is not.
Gradient from two points; horizontal and vertical extremes
Given A(x₁, y₁) and B(x₂, y₂), the gradient is the rise over the run.
Horizontal: y does not change. m = 0, equation y = b, general form along the lines of y − b = 0. Every point shares the same ordinate. Vertical: the case above. The two extremes answer each other: a horizontal and a vertical are perpendicular, even though one of them has no m. Two verticals are parallel to each other; two horizontals likewise.
The same two differences feed the distance and midpoint calculator. Distance squares them and takes a root. The gradient divides them. Midpoint averages the raw coordinates. Three readings of one pair of points; the line is the fourth.
Parallel and perpendicular from m
Two non-vertical lines are parallel* when their gradients are equal. They are perpendicular* when the product of the gradients is −1 — one gradient is the negative reciprocal of the other.
parallel: m₁ = m₂ ; perpendicular: m₁ · m₂ = −1
Gradient 2, the perpendicular is −1/2. Gradient −3, the perpendicular is 1/3. Zero and undefined answer each other, as already noted. The general form says the same without passing through m. ax + by + c = 0 has normal vector (a, b) and direction vector (−b, a). Two lines are parallel if their normals are scalar multiples (a₁/a₂ = b₁/b₂), perpendicular if a₁a₂ + b₁b₂ = 0. Useful when one of them has no intercept form.
A percentage gradient is not an angle. 10% means m = 0.10 (a rise of 0.10 on a run of 1), not 10°. The angle with the positive x-axis is arctan(m). Mixing the two, on an access ramp or a roof pitch, is not a rounding error. 100% is 45°, not vertical; vertical has no percentage at all.
Point–slope when only one point is known
Two points are not always to hand. Sometimes there is a point and a gradient: a roof at 1 in 10, a ramp, a constant speed. One line of algebra, the point–slope* form (point–gradient, in UK texts).
y − y₁ = m (x − x₁)
Expand, tidy, and y = mx + b returns, with b = y₁ − m x₁. That is exactly what this calculator does, except it first finds m from the second point. Here m is given, so the second point is spare. One trap: “vertical through (3, 5)” has no m. The equation is x = 3, and the point–slope form does not get written. Another: a gradient given as an angle. Convert first, m = tan θ, with θ measured from the positive x-axis, not from the vertical.
Where two lines meet
A shared point, if there is one, satisfies both equations at once. The pair is a simultaneous system. In intercept form, y = m₁x + b₁ and y = m₂x + b₂, set them equal: m₁x + b₁ = m₂x + b₂, so x = (b₂ − b₁) / (m₁ − m₂), then y from either. If m₁ = m₂ and the intercepts differ, distinct parallels, no crossing. If m₁ = m₂ and the intercepts match, it is the same line, infinitely many points. In general form it is a 2 × 2 system. A vanishing determinant a₁b₂ − a₂b₁ says the same: parallel or coincident.
x = (b₂ − b₁) / (m₁ − m₂) (if m₁ ≠ m₂)
A vertical x = k cut by y = mx + b is the easy case: x is k, y is mk + b. Intercept form on one side, a constant on the other, and they get along. This calculator does not cross two lines. It builds one. Crossing is a simultaneous system, on paper or in another tool. Three lines in the plane may form a triangle, meet in one point, or fail to meet at all; that is a different count again.
Distance from a point to a line
The shortest distance from a point M(x₀, y₀) to a line is the length of the perpendicular. With ax + by + c = 0 it is one fraction.
The numerator is the general form evaluated at M, taken absolute. If it is zero, M lies on the line, d = 0. The denominator scales: without it, doubling a, b and c would change the answer, even though the line is the same. Hence the root, which is the magnitude of the vector (a, b).
The formula wants the general form, right-hand side zero. Starting from y = mx + b without bringing everything to one side (mx − y + b = 0) is not optional. Dropping the absolute value gives a signed offset, not a distance.
This page does not compute d. It hands over a, b and c. The rest is one extra line, once the point M is known. The distance between two points is a different length: point to point, not point to line.
Worked case: (1, 2) and (4, 8)
A(1, 2), B(4, 8). The gaps: 3 to the right, 6 up.
- Gradient: m = (8 − 2) / (4 − 1) = 6/3 = 2.
- Y-intercept: b = 2 − 2 × 1 = 0.
- Slope-intercept: y = 2x + 0, that is y = 2x. (UK: y = 2x + c with c = 0.)
- General form: 6x − 3y = 0, or simplified 2x − y = 0.
The line goes through the origin. Not a requirement — a gift of these two points. Check: x = 1 gives y = 2, x = 4 gives y = 8. A perpendicular would have gradient −1/2, for instance y = −(1/2)x + 2.5 if forced through A. Distance from (0, 4) to this line, using 2x − y = 0: |0 − 4| / √5 = 4/√5 ≈ 1.789. The unsimplified 6 and 3 give the same quotient; that is the denominator earning its keep.
Cartesian plane, intercepts, and a line that misses the origin
x across, y up, O(0, 0) where they cross. A line in this plane generally cuts both axes. The y-intercept b (or c) is the ordinate of the point where the line meets the y-axis (x = 0). The x-intercept, if m ≠ 0, is −b/m: that is where it meets the x-axis (y = 0). The line y = 2x + 3 meets the y-axis at (0, 3) and the x-axis at (−1.5, 0). To sketch, two points are enough: those two, and a ruler.
Without an origin, b would not mean anything. Shift the axes and b changes, m does not: the gradient belongs to the line, the intercept belongs to the line in this coordinate system. The same rail drawn on two shifted sheets does not share a b. That is one reason the general form is sometimes kinder — it is less glued to a particular axis. If O is out of frame on the diagram, b still appears in the numbers — it may fall off the sketch. No contradiction. The line has no end.
If b = 0 the line goes through O, as in the (1, 2)–(4, 8) example. If m = 0 it does not meet the x-axis (horizontal, unless y = 0, which is the x-axis). If it is vertical it does not meet the y-axis (unless x = 0, which is that axis), and the x-intercept is simply its constant k. The exceptions are easier to see on the sketch than in the algebra — another reason to keep the vertical as a case, not as a “very large” gradient. Other tools in the geometry section stay with lengths and areas; this one stays with an equation.
A gradient as a percentage is not an angle
Roads, ramps and roofs quote a percentage: “1 in 10”, “10%”, “a 4 in 12 pitch”. All of those are values of m, or of 100m, not of an angle in degrees. 10% is m = 0.10, angle arctan(0.10) ≈ 5.7°. A 4-in-12 roof is m = 4/12 = 1/3, about 18.4°. A 45° slope is 100%, not “steep enough to be called vertical”. Vertical remains x = constant.
Engineering drawings sometimes give the angle to the horizontal, sometimes the ratio. Converting one into the other before writing y = mx + b avoids a line that is ten times too steep. The calculator does not convert angles; it takes two points. A point and a percentage still go through the point–slope form, once the percentage has become a decimal m.
Glossary
- Slope-intercept form
- y = mx + b in US usage, y = mx + c in UK/GCSE usage. m is the gradient; b (or c) is the y-intercept, where the line meets the y-axis. A vertical line has no such equation.
- General form
- ax + by + c = 0. Handles every line, including the vertical (b = 0). The coefficients are unique only up to a common factor: 2x − y = 0 and 6x − 3y = 0 are the same line.
- Gradient (slope)
- m = (y₂ − y₁)/(x₂ − x₁). How much y changes for a unit change in x. GCSE: gradient. US: slope. Undefined on a vertical line. m = 1 is 45° on an equal-scale grid.
- Parallel
- Two lines that never meet. Non-vertical: equal gradients. Two verticals are parallel to each other. In general form, the normals (a, b) are scalar multiples.
- Perpendicular
- Two lines that meet at a right angle. Non-vertical: m₁ m₂ = −1. A horizontal and a vertical are perpendicular even though one gradient is undefined. In general form: a₁a₂ + b₁b₂ = 0.
- Point–slope form
- y − y₁ = m(x − x₁). The equation of the line through a known point with a known gradient. US name: point–slope. UK name: point–gradient. Does not cover a vertical line.