Bresenham's Line Algorithm
Understand Bresenham's Line Algorithm, a highly efficient rasterization technique that determines which grid points should be plotted to approximate a straight line.
Continue with the guided workspace
Use the paired workspace to connect this guide's mental model with live component state, visible signals, and the outcome each step or scenario is meant to prove.
Mental Model
A computer screen is a grid of discrete square pixels, but a mathematical line is continuous. When you ask a graphics system to draw a line from point A to point B, it cannot light up the infinite points along that line β it has to choose a finite set of pixels that best approximate the ideal path. That choice is called rasterization, and getting it wrong produces lines that look jagged, stair-stepped, or wandering.
Bresenhamβs Line Algorithm is the classic solution, and the trick that makes it fast is integer-only math. Starting at one endpoint, it steps along the dominant axis (whichever of X or Y has the larger distance to travel) one pixel at a time. At each step, an integer decision variable tracks the cumulative drift between the ideal line and the pixel path chosen so far. When stays negative, the line is close enough to the dominant axis that only the dominant coordinate advances; when reaches zero or above, the algorithm takes a diagonal step on both axes to correct the drift. Because every update is an addition, a subtraction, or a multiply-by-two (a cheap bit shift), the algorithm runs without any floating-point arithmetic or division β which is why it became the standard rasterizer in early graphics hardware and is still taught as a foundational computer-graphics skill.
Components and Why They Exist
Pixel Grid
The grid is the screen itself β a 16Γ16 array of cells where the algorithm will light up pixels. The demo overlays the ideal mathematical line as a dashed reference on top of the discrete cells, then fills in the chosen pixels as colored blocks as the algorithm evaluates each step. Watching the gap between the dashed line and the colored blocks is the fastest way to see what βbest approximationβ means: the algorithm is always picking the pixel whose center is closest to the true line at that step.
Endpoints (Start and End Points)
The two endpoints define the line segment and everything the algorithm needs to derive: the absolute distances and , the step directions and (each or ), and which axis is dominant. Drag the green start handle or the red end handle in the demo to change the line and watch the algorithm recompute β a shallow line keeps X as the dominant axis, a steep line swaps to Y, and a line going left or down simply flips the step signs without needing a different algorithm.
Decision Variable D
is the heart of the algorithm. It is a scaled error accumulator β not the slope itself β that records how far the pixel path has drifted from the ideal line. Its sign alone makes the decision: if , the drift is small enough that the next pixel advances only on the dominant axis; if , the drift has crossed the midpoint and the next pixel must take a diagonal step on both axes to correct it. Watch the value of in the inspector at each step and you will see it climb, trigger a diagonal step, drop back, and climb again β that oscillation is the algorithm tracking the line.
Pseudocode and Execution Log
The pseudocode panel highlights the exact line the algorithm is executing at each step, and the execution log records the coordinates, the value, and the decision that was made. Following the highlighted line and matching it to the pixel just plotted on the grid is what turns the formula into a skill: you see the cause (the update rule), the decision (step vs. diagonal step), and the effect (which pixel lit up) all in one glance.
Edge Cases and Misconceptions
- Steep Lines (): A common misconception is that the algorithm only works for shallow lines. For steep lines, Y becomes the dominant axis. The algorithm operates identically, but Y increments every step while X is updated conditionally based on .
- Negative Slopes and Directions: Lines moving left () or downward () do not require special algorithms. Bresenhamβs handles them by computing direction sign offsets ( or ) and using the absolute values of and for error calculations.
- D is Not the Slope: The decision variable is not the slope () itself. It is a scaled error accumulator. The slope is constant, but is updated incrementally at each step to reflect the drift.
- Integer-Only is Exact, Not Approximate: The grid display is an approximation because of pixel layout, but the math used by Bresenhamβs algorithm to choose the closest pixel is mathematically exact. It does not introduce rounding errors.
Common Questions
Why does Bresenham's algorithm use the factor of 2 in its error update terms?
The factor of 2 arises from multiplying the slope equation (y = mx + b) by 2 * delta_x to eliminate fractions. This clears the 0.5 threshold used to check if the mathematical line has crossed the midpoint between two pixel centers, allowing the entire comparison to run using only integers.
Why does Bresenham's algorithm track an error term?
The error term records how far the chosen pixel path has drifted from the ideal mathematical line, letting the algorithm decide when the next pixel must also move on the minor axis.
How does the algorithm handle lines in different octants?
Bresenham's algorithm is defined for the first octant (where 0 <= dy <= dx). To handle all octants, it calculates step directions (s_x, s_y = +/- 1) and swaps the dominant and minor axes if the line is steep (dy > dx).
What happens when the decision variable D is exactly zero?
When D = 0, the mathematical line passes exactly halfway between two pixels. The algorithm treats this as a step on both the dominant and minor axes (a diagonal step) to remain consistent, though choosing either pixel is mathematically equidistant.
Why is Bresenham's algorithm so famous?
It avoids expensive floating-point math or division, making it extremely fast to implement in hardware or low-power processors.