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.




SUB draw_line(x1, y1, x2, y2, colour)
    x_dist = abs(x2-x1)
    y_dist = abs(y2-y1)
    IF y2-y1 < -x_dist OR x2-x1 <= -y_dist THEN
        SWAP x1, x2       ' Swap start and end points
  SWAP y1, y2
    END IF
    IF x1 < x2 THEN x_step = 1 ELSE x_step = -1
    IF y1 < y2 THEN y_step = 1 ELSE y_step = -1
 
    IF y_dist > x_dist THEN     ' steep angle, step by y
  error = y_dist/2
  x = x1
  FOR y = y1 TO y2
      canvas.Pset(x, y, colour)
      error = error - x_dist
      IF error < 0 THEN
          x = x + x_step
    error = error + y_dist
      END IF
  NEXT y
    ELSE          ' not steep, step by x
        error = x_dist/2
  y = y1
  FOR x = x1 TO x2
      canvas.Pset(x, y, colour)
      error = error - y_dist
      IF error < 0 THEN
          y = y + y_step
    error = error + x_dist
      END IF
  NEXT y
    END IF
 
END SUB