Anonymous user
Galton box animation: Difference between revisions
m
→{{header|Julia}}
(→{{header|REXX}}: added a prompt so the user knows what to do when the simulation is frozen on a specific step.) |
|||
Line 1,699:
=={{header|Julia}}==
This is a proof of concept code. It does not use
6 pins in 6 rows are hard coded. The next ball is released at the press of the Enter key. Keep it depressed for continuous running.
The balls are randomly deflected left or right at hitting the pins, and they fall to the bins at the bottom, which extend downwards.
The timer function sets the speed of the animation. Change the "interval" parameter to larger values for slower movement.
Pressing x then Enter exits, other keys are ignored.
{{works with|Julia|1.0}}
Line 1,710:
function drawball(timer)
global r, c, d
print("\e[$r;$(c)H ") # clear last ball position (r,c)
if (r
close(timer)
b = (bin[(c+2)>>2] += 1)# update count in bin
print("\e[$b;$(c)Ho") # lengthen bar of balls in bin
else
r in 3:2:13
print("\e[$r;$(c+=d)Ho")# show ball moving in
end
end
Line 1,728:
bin = fill(15,7) # positions of top of bins
while "x" != readline() >= "" # x-Enter: exit, {keys..}Enter: next ball
global r,c,d = 0,14,0
t = Timer(drawball,
while r < 15 sleep(0.01) end
print("\e[40;1H") # move cursor far down
|