How to resolve the algorithm Galton box animation step by step in the Lua programming language
How to resolve the algorithm Galton box animation step by step in the Lua programming language
Table of Contents
Problem Statement
A Galton device Sir Francis Galton's device is also known as a bean machine, a Galton Board, or a quincunx.
In a Galton box, there are a set of pins arranged in a triangular pattern. A number of balls are dropped so that they fall in line with the top pin, deflecting to the left or the right of the pin. The ball continues to fall to the left or right of lower pins before arriving at one of the collection points between and to the sides of the bottom row of pins. Eventually the balls are collected into bins at the bottom (as shown in the image), the ball column heights in the bins approximate a bell curve. Overlaying Pascal's triangle onto the pins shows the number of different paths that can be taken to get to each bin.
Generate an animated simulation of a Galton device.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Galton box animation step by step in the Lua programming language
Source code in the lua programming language
Bitmap.render = function(self)
for y = 1, self.height do
print(table.concat(self.pixels[y], " "))
end
end
-- globals (tweak here as desired)
math.randomseed(os.time())
local W, H, MIDX = 15, 40, 7
local bitmap = Bitmap(W, H)
local AIR, PIN, BALL, FLOOR = ".", "▲", "☻", "■"
local nballs, balls = 60, {}
local frame, showEveryFrame = 1, false
-- the game board:
bitmap:clear(AIR)
for row = 1, 7 do
for col = 0, row-1 do
bitmap:set(MIDX-row+col*2+1, 1+row*2, PIN)
end
end
for col = 0, W-1 do
bitmap:set(col, H-1, FLOOR)
end
-- ball class
Ball = {
new = function(self, x, y, bitmap)
local instance = setmetatable({ x=x, y=y, bitmap=bitmap, alive=true }, self)
return instance
end,
update = function(self)
if not self.alive then return end
self.bitmap:set(self.x, self.y, AIR)
local newx, newy = self.x, self.y+1
local below = self.bitmap:get(newx, newy)
if below==PIN then
newx = newx + (math.random(2)-1)*2-1
end
local there = self.bitmap:get(newx, newy)
if there==AIR then
self.x, self.y = newx, newy
else
self.alive = false
end
self.bitmap:set(self.x, self.y, BALL)
end,
}
Ball.__index = Ball
setmetatable(Ball, { __call = function (t, ...) return t:new(...) end })
-- simulation:
local function spawn()
if nballs > 0 then
balls[#balls+1] = Ball(MIDX, 0, bitmap)
nballs = nballs - 1
end
end
spawn()
while #balls > 0 do
if frame%2==0 then spawn() end
alive = {}
for _,ball in ipairs(balls) do
ball:update()
if ball.alive then alive[#alive+1]=ball end
end
balls = alive
if frame%50==0 or #alive==0 or showEveryFrame then
print("FRAME "..frame..":")
bitmap:render()
end
frame = frame + 1
end
You may also check:How to resolve the algorithm Number names step by step in the Perl programming language
You may also check:How to resolve the algorithm Copy a string step by step in the NewLISP programming language
You may also check:How to resolve the algorithm Pancake numbers step by step in the Ring programming language
You may also check:How to resolve the algorithm Rosetta Code/Rank languages by popularity step by step in the HicEst programming language
You may also check:How to resolve the algorithm Lah numbers step by step in the Go programming language