Advertisement
FCKJesus

star

Nov 10th, 2020
968
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.21 KB | None | 0 0
  1. local event = require("event")
  2. local unicode = require("unicode")
  3.  
  4. local gpu = require("component").gpu
  5.  
  6. local chars = {
  7.   unicode.char(0x0020),  -- [ ]
  8.   unicode.char(0x00b7),  -- [·]
  9.   unicode.char(0x16eb),  -- [᛫]
  10.   unicode.char(0x22c5),  -- [⋅]
  11.   unicode.char(0xff65),  -- [・]
  12.   unicode.char(0x2022),  -- [•]
  13.   unicode.char(0x25cf)   -- [●]
  14. }
  15.  
  16. local function line(x0, y0, x1, y1)
  17.   local steep = false
  18.   if math.abs(x0 - x1) < math.abs(y0 - y1) then
  19.     x0, y0 = y0, x0
  20.     x1, y1 = y1, x1
  21.     steep = true
  22.   end
  23.   if x0 > x1 then
  24.     x0, x1 = x1, x0
  25.     y0, y1 = y1, y0
  26.   end
  27.   local dx = x1 - x0
  28.   local dy = y1 - y0
  29.   local derr = math.abs(dy) * 2;
  30.   local err = 0;
  31.   local y = y0
  32.   local points = {}
  33.   for x = x0, x1, 1 do
  34.     if steep then
  35.       table.insert(points, {y, x})
  36.     else
  37.       table.insert(points, {x, y})
  38.     end
  39.     err = err + derr
  40.     if err > dx then
  41.       if y1 > y0 then
  42.         y = y + 1
  43.       else
  44.         y = y - 1
  45.       end
  46.       err = err - dx * 2
  47.     end
  48.   end
  49.   return points
  50. end
  51.  
  52. local width, height = gpu.getResolution()
  53. local x, y = math.floor(width / 2), math.floor(height / 2)
  54.  
  55. local stars = {}
  56. local distance = math.ceil(math.max(
  57.   -- top-left
  58.   math.sqrt(x^2 + y^2),
  59.   -- top-right
  60.   math.sqrt((width - x)^2 + y^2),
  61.   -- bottom-left
  62.   math.sqrt(x^2 + (height - y)^2),
  63.   -- bottom-right
  64.   math.sqrt((width -x)^2 + (height - y)^2)
  65. ))
  66.  
  67. local lines = {}
  68. for line = 1, height, 1 do
  69.   lines[line] = {}
  70.   for c = 1, width, 1 do
  71.     table.insert(lines[line], " ")
  72.   end
  73. end
  74.  
  75. while true do
  76.   for n = 1, 10, 1 do
  77.     local angle = math.random(0, 359)
  78.     local x1 = math.floor(x + distance * math.cos(math.rad(angle)))
  79.     local y1 = math.floor(y + distance * math.sin(math.rad(angle)))
  80.     local points = line(x, y, x1, y1)
  81.     if points[1][1] ~= x or points[1][2] ~= y then
  82.       -- reverse the table
  83.       for i = 1, math.floor(#points / 2), 1 do
  84.         points[i], points[#points - i + 1] = points[#points - i + 1], points[i]
  85.       end
  86.     end
  87.     for i = 1, #points, 1 do
  88.       local p = points[i]
  89.       if p[1] < 1 or p[1] > width or p[2] < 1 or p[2] > height then
  90.         for j = i, #points, 1 do
  91.           points[j] = nil
  92.         end
  93.         break
  94.       end
  95.     end
  96.     local star = {
  97.       points = points,
  98.       angle = angle,
  99.       i = 0,
  100.       state = 2
  101.     }
  102.     table.insert(stars, star)
  103.   end
  104.  
  105.   for i = #stars, 1, -1 do
  106.     local star = stars[i]
  107.     star.i = star.i + star.state - 2 + 1
  108.     if not star.points[star.i] then
  109.       table.remove(stars, i)
  110.     else
  111.       local sDist = math.sqrt(
  112.         (x - star.points[star.i][1])^2 + (y - star.points[star.i][2])^2
  113.       )
  114.       star.state = 2 + math.floor(sDist / (distance + 1) * 6)
  115.     end
  116.   end
  117.  
  118.   gpu.fill(1, 1, width, height, " ")
  119.   for _, line in pairs(lines) do
  120.     for k in pairs(line) do
  121.       line[k] = " "
  122.     end
  123.   end
  124.   for _, star in pairs(stars) do
  125.     local sX, sY = table.unpack(star.points[star.i])
  126.     lines[sY][sX] = chars[star.state]
  127.   end
  128.   for n, line in pairs(lines) do
  129.     gpu.set(1, n, table.concat(line))
  130.   end
  131.  
  132.   if event.pull(.05, "interrupted") then
  133.     break
  134.   end
  135. end
  136.  
  137. gpu.fill(1, 1, width, height, " ")
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement