go back
Illustration of Bresenham's line algorithm from 1962.
Click and drag the mouse to trace a line.
Bresenham's algorithm will appear as you release the mouse.
spacebar clears the canvas.

To view this content, you need to install Java from java.com

Built with Processing


This is the Pseudocode as extracted from Wikipedia:

function line(x0, x1, y0, y1)
    boolean steep := abs(y1 - y0) > abs(x1 - x0)
    if steep then
        swap(x0, y0)
        swap(x1, y1)
    if x0 > x1 then
        swap(x0, x1)
        swap(y0, y1)
    int deltax := x1 - x0
    int deltay := abs(y1 - y0)
    int error := 0
    int ystep
    int y := y0
    if y0 < y1 then ystep := 1 else ystep := -1
    for x from x0 to x1
        if steep then plot(y,x) else plot(x,y)
    error := error + deltay
    if 2*error >= deltax
        y := y + ystep
        error := error - deltax