1. Bresenham's Line Algorithm

Drawing straight lines on a pixel grid using only integer arithmetic

Before you can render a 3D wireframe, you need to solve a more basic problem: how do you draw a straight line between two pixels?

A mathematical line is infinitely thin and perfectly smooth. A screen is a grid of discrete squares. Drawing a line means choosing which pixels to light up so the result looks straight. Bresenham's line algorithm, published in 1962, solves this using nothing but addition, subtraction, and bit shifts — no multiplication, no division, no floating point.

The Naive Approach

The slope-intercept form of a line is:

y=mx+by = mx + b

where m = (y1 - y0) / (x1 - x0). You could step through each x-coordinate from x0 to x1, compute y, and round to the nearest integer:

for x from x0 to x1:
    y = round(y0 + (x - x0) * dy / dx)
    plot(x, y)

This works, but every step requires a floating-point multiply and a divide. In 1962, on an IBM 7090 driving a Calcomp plotter, that was unacceptably slow. Even today, when a GPU rasterizes millions of lines per frame, integer-only arithmetic matters.

The Key Insight: Accumulate the Error

Instead of computing the exact y-coordinate at each step, Bresenham tracks how far the ideal line has drifted from the current pixel row. This accumulated drift is called the error term.

Start at (x0, y0). At each step, move one pixel to the right (x++). The ideal y-value increases by the slope dy/dx. Instead of computing that fraction, we track the error as an integer:

  1. Initialize error = 0
  2. At each step, add dy to the error
  3. If error >= dx, the ideal line has crossed into the next row — step y up by one and subtract dx from the error

That's it. The error oscillates around zero, and every time it crosses the threshold we step diagonally instead of horizontally.

Why it works

The error term is proportional to the vertical distance between the ideal line and the current pixel center. By scaling everything by dx, we eliminate the division entirely. The decision at each pixel — "go right" or "go right and up" — depends only on the sign of the accumulated error.

The Algorithm Step by Step

Let's draw a line from (1, 1) to (8, 4). That gives us:

  • dx = 8 - 1 = 7
  • dy = 4 - 1 = 3

We use a decision variable D initialized to 2*dy - dx:

Step x y D Decision
start 1 1 2(3) - 7 = -1
1 2 1 -1 + 2(3) = 5 D < 0 before step, go right
2 3 2 5 - 2(7) + 2(3) = -3 D >= 0, go right+up
3 4 2 -3 + 2(3) = 3 D < 0, go right
4 5 3 3 - 2(7) + 2(3) = -5 D >= 0, go right+up
5 6 3 -5 + 2(3) = 1 D < 0, go right
6 7 4 1 - 2(7) + 2(3) = -7 D >= 0, go right+up
7 8 4 Done

The pixels lit up are: (1,1), (2,1), (3,2), (4,2), (5,3), (6,3), (7,4), (8,4).

Pseudocode

Here's the full algorithm for lines where 0 < slope < 1 (the "gentle slope" case, where dx > dy > 0):

function bresenham(x0, y0, x1, y1):
    dx = x1 - x0
    dy = y1 - y0
    D = 2 * dy - dx
    y = y0

    for x from x0 to x1:
        plot(x, y)
        if D > 0:
            y = y + 1
            D = D - 2 * dx
        D = D + 2 * dy

Notice: no division, no floating point. Every operation is an integer add or subtract.

Handling All Octants

The algorithm above only handles lines going right and slightly upward (octant 1). A complete implementation must handle all eight octants — lines that go left, steeply up, steeply down, etc.

The standard approach:

  1. Swap endpoints if x0 > x1 (always draw left to right)
  2. Check if the slope is steep (|dy| > |dx|). If so, swap the roles of x and y — step along y and decide about x
  3. Handle negative dy by stepping y down instead of up
function bresenhamFull(x0, y0, x1, y1):
    steep = abs(y1 - y0) > abs(x1 - x0)
    if steep:
        swap(x0, y0)
        swap(x1, y1)
    if x0 > x1:
        swap(x0, x1)
        swap(y0, y1)

    dx = x1 - x0
    dy = abs(y1 - y0)
    D = 2 * dy - dx
    y = y0
    yStep = (y0 < y1) ? 1 : -1

    for x from x0 to x1:
        if steep:
            plot(y, x)
        else:
            plot(x, y)
        if D > 0:
            y = y + yStep
            D = D - 2 * dx
        D = D + 2 * dy

Try It: Line Drawer

Click two points on the grid to draw a line between them. The algorithm steps are shown below the grid.

Click to place the first endpoint

Why This Matters for 3D Rendering

Every wireframe renderer draws lines between projected vertices. Every rasterizer fills triangles by scanning along edges — which is really just Bresenham's algorithm applied to two edges at once. The rasterization article later in this series builds directly on these ideas.

Understanding line drawing at the pixel level also demystifies aliasing — the staircase artifacts you see on diagonal lines. Anti-aliasing techniques like Xiaolin Wu's algorithm extend Bresenham's approach by partially lighting adjacent pixels to smooth those staircases.

Summary

Bresenham's line algorithm turns a geometric line into a sequence of pixel decisions:

  • Track the error between the ideal line and the current pixel
  • At each step, either go straight or step diagonally, based on the error's sign
  • All integer arithmetic — no division, no floating point
  • Extends to all directions by swapping axes and adjusting step direction

It's one of the oldest algorithms in computer graphics, and it's still running inside your GPU today.