Task

Using the data storage type defined on this page for raster images,
write an implementation of the midpoint circle algorithm   (also known as Bresenham's circle algorithm).

(definition on Wikipedia).




T Colour
   Byte r, g, b
 
   F (r, g, b)
      .r = r
      .g = g
      .b = b
 
   F ==(other)
      R .r == other.r & .g == other.g & .b == other.b
 
V black = Colour(0, 0, 0)
V white = Colour(255, 255, 255)
 
T Bitmap
   Int width, height
   Colour background
   [[Colour]] map
 
   F (width = 40, height = 40, background = white)
      assert(width > 0 & height > 0)
      .width = width
      .height = height
      .background = background
      .map = (0 .< height).map(h -> (0 .< @width).map(w -> @@background))
 
   F fillrect(x, y, width, height, colour = black)
      assert(x >= 0 & y >= 0 & width > 0 & height > 0)
      L(h) 0 .< height
         L(w) 0 .< width
            .map[y + h][x + w] = colour
 
   F chardisplay()
      V txt = .map.map(row -> row.map(bit -> (I bit == @@.background {‘ ’} E ‘@’)).join(‘’))
      txt = txt.map(row -> ‘|’row‘|’)
      txt.insert(0, ‘+’(‘-’ * .width)‘+’)
      txt.append(‘+’(‘-’ * .width)‘+’)
      print(reversed(txt).join("\n"))
 
   F set(x, y, colour = black)
      .map[y][x] = colour
 
   F get(x, y)
      R .map[y][x]
 
   F circle(x0, y0, radius, colour = black)
      V f = 1 - radius
      V ddf_x = 1
      V ddf_y = -2 * radius
      V x = 0
      V y = radius
      .set(x0, y0 + radius, colour)
      .set(x0, y0 - radius, colour)
      .set(x0 + radius, y0, colour)
      .set(x0 - radius, y0, colour)
 
      L x < y
         I f >= 0
            y--
            ddf_y += 2
            f += ddf_y
         x++
         ddf_x += 2
         f += ddf_x
         .set(x0 + x, y0 + y, colour)
         .set(x0 - x, y0 + y, colour)
         .set(x0 + x, y0 - y, colour)
         .set(x0 - x, y0 - y, colour)
         .set(x0 + y, y0 + x, colour)
         .set(x0 - y, y0 + x, colour)
         .set(x0 + y, y0 - x, colour)
         .set(x0 - y, y0 - x, colour)
 
V bitmap = Bitmap(25, 25)
bitmap.circle(x0' 12, y0' 12, radius' 12)
bitmap.chardisplay()