Advertisement
Norda97

Metaballs

Dec 12th, 2019
607
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. os.loadAPI("API/Menu.lua")
  2.  
  3. local function drawCircle(c, r, l)
  4. local angle = 0
  5.  
  6. for i=1, l do
  7. local x = c.x + r * math.cos(angle)
  8. local y = c.y + r * math.sin(angle)
  9. angle = angle + (2*math.pi) / l
  10. local dx = c.x + r * math.cos(angle)
  11. local dy = c.y + r * math.sin(angle)
  12. paintutils.drawLine(x, y, dx, dy, colors.lime)
  13. end
  14. angle = 0
  15. end
  16.  
  17.  
  18.  
  19. local function clearScreen()
  20. print("Clear")
  21. mon = peripheral.wrap("monitor_0")
  22. mon.setBackgroundColor(colors.black)
  23. mon.clear()
  24. end
  25.  
  26. local function keepOnScreen(entity, offset, w, h)
  27. if (entity.pos.y < -offset) then
  28. entity.pos.y = h+offset
  29. elseif (entity.pos.y >= h+offset) then
  30. entity.pos.y = -offset
  31. end
  32.  
  33. if (entity.pos.x < -offset) then
  34. entity.pos.x = w+offset
  35. elseif (entity.pos.x >= w+offset) then
  36. entity.pos.x = -offset
  37. end
  38. end
  39.  
  40. local function metaBalls()
  41. print("MetaBalls")
  42. mon = peripheral.wrap("monitor_0")
  43. cTerm = term.redirect(mon)
  44.  
  45. local w, h = term.getSize()
  46.  
  47. local balls = {
  48. }
  49.  
  50. local nBalls = 8
  51. for i=1, nBalls do
  52. table.insert(balls,
  53. {
  54. vel = {x = math.random(1,4) - 2, y = math.random(1,4) - 2},
  55. pos = {x = math.random(0,w), y = math.random(0,h)},
  56. r = math.random(2,4)
  57. }
  58. )
  59. end
  60. local timer = os.startTimer(0.15)
  61.  
  62. term.redirect(cTerm)
  63. print("Running Monitor - Press Enter to exit!")
  64. term.redirect(mon)
  65.  
  66. while true do
  67. term.redirect(cTerm)
  68. event, key = os.pullEventRaw()
  69. if event == "key" then
  70. if key == keys.enter then
  71. return
  72. end
  73. end
  74.  
  75. if event == "timer" then
  76. timer = os.startTimer(0.3)
  77. end
  78. term.redirect(mon)
  79. clearScreen()
  80. for i=1, #balls do
  81.  
  82. -- drawCircle(balls[i].pos, balls[i].r, 6)
  83. balls[i].pos.x = balls[i].pos.x + balls[i].vel.x
  84. balls[i].pos.y = balls[i].pos.y + balls[i].vel.y
  85. keepOnScreen(balls[i], balls[i].r, w, h)
  86. end
  87.  
  88. for i=1, w do
  89. for j=1, h do
  90. local sum = 0
  91. for k=1, #balls do
  92. local ball = balls[k]
  93. local x_dist = math.pow(i - ball.pos.x, 2)
  94. local y_dist = math.pow(j - ball.pos.y, 2)
  95. sum = sum + math.pow(ball.r, 2) / (x_dist + y_dist)
  96. if sum > 1.0 then
  97. paintutils.drawPixel(i, j, colors.lime)
  98. end
  99. end
  100. end
  101. end
  102. end
  103.  
  104. term.redirect(cTerm)
  105. end
  106.  
  107. Menu.setColors()
  108. Menu.addOptions({
  109. {name = "Meta Balls", handler = metaBalls},
  110. {name = "Clear", handler = clearScreen}
  111. })
  112. Menu.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement