Short answer: Bresenham’s line algorithm chooses the pixel sequence from (x0, y0) to (x1, y1) using only integer additions. It walks the major axis one pixel at a time and keeps an error term that decides when the minor axis steps. Starting from zero and adding twice the minor delta, then subtracting twice the major delta when the error crosses zero, stays in integers. The line is the one a raster display should draw. It is not a sub-pixel geometric intersection test.
What the problem actually asks
The real segment crosses a row or column of pixels, and the pixel closest to the ideal line is the one to light. Floating-point interpolation computes that pixel and spends a division per step. Bresenham folds the comparison into an integer error that has the sign of the distance to the boundary between two candidate pixels.
The major axis is the one with the larger absolute delta, so the walk takes one step on that axis every iteration and zero or one step on the other. Swapping the axes when |dy| > |dx| is required. A version written only for shallow slopes draws a broken line on a steep segment.
What the algorithm guarantees
For integer endpoints the algorithm emits a connected 8-connected sequence from the start to the end, inclusive, and it is symmetric if the error rule is symmetric. Time is proportional to the longer delta. Extra memory is a few integers. The decision does not use multiplication after the deltas are doubled, and many implementations keep the doubled constants outside the loop.
The raster line is not the same object as a geometric segment in Bentley-Ottmann. Two raster lines can share a pixel without the real segments intersecting, and the real segments can intersect in a pixel neither line lists if the thickness is zero and the crossing is off the chosen pixels. Use the raster algorithm for pixels and the geometric algorithm for geometry.
The working steps
- Let dx = x1−x0 and dy = y1−y0. Record the signs sx and sy as −1 or +1, and replace dx and dy by their absolute values.
- If dy > dx, the major axis is y. Use the same error logic with the axes swapped, or loop on y.
- Set err = 2·dy − dx for a shallow line that steps in x. At each x step, emit the pixel, add 2·dy to err, and when err is positive subtract 2·dx and step y by sy.
- Include both endpoints. The loop runs dx times after the first pixel, or an equivalent inclusive count.
- For a steep line, exchange the roles of x and y in the error update.
The integer error is for a single-pixel line between integer endpoints. Wide lines, anti-aliasing, and sub-pixel endpoints are different algorithms. Wu’s algorithm anti-aliases. A geometric clipping test such as Cohen-Sutherland decides whether the real segment meets a rectangle and does not choose pixels.
Approaches that look simpler and fail
| Approach | Why it fails when a steep line uses the shallow error update |
|---|---|
| Always step x and sometimes step y, even when the line is steeper than 45 degrees | The pixels then jump in y by more than one and the line has holes. The major axis has to be the longer delta. |
| Use a floating slope and round | The picture can match and the algorithm is no longer Bresenham. The integer error is the algorithm. |
| Treat a shared raster pixel as a geometric intersection | Pixel choice is a rounding of the line. Intersection of the underlying segments is an orientation test. |
| Stop before the final endpoint | An off-by-one in the iteration count leaves the last pixel dark. The inclusive convention has to be stated and tested on a one-pixel segment. |
Anti-aliased lines are Wu’s algorithm. Rectangle clipping of the geometric segment is Cohen-Sutherland. Bresenham is the integer raster line.
A working example
This Python fragment walks the major axis one pixel at a time and keeps an integer error term. From (0, 0) to (4, 1) the cells are (0, 0) (1, 0) (2, 0) (3, 1) (4, 1).
def bresenham(x0, y0, x1, y1):
steep = abs(y1 - y0) > abs(x1 - x0)
if steep:
x0, y0, x1, y1 = y0, x0, y1, x1
if x0 > x1:
x0, x1, y0, y1 = x1, x0, y1, y0
dx, dy = x1 - x0, abs(y1 - y0)
err, y, ystep = dx // 2, y0, 1 if y0 < y1 else -1
cells = []
for x in range(x0, x1 + 1):
cells.append((y, x) if steep else (x, y))
err -= dy
if err < 0:
y += ystep
err += dx
return cells
cells = bresenham(0, 0, 4, 1)
assert cells[0] == (0, 0) and cells[-1] == (4, 1)
print(*cells)
Where it is used
Bresenham’s line algorithm is the integer stepper in raster graphics, plotters, and embedded framebuffers. Each step picks the pixel closer to the true line without a division. Antialiased and GPU lines use a different coverage test.
When you can drop this algorithm
Drop a per-step division when drawing many integer-endpoint lines. Use the error term, and swap axes when the slope is steep.
Why this stays published
We publish this so a search for Bresenham’s algorithm returns the integer error term and the major-axis swap. Bresenham’s algorithm rasters a line with integer additions by stepping the major axis every time and stepping the minor axis when an error term crosses zero.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
