local abs,atan,cos,floor,pi,sin,sqrt = math.abs,math.atan,math.cos,math.floor,math.pi,math.sin,math.sqrt
local bitmap = {
  init = function(self, w, h, value)
    self.w, self.h, self.pixels = w, h, {}
    for y=1,h do self.pixels[y]={} end
    self:clear(value)
  end,
  clear = function(self, value)
    for y=1,self.h do
      for x=1,self.w do
        self.pixels[y][x] = value or "  "
      end
    end
  end,
  set = function(self, x, y, value)
    x,y = floor(x),floor(y)
    if x>0 and y>0 and x<=self.w and y<=self.h then
      self.pixels[y][x] = value or "#"
    end
  end,
  line = function(self, x1, y1, x2, y2, c)
    x1,y1,x2,y2 = floor(x1),floor(y1),floor(x2),floor(y2)
    local dx, sx = abs(x2-x1), x1<x2 and 1 or -1
    local dy, sy = abs(y2-y1), y1<y2 and 1 or -1
    local err = floor((dx>dy and dx or -dy)/2)
    while(true) do
      self:set(x1, y1, c)
      if (x1==x2 and y1==y2) then break end
      if (err > -dx) then
        err, x1 = err-dy, x1+sx
        if (x1==x2 and y1==y2) then
          self:set(x1, y1, c)
          break
        end
      end
      if (err < dy) then
        err, y1 = err+dx, y1+sy
      end
    end
  end,  
  render = function(self)
    for y=1,self.h do
      print(table.concat(self.pixels[y]))
    end
  end,
}
screen = {
  clear = function()
    os.execute("cls")