Theory
The ideal line meets the pixel grid
Mathematically, a line is a perfectly straight, continuous path between two points. But a screen is a grid of discrete pixels at whole-number positions, and the ideal line usually passes between pixels, not neatly through their centres.
So drawing a line on a raster screen is really a problem of approximation: which whole pixels best represent that ideal straight path? Before the algorithms that answer this, you need the geometry: the slope of a line, and why the pixel grid forces a choice. This lesson lays that groundwork; the next turns it into working algorithms.
Theory
Slope: how steeply a line rises
A line from (x1, y1) to (x2, y2) has a slope, written m, that measures how steeply it rises:
m = (y2 - y1) / (x2 - x1) = dy / dx
where dy is the change in y and dx the change in x. For example, a line from (0, 0) to (5, 3) has slope m = 3 / 5 = 0.6, it rises 0.6 units for every 1 unit across. The full line equation is *y = mx + c**, where c is the y-intercept (where the line crosses the y-axis). Slope is the key number: it tells you the line's direction and, crucially, how to step along it when drawing.
Formula
Step along the faster-changing axis
The slope decides how you should walk the line pixel by pixel. If the slope's magnitude is 1 or less (|m| <= 1, the line is more horizontal), you step one pixel at a time along x and compute the matching y. If the magnitude is greater than 1 (|m| > 1, more vertical), you step along y and compute x.
Why? Stepping along the faster-changing axis ensures you place one pixel per row (or column) with no gaps. Step along the wrong axis and a steep line would come out as disconnected dots. This 'step along the dominant axis' rule underlies every line-drawing algorithm.
Quiz
What is the slope of a line drawn from (0, 0) to (5, 3)?
- 5/3, about 1.67
- 3/5, which is 0.6 (rise 3 over run 5)
- 8, the sum of the coordinates
- 1, because all lines have slope 1
Show the answer
3/5, which is 0.6 (rise 3 over run 5)
Slope m = (y2 - y1) / (x2 - x1) = (3 - 0) / (5 - 0) = 3/5 = 0.6. The line rises 3 units over a run of 5 units, so its slope is 0.6. Option A inverts the ratio (run over rise instead of rise over run); slope is dy/dx, not dx/dy. Option C adds the coordinates, which has nothing to do with slope. Option D is false: lines have different slopes depending on their direction; only a 45-degree line has slope 1. Compute slope as change in y divided by change in x; here that is 3/5 = 0.6, and since it is 1 or less, you would step along x when drawing.
Think first
Why must drawing a line be an approximation at all?
Why can a computer not just draw the exact line? What forces the pixel approximation? Then tap.
Show the answer
Because a raster screen can only turn on WHOLE pixels at fixed grid positions, while a true mathematical line is a continuous path that almost always passes BETWEEN those grid points, so the exact line simply cannot be displayed, only approximated by the nearest pixels. Think about the line from (0,0) to (5,3). Its true points include places like (1, 0.6) and (2, 1.2), the y-values are fractions. But there is no such thing as lighting up 'pixel (1, 0.6)'; pixels exist only at integer coordinates like (1,0) or (1,1). So the computer must DECIDE, for each column, which integer-row pixel best stands in for the ideal line at that point, here, is (1, 0.6) better represented by pixel (1,0) or (1,1)? Choosing (1,1) because 0.6 rounds to 1. Doing this for every step produces a staircase of pixels that our eyes read as a line, but it is fundamentally an approximation of the continuous ideal onto a discrete grid, a process called rasterisation. This is also why diagonal lines can look slightly jagged (aliasing): the grid cannot perfectly represent a smooth slope. The whole art of line-drawing algorithms (DDA, Bresenham) is to make this approximation FAST and as visually faithful as possible, picking the best pixels efficiently. The discreteness of the pixel grid is the root cause: continuous geometry must be sampled onto whole pixels, so drawing is always choosing the closest ones. Continuous line, discrete grid, hence approximation.
Summary
Key takeaways
- A true line is continuous, but a raster screen is a grid of discrete pixels, so drawing a line means approximating it with the closest pixels (rasterisation).
- The slope of a line from (x1,y1) to (x2,y2) is m = (y2-y1)/(x2-x1) = dy/dx.
- Example: (0,0) to (5,3) has slope 3/5 = 0.6 (rise 3 over run 5); the line equation is y = m*x + c.
- Step along the faster-changing axis: if |m| <= 1 step along x, if |m| > 1 step along y, to avoid gaps.
- The ideal line passes between pixels, so the computer must choose the nearest whole pixel at each step.
- This approximation is why diagonal lines can look jagged, and why line-drawing algorithms exist.
- Memory hook: slope is rise over run; the pixel grid forces you to pick the closest pixels, stepping along the dominant axis.