The Discrete Line

bresenham_02.png

This is an applet I made to illustrate Jack E. Bresenham’s line algorithm. And here 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

Bresenham later modified his algorithm to produce circles.

Leave a Reply