Graphing Linear Equations
The graph of an equation is the set of all points that make it true. For a linear equation such as or , those points form a straight line, so two points are enough to draw it. This lesson shows three ways to find those points and how a computer turns a line into pixels.
Method 1: a table of values
Pick a few -values, compute , plot the pairs and connect them. For :
| −1 | 0 | 1 | 2 | 3 | |
|---|---|---|---|---|---|
| −3 | −1 | 1 | 3 | 5 |
Each step of 1 in raises by 2: the points climb at a constant rate, which is why they line up. Two points determine the line; a third is a free check against arithmetic slips.
Method 2: the intercepts
The -intercept is where the line crosses the -axis (); the -intercept is where it crosses the -axis (). For an equation in standard form like they are the fastest pair of points:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-2, 7, 50)
y = (6 - 2 * x) / 3 # 2x + 3y = 6 solved for y
plt.figure(figsize=(6, 4))
plt.axhline(0, color="black", linewidth=0.8)
plt.axvline(0, color="black", linewidth=0.8)
plt.plot(x, y, label="2x + 3y = 6")
plt.plot([0, 3, 6], [2, 0, -2], "o", color="tab:red")
for px, py in [(0, 2), (3, 0), (6, -2)]:
plt.annotate(f"({px}, {py})", (px, py), textcoords="offset points", xytext=(6, 6))
plt.grid(True, alpha=0.3)
plt.gca().set_aspect("equal")
plt.legend()
plt.show()
print("intercepts: (0, 2) and (3, 0)")intercepts: (0, 2) and (3, 0)
The intercept method fails only for lines through the origin (both intercepts are ); then use a table.
Method 3: slope and intercept
If the equation is solved for , as in , it tells you how to draw it directly: start at the -intercept , then for every 1 step right go steps up (down if is negative). For : start at , then go 2 right and 1 down, to , and so on. The number is the slope, the subject of Slope of a Line.
Horizontal and vertical lines
| Equation | Graph | Why |
|---|---|---|
| horizontal line through | is 3 whatever is | |
| vertical line through | is −2 whatever is | |
| the -axis | ||
| the -axis |
A vertical line is the one line that cannot be written as , because a single has infinitely many -values. That makes it a line but not the graph of a function.
Does a point lie on the line?
A point lies on the graph exactly when its coordinates satisfy the equation. Is on ? , so no. Is ? , yes.
In software: drawing a line with pixels
A screen has no "line", only a grid of pixels with whole-number coordinates. To draw from to , a program must decide, for each column , which row to light up. The exact line is , so you could compute round(0.4 * x) for each . Early graphics hardware avoided multiplication and fractions entirely; Bresenham's line algorithm (1962) does it with integer additions only:
- Step one column at a time along .
- Keep an integer "error" that measures how far the true line has drifted above the current pixel row.
- When the drift passes half a pixel, move up one row and subtract one pixel's worth of error.
def bresenham(x0, y0, x1, y1):
"""Pixels of a line with 0 <= slope <= 1 and x0 < x1, integers only."""
dx, dy = x1 - x0, y1 - y0
err = 2 * dy - dx # doubled so everything stays an integer
y = y0
pixels = []
for x in range(x0, x1 + 1):
pixels.append((x, y))
if err > 0: # true line is more than half a pixel above
y += 1
err -= 2 * dx
err += 2 * dy
return pixels
pts = bresenham(0, 0, 10, 4)
print(pts)
print("same as rounding 0.4x:", [y for _, y in pts] == [round(0.4 * x) for x in range(11)])
for row in range(4, -1, -1): # print with y up, like maths axes
print("".join("#" if (x, row) in pts else "." for x in range(11)))[(0, 0), (1, 0), (2, 1), (3, 1), (4, 2), (5, 2), (6, 2), (7, 3), (8, 3), (9, 4), (10, 4)]
same as rounding 0.4x: True
.........##
.......##..
....###....
..##.......
##.........
The algorithm lights exactly the pixels nearest to the true line, using only addition, subtraction and comparison. This version handles slopes between 0 and 1; the full algorithm handles the other directions by swapping the roles of and or flipping signs. GPUs today rasterise lines and triangles with related integer "edge function" tests, and the same stepping idea appears in grid ray-casting for games (which tiles does a bullet cross?).
Exercises
Exercise 1: Table of values
Make a table for with . Where does the line cross the axes?
Answer
; it crosses the -axis at and the -axis at .
Solution
x: −1 0 1 2 3 4
y=3−x: 4 3 2 1 0 −1
x = 0 → y = 3 y = 0 → x = 3Exercise 2: Intercepts
Find both intercepts of and one more point as a check.
Answer
-intercept , -intercept ; for example is also on the line.
Solution
Exercise 3: On the line or not?
Which of , and lie on ?
Answer
and ; does not.
Solution
for x, y in [(2, 1), (-1, 7), (0, 4)]:
print((x, y), y == -2 * x + 5)(2, 1) True
(-1, 7) True
(0, 4) False
Exercise 4: Pixels by rounding
Without Bresenham, list the pixels of the line from to by rounding to the nearest integer, rounding halves up. Then compare with bresenham(0, 0, 6, 3).
Answer
Rounding halves up gives , , , , , , . Bresenham gives , , , , , , . Both are valid: at , 3 and 5 the true line is exactly halfway between two rows, so either choice is equally close.
Solution
x: 0 1 2 3 4 5 6
x/2: 0 0.5 1 1.5 2 2.5 3
up: 0 1 1 2 2 3 3Bresenham's test err > 0 (strictly greater) moves up only when the line is more than half a pixel above, so ties stay on the lower row.
import math
def bresenham(x0, y0, x1, y1):
dx, dy = x1 - x0, y1 - y0
err, y, pixels = 2 * dy - dx, y0, []
for x in range(x0, x1 + 1):
pixels.append((x, y))
if err > 0:
y += 1
err -= 2 * dx
err += 2 * dy
return pixels
print([(x, math.floor(x / 2 + 0.5)) for x in range(7)])
print(bresenham(0, 0, 6, 3))[(0, 0), (1, 1), (2, 1), (3, 2), (4, 2), (5, 3), (6, 3)]
[(0, 0), (1, 0), (2, 1), (3, 1), (4, 2), (5, 2), (6, 3)]
Test yourself
What is the -intercept of ?
Which equation describes a vertical line?
Why did Bresenham's algorithm matter for early graphics?
- It draws curves exactly
- It picks the pixels of a line using only integer additions and comparisons
- It removes the need for a screen
- It works only for vertical lines