TExcelTAD
Home › Algebra › Graphing Linear Equations

Graphing Linear Equations

The graph of an equation is the set of all points (x,y)(x, y) that make it true. For a linear equation such as y=2x−1y = 2x - 1 or 2x+3y=62x + 3y = 6, 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 xx-values, compute yy, plot the pairs and connect them. For y=2x−1y = 2x - 1:

xx −1 0 1 2 3
y=2x−1y = 2x - 1 −3 −1 1 3 5

Each step of 1 in xx raises yy 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 xx-intercept is where the line crosses the xx-axis (y=0y = 0); the yy-intercept is where it crosses the yy-axis (x=0x = 0). For an equation in standard form like 2x+3y=62x + 3y = 6 they are the fastest pair of points:

x=0:3y=6→y=2y-intercept (0,2)y=0:2x=6→x=3x-intercept (3,0)check with x=6:12+3y=6→y=−2so (6,−2) is on the line\begin{aligned} x = 0: &\quad 3y = 6 \to y = 2 && y\text{-intercept } (0, 2) \\ y = 0: &\quad 2x = 6 \to x = 3 && x\text{-intercept } (3, 0) \\ \text{check with } x = 6: &\quad 12 + 3y = 6 \to y = -2 && \text{so } (6, -2) \text{ is on the line} \end{aligned}
python · try it yourself
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 (0,0)(0, 0)); then use a table.

Method 3: slope and intercept

If the equation is solved for yy, as in y=mx+by = mx + b, it tells you how to draw it directly: start at the yy-intercept (0,b)(0, b), then for every 1 step right go mm steps up (down if mm is negative). For y=−12x+4y = -\frac{1}{2}x + 4: start at (0,4)(0, 4), then go 2 right and 1 down, to (2,3)(2, 3), (4,2)(4, 2) and so on. The number mm is the slope, the subject of Slope of a Line.

Horizontal and vertical lines

Equation Graph Why
y=3y = 3 horizontal line through (0,3)(0, 3) yy is 3 whatever xx is
x=−2x = -2 vertical line through (−2,0)(-2, 0) xx is −2 whatever yy is
y=0y = 0 the xx-axis
x=0x = 0 the yy-axis

A vertical line is the one line that cannot be written as y=mx+by = mx + b, because a single xx has infinitely many yy-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 (4,−1)(4, -1) on 2x+3y=62x + 3y = 6? 2⋅4+3⋅(−1)=8−3=5≠62 \cdot 4 + 3 \cdot (-1) = 8 - 3 = 5 \ne 6, so no. Is (−3,4)(-3, 4)? −6+12=6-6 + 12 = 6, 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 (0,0)(0, 0) to (10,4)(10, 4), a program must decide, for each column xx, which row yy to light up. The exact line is y=0.4xy = 0.4x, so you could compute round(0.4 * x) for each xx. 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 xx.
  • 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.
python · try it yourself
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 xx and yy 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 y=3−xy = 3 - x with x=−1,0,1,2,3,4x = -1, 0, 1, 2, 3, 4. Where does the line cross the axes?

Answer

y=4,3,2,1,0,−1y = 4, 3, 2, 1, 0, -1; it crosses the yy-axis at (0,3)(0, 3) and the xx-axis at (3,0)(3, 0).

Solution
text
x:      −1   0   1   2   3   4
y=3−x:   4   3   2   1   0  −1
x = 0 → y = 3      y = 0 → x = 3

Exercise 2: Intercepts

Find both intercepts of 4x−5y=204x - 5y = 20 and one more point as a check.

Answer

xx-intercept (5,0)(5, 0), yy-intercept (0,−4)(0, -4); for example (10,4)(10, 4) is also on the line.

Solution
y=0:4x=20→x=5x=0:−5y=20→y=−4x=10:40−5y=20→y=4check point (10,4)\begin{aligned} y = 0: &\quad 4x = 20 \to x = 5 \\ x = 0: &\quad -5y = 20 \to y = -4 \\ x = 10: &\quad 40 - 5y = 20 \to y = 4 && \text{check point } (10, 4) \end{aligned}

Exercise 3: On the line or not?

Which of (2,1)(2, 1), (−1,7)(-1, 7) and (0,4)(0, 4) lie on y=−2x+5y = -2x + 5?

Answer

(2,1)(2, 1) and (−1,7)(-1, 7); (0,4)(0, 4) does not.

Solution
(2,1):−4+5=1✓(−1,7):2+5=7✓(0,4):0+5=5≠4×\begin{aligned} (2, 1)&: & -4 + 5 &= 1 && \checkmark \\ (-1, 7)&: & 2 + 5 &= 7 && \checkmark \\ (0, 4)&: & 0 + 5 &= 5 \ne 4 && \times \end{aligned}
python · try it yourself
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 (0,0)(0, 0) to (6,3)(6, 3) by rounding y=x/2y = x/2 to the nearest integer, rounding halves up. Then compare with bresenham(0, 0, 6, 3).

Answer

Rounding halves up gives (0,0)(0, 0), (1,1)(1, 1), (2,1)(2, 1), (3,2)(3, 2), (4,2)(4, 2), (5,3)(5, 3), (6,3)(6, 3). Bresenham gives (0,0)(0, 0), (1,0)(1, 0), (2,1)(2, 1), (3,1)(3, 1), (4,2)(4, 2), (5,2)(5, 2), (6,3)(6, 3). Both are valid: at x=1x = 1, 3 and 5 the true line is exactly halfway between two rows, so either choice is equally close.

Solution
text
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    3

Bresenham'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.

python · try it yourself
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 yy-intercept of 5x+2y=105x + 2y = 10?

  1. (2,0)(2, 0)
  2. (0,5)(0, 5)
  3. (0,2)(0, 2)
  4. (5,0)(5, 0)

Which equation describes a vertical line?

  1. y=4y = 4
  2. y=xy = x
  3. x=4x = 4
  4. y=4xy = 4x

Why did Bresenham's algorithm matter for early graphics?

  1. It draws curves exactly
  2. It picks the pixels of a line using only integer additions and comparisons
  3. It removes the need for a screen
  4. It works only for vertical lines