Advertisement
MoonlightOwl

Braille Bicycle

Feb 17th, 2016
730
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.72 KB | None | 0 0
  1. --
  2. --        Braille  Bicycle        --
  3. --      use your imagination      --
  4. --            ** ** **            --
  5. --        Totoro  (c) 2016        --
  6. --        computercraft.ru        --
  7.  
  8. local unicode = require('unicode')
  9. local gpu = require("component").gpu
  10. local braille = {}
  11.  
  12. function sign(x)
  13.    if x<0 then
  14.      return -1
  15.    elseif x>0 then
  16.      return 1
  17.    else
  18.      return 0
  19.    end
  20. end
  21. function round(num, idp)
  22.   local mult = 10^(idp or 0)
  23.   return math.floor(num * mult + 0.5) / mult
  24. end
  25.  
  26. braille.matrix = function(width, height)
  27.     local matrix = {}
  28.     matrix.width = width
  29.     matrix.height = height
  30.     return matrix
  31. end
  32.  
  33. braille.set = function(matrix, sx, sy, value)
  34.     local x, y, v = sx-1, sy-1, value or 1
  35.     if (x >= 0 and x < matrix.width) and (y >= 0 and y < matrix.height) then
  36.         matrix[matrix.width*y + x] = value
  37.     end
  38. end
  39.  
  40. braille.get = function(matrix, sx, sy)
  41.     local x, y = sx-1, sy-1
  42.     if (x >= 0 and x < matrix.width) and (y >= 0 and y < matrix.height) then
  43.         return (matrix[matrix.width*y + x] or 0)
  44.     else
  45.         return 0
  46.     end
  47. end
  48.  
  49. braille.clear = function(matrix)
  50.     for i = 1, matrix.width * matrix.height do matrix[i] = nil end
  51. end
  52.  
  53. braille.line = function(matrix, x1, y1, x2, y2, value)
  54.     local dx, dy = math.abs(x1-x2), math.abs(y1-y2)
  55.     local v = value or 1
  56.     if dx > 0 or dy > 0 then
  57.         if dx > dy then
  58.             local y = y1
  59.             for x = x1, x2, sign(x2-x1) do
  60.                 braille.set(matrix, x, round(y), v)
  61.                 y = y + (dy / dx) * sign(y2-y1)
  62.             end
  63.         else
  64.             local x = x1
  65.             for y = y1, y2, sign(y2-y1) do
  66.                 braille.set(matrix, round(x), y, v)
  67.                 x = x + (dx / dy) * sign(x2-x1)
  68.             end
  69.         end
  70.     end
  71. end
  72.  
  73. braille.render = function(matrix, sx, sy)
  74.     local y = 0
  75.     local line
  76.     for dy = 1, matrix.height, 4 do
  77.         line = ''
  78.         for dx = 1, matrix.width, 2 do
  79.             line = line ..
  80.                 braille.unit(braille.get(matrix, dx, dy),
  81.                              braille.get(matrix, dx, dy+1),
  82.                              braille.get(matrix, dx, dy+2),
  83.                              braille.get(matrix, dx, dy+3),
  84.                              braille.get(matrix, dx+1, dy),
  85.                              braille.get(matrix, dx+1, dy+1),
  86.                              braille.get(matrix, dx+1, dy+2),
  87.                              braille.get(matrix, dx+1, dy+3))
  88.         end
  89.         gpu.set(sx, sy + y, line)
  90.         y = y + 1
  91.     end
  92. end
  93.  
  94. braille.unit = function(a, b, c, d, e, f, g, h)
  95.     return unicode.char(10240 + 128*h + 64*d + 32*g + 16*f + 8*e + 4*c + 2*b + a);
  96. end
  97.  
  98. return braille
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement