Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("API/Menu.lua")
- local function drawCircle(c, r, l)
- local angle = 0
- for i=1, l do
- local x = c.x + r * math.cos(angle)
- local y = c.y + r * math.sin(angle)
- angle = angle + (2*math.pi) / l
- local dx = c.x + r * math.cos(angle)
- local dy = c.y + r * math.sin(angle)
- paintutils.drawLine(x, y, dx, dy, colors.lime)
- end
- angle = 0
- end
- local function clearScreen()
- print("Clear")
- mon = peripheral.wrap("monitor_0")
- mon.setBackgroundColor(colors.black)
- mon.clear()
- end
- local function keepOnScreen(entity, offset, w, h)
- if (entity.pos.y < -offset) then
- entity.pos.y = h+offset
- elseif (entity.pos.y >= h+offset) then
- entity.pos.y = -offset
- end
- if (entity.pos.x < -offset) then
- entity.pos.x = w+offset
- elseif (entity.pos.x >= w+offset) then
- entity.pos.x = -offset
- end
- end
- local function metaBalls()
- print("MetaBalls")
- mon = peripheral.wrap("monitor_0")
- cTerm = term.redirect(mon)
- local w, h = term.getSize()
- local balls = {
- }
- local nBalls = 8
- for i=1, nBalls do
- table.insert(balls,
- {
- vel = {x = math.random(1,4) - 2, y = math.random(1,4) - 2},
- pos = {x = math.random(0,w), y = math.random(0,h)},
- r = math.random(2,4)
- }
- )
- end
- local timer = os.startTimer(0.15)
- term.redirect(cTerm)
- print("Running Monitor - Press Enter to exit!")
- term.redirect(mon)
- while true do
- term.redirect(cTerm)
- event, key = os.pullEventRaw()
- if event == "key" then
- if key == keys.enter then
- return
- end
- end
- if event == "timer" then
- timer = os.startTimer(0.3)
- end
- term.redirect(mon)
- clearScreen()
- for i=1, #balls do
- -- drawCircle(balls[i].pos, balls[i].r, 6)
- balls[i].pos.x = balls[i].pos.x + balls[i].vel.x
- balls[i].pos.y = balls[i].pos.y + balls[i].vel.y
- keepOnScreen(balls[i], balls[i].r, w, h)
- end
- for i=1, w do
- for j=1, h do
- local sum = 0
- for k=1, #balls do
- local ball = balls[k]
- local x_dist = math.pow(i - ball.pos.x, 2)
- local y_dist = math.pow(j - ball.pos.y, 2)
- sum = sum + math.pow(ball.r, 2) / (x_dist + y_dist)
- if sum > 1.0 then
- paintutils.drawPixel(i, j, colors.lime)
- end
- end
- end
- end
- end
- term.redirect(cTerm)
- end
- Menu.setColors()
- Menu.addOptions({
- {name = "Meta Balls", handler = metaBalls},
- {name = "Clear", handler = clearScreen}
- })
- Menu.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement