Created
May 28, 2021 18:19
-
-
Save catethos/9956ce7c58954e7aae8d58679c1741c8 to your computer and use it in GitHub Desktop.
galton
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### A Pluto.jl notebook ### | |
# v0.14.7 | |
using Markdown | |
using InteractiveUtils | |
# This Pluto notebook uses @bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of @bind gives bound variables a default value (instead of an error). | |
macro bind(def, element) | |
quote | |
local el = $(esc(element)) | |
global $(esc(def)) = Core.applicable(Base.get, el) ? Base.get(el) : missing | |
el | |
end | |
end | |
# ╔═╡ 0aa2f354-05b2-498e-a490-b69f6b343f5f | |
begin | |
using ColorTypes | |
using Plots | |
using PlutoUI | |
import Random | |
end | |
# ╔═╡ b42f1930-bf80-11eb-0d1f-8b64e8d05f4b | |
struct Ball | |
x:: Int32 | |
y:: Int32 | |
t:: Int32 | |
color:: RGB | |
end | |
# ╔═╡ c400eb2a-68a9-4a04-aaed-ca75d60284a6 | |
struct World | |
balls :: Array{Ball} | |
counter :: Dict{Int32, Int32} | |
end | |
# ╔═╡ 3ec54d37-d812-4dd7-aa98-74edf0f84c2a | |
w = World( | |
[Ball(0,0, i-499,RGB(rand(), rand(), rand())) for i in 1:500], | |
Dict() | |
) | |
# ╔═╡ acb8e499-c5ed-4d5b-9ddb-1542718fe14b | |
function evolve(w::World) | |
balls = w.balls | |
counter = w.counter | |
balls′ = [] | |
counter′ = copy(counter) | |
for b in balls | |
b′ = evolve(b) | |
if b′.t == 12 | |
x′ = b′.x | |
if haskey(counter, x′) | |
counter′[x′] = counter[x′] + 1 | |
else | |
counter′[x′] = 1 | |
end | |
end | |
push!(balls′, b′) | |
end | |
World(balls′, counter′) | |
end | |
# ╔═╡ 7a030492-644b-4b30-a14c-55de52b3f0b6 | |
function evolve(b::Ball) | |
if b.t <= 0 || b.t >= 12 | |
Ball(b.x, b.y, b.t + 1, b.color) | |
else | |
Ball(b.x + rand((-1,1)), b.y - 1, b.t + 1, b.color) | |
end | |
end | |
# ╔═╡ a2939f6d-263b-4b6e-9a98-626bef18b34a | |
function paint(b::Ball) | |
if b.t > 0 | |
Plots.plot!([b.x], [b.y], seriestype=:scatter, legend=false, alpha=0.5, color=b.color) | |
end | |
end | |
# ╔═╡ 1c600ecc-daba-4f14-8173-6d2de60f366c | |
function paint(d:: Dict{Int32, Int32}) | |
xs = [x for x in keys(d)] | |
ys = [x for x in values(d)] | |
bar(xs, ys, xlims=(-15,15), ylims=(0,200), legend=false, border=:none) | |
end | |
# ╔═╡ d692114c-4c75-486c-978b-2307b8707eed | |
function paint(w::World) | |
p = plot(seriestype=:scatter, legend=false, alpha=0.5, border=:none) | |
xlims!(-15,15) | |
ylims!(-13,2) | |
for b in w.balls | |
paint(b) | |
end | |
q = paint(w.counter) | |
plot(p,q, layout=(2,1)) | |
end | |
# ╔═╡ 2aa8a888-c3f2-4064-9de0-a5633cab93cb | |
begin | |
ws = [w] | |
for _ in 1:1000 | |
push!(ws, evolve(ws[end])) | |
end | |
end | |
# ╔═╡ dad0cc60-69ad-4864-8f34-74e4df544e5c | |
@bind i Slider(1:511) | |
# ╔═╡ 1c2d18d1-81cc-486b-96b1-d224bd1a0a90 | |
i | |
# ╔═╡ 646cada1-a161-43e7-a8ab-f26fa4b5df4e | |
paint(ws[i]) | |
# ╔═╡ 15c4170e-3136-4919-b677-206e15060698 | |
anim = @animate for i ∈ 1:511 | |
paint(ws[i]) | |
end | |
# ╔═╡ c4de7e1e-8934-4b17-bd53-ba22435a9221 | |
gif(anim, "/home/catethos/Videos/anim_fps15.gif", fps = 15) | |
# ╔═╡ Cell order: | |
# ╠═0aa2f354-05b2-498e-a490-b69f6b343f5f | |
# ╠═b42f1930-bf80-11eb-0d1f-8b64e8d05f4b | |
# ╠═c400eb2a-68a9-4a04-aaed-ca75d60284a6 | |
# ╠═3ec54d37-d812-4dd7-aa98-74edf0f84c2a | |
# ╠═acb8e499-c5ed-4d5b-9ddb-1542718fe14b | |
# ╠═7a030492-644b-4b30-a14c-55de52b3f0b6 | |
# ╠═a2939f6d-263b-4b6e-9a98-626bef18b34a | |
# ╠═1c600ecc-daba-4f14-8173-6d2de60f366c | |
# ╠═d692114c-4c75-486c-978b-2307b8707eed | |
# ╠═2aa8a888-c3f2-4064-9de0-a5633cab93cb | |
# ╠═1c2d18d1-81cc-486b-96b1-d224bd1a0a90 | |
# ╠═dad0cc60-69ad-4864-8f34-74e4df544e5c | |
# ╠═646cada1-a161-43e7-a8ab-f26fa4b5df4e | |
# ╠═15c4170e-3136-4919-b677-206e15060698 | |
# ╠═c4de7e1e-8934-4b17-bd53-ba22435a9221 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment