Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- --
- -- Moon Braille Bicycle --
- -- use your imagination --
- -- v 0.0.2 --
- -- ** ** ** --
- -- Totoro (c) 2016 --
- -- computercraft.ru --
- -- --
- unicode = require('unicode')
- sign = (x) -> if x < 0 then -1 elseif x > 0 then 1 else 0
- round = (num, idp) ->
- mult = 10 ^ (idp or 0)
- math.floor(num * mult + 0.5) / mult
- unit = (a, b, c, d, e, f, g, h) ->
- unicode.char(10240 + 128*h + 64*d + 32*g + 16*f + 8*e + 4*c + 2*b + a)
- class Matrix
- matrix: {}
- new: (width, height) =>
- @width = width
- @height = height
- set: (x, y, value = 1) =>
- dx, dy = x - 1, y - 1
- if (dx >= 0 and dx < @width) and (dy >= 0 and dy < @height)
- @matrix[@width * dy + dx] = value
- get: (x, y) =>
- dx, dy = x - 1, y - 1
- if (dx >= 0 and dx < @width) and (dy >= 0 and dy < @height)
- (@matrix[@width * dy + dx] or 0)
- else
- 0
- clear: => @matrix = {}
- line: (x1, y1, x2, y2, value = 1) =>
- dx, dy = math.abs(x1 - x2), math.abs(y1 - y2)
- if dx > 0 or dy > 0
- if dx > dy
- y = y1
- for x = x1, x2, sign(x2 - x1)
- @set(x, round(y), value)
- y += (dy / dx) * sign(y2 - y1)
- else
- x = x1
- for y = y1, y2, sign(y2 - y1)
- @set(round(x), y, value)
- x += (dx / dy) * sign(x2 - x1)
- render: (gpu, x, y) =>
- sy = 0
- for dy = 1, @height, 4
- line = ''
- for dx = 1, @width, 2
- line ..= unit @get(dx, dy), @get(dx, dy+1), @get(dx, dy+2), @get(dx, dy+3),
- @get(dx+1, dy), @get(dx+1, dy+1), @get(dx+1, dy+2), @get(dx+1, dy+3)
- gpu.set(x, y + sy, line)
- sy += 1
- return { :Matrix }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement