Advertisement
MoonlightOwl

Moon Braille Bicycle

Jun 8th, 2016
537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.79 KB | None | 0 0
  1. --                                --
  2. --     Moon  Braille  Bicycle     --
  3. --      use your imagination      --
  4. --            v  0.0.2            --
  5. --            ** ** **            --
  6. --        Totoro  (c) 2016        --
  7. --        computercraft.ru        --
  8. --                                --
  9.  
  10. unicode = require('unicode')
  11.  
  12. sign = (x) -> if x < 0 then -1 elseif x > 0 then 1 else 0
  13. round = (num, idp) ->
  14.   mult = 10 ^ (idp or 0)
  15.   math.floor(num * mult + 0.5) / mult
  16. unit = (a, b, c, d, e, f, g, h) ->
  17.   unicode.char(10240 + 128*h + 64*d + 32*g + 16*f + 8*e + 4*c + 2*b + a)
  18.  
  19. class Matrix
  20.   matrix: {}
  21.  
  22.   new: (width, height) =>
  23.     @width = width
  24.     @height = height
  25.  
  26.   set: (x, y, value = 1) =>
  27.     dx, dy = x - 1, y - 1
  28.     if (dx >= 0 and dx < @width) and (dy >= 0 and dy < @height)
  29.       @matrix[@width * dy + dx] = value
  30.  
  31.   get: (x, y) =>
  32.     dx, dy = x - 1, y - 1
  33.     if (dx >= 0 and dx < @width) and (dy >= 0 and dy < @height)
  34.       (@matrix[@width * dy + dx] or 0)
  35.     else
  36.       0
  37.  
  38.   clear: => @matrix = {}
  39.  
  40.   line: (x1, y1, x2, y2, value = 1) =>
  41.     dx, dy = math.abs(x1 - x2), math.abs(y1 - y2)
  42.     if dx > 0 or dy > 0
  43.       if dx > dy
  44.         y = y1
  45.         for x = x1, x2, sign(x2 - x1)
  46.           @set(x, round(y), value)
  47.           y += (dy / dx) * sign(y2 - y1)
  48.       else
  49.         x = x1
  50.         for y = y1, y2, sign(y2 - y1)
  51.           @set(round(x), y, value)
  52.           x += (dx / dy) * sign(x2 - x1)
  53.  
  54.   render: (gpu, x, y) =>
  55.     sy = 0
  56.     for dy = 1, @height, 4
  57.       line = ''
  58.       for dx = 1, @width, 2
  59.         line ..= unit @get(dx, dy), @get(dx, dy+1), @get(dx, dy+2), @get(dx, dy+3),
  60.                       @get(dx+1, dy), @get(dx+1, dy+1), @get(dx+1, dy+2), @get(dx+1, dy+3)
  61.       gpu.set(x, y + sy, line)
  62.       sy += 1
  63.  
  64. return { :Matrix }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement