Task

Using the data storage type defined on the Bitmap page for raster graphics images,

draw a line given two points with Bresenham's line algorithm.




# doesn't handle vertical lines
def (line x0 y0 x1 y1)
  let steep ((> abs) y1-y0 x1-x0)
    when steep
      swap! x0 y0
      swap! x1 y1
    when (x0 > x1)
      swap! x0 x1
      swap! y0 y1
    withs (deltax x1-x0
           deltay (abs y1-y0)
           error deltax/2
           ystep (if (y0 < y1) 1 -1)
           y y0)
      for x x0 (x <= x1) ++x
        if steep
          plot y x
          plot x y
        error -= deltay
        when (error < 0)
          y += ystep
          error += deltax