Skip to content

Instantly share code, notes, and snippets.

@cormullion
Created October 9, 2019 16:18
Show Gist options
  • Select an option

  • Save cormullion/915d4c1e9047aab7cd4ccc8f28b3c0a6 to your computer and use it in GitHub Desktop.

Select an option

Save cormullion/915d4c1e9047aab7cd4ccc8f28b3c0a6 to your computer and use it in GitHub Desktop.
agents
using Luxor, DSP
# dimensions drawing
const W = 500
const H = 500
const GRIDSQUARESIZE = 10
function drawcellulargrid(a)
rows, cols = size(a)
t = Tiler(W, H, rows, cols, margin=0)
for (pos, n) in t
if a[n]
sethue([Luxor.julia_green, Luxor.julia_red,Luxor.julia_purple, Luxor.julia_blue][rand(1:end)])
box(pos, t.tilewidth - 1, t.tilewidth - 1, :fill)
end
end
end
function nextgeneration!(GoL, m)
convGoL = conv(GoL, m)
lives2 = convGoL .== 2
lives3 = convGoL .== 3
twoLiveNeig = GoL .& lives2[2:end-1, 2:end-1]
threeLiveNeig = GoL .& lives3[2:end-1, 2:end-1]
reproduce = .~(GoL) .& lives3[2:end-1, 2:end-1]
GoL[:] = twoLiveNeig .| threeLiveNeig .| reproduce
end
function update(GoL, Mfilt)
nextgeneration!(GoL, Mfilt)
end
function makegriddedtext(str, fontsz, rows, cols, width, height)
Drawing(width, height, "/tmp/temp.png")
origin()
fontsize(fontsz)
fontface("AvenirNext-Bold")
textoutlines(str, O, :path, halign=:center, valign=:middle)
thetextpaths = pathtopoly()
a = zeros(Bool, rows, cols)
t = Tiler(width, height, rows, cols, margin=0)
for (pos, n) in t
for (i, p) in enumerate(thetextpaths)
if isinside(pos, p) && i != 2 # dat "A"'s got a hole :(
a[n] = 1
continue
elseif isinside(pos, p) && i == 2
a[n] = 0
end
end
end
finish()
return a
end
# single frame of animation
function frame(scene, framenumber)
GoL, a = scene.opts
Mfilt = [true true true; true false true; true true true]
squircle(O, W/2, H/2, :clip, rt=0.2)
@layer begin
setline(0.5)
backgroundcolor = "grey10"
background(backgroundcolor)
rows, cols = size(GoL)
update(GoL, Mfilt)
drawcellulargrid(GoL)
t = Tiler(W, H, rows, cols, margin=0)
for (pos, n) in t
if a[n] == 1
sethue("orange")
box(pos, GRIDSQUARESIZE - 2, GRIDSQUARESIZE - 2, :fill)
sethue("white")
box(pos, GRIDSQUARESIZE - 2, GRIDSQUARESIZE - 2, :stroke)
end
end
end
end
function makeanimation()
life = Movie(W, H, "life")
GoL = rand(Bool, W ÷ GRIDSQUARESIZE, H ÷ GRIDSQUARESIZE)
# GoL = makegriddedtext("AGENTS", 120, W ÷ GRIDSQUARESIZE, H ÷ GRIDSQUARESIZE, W, H)
# create the outlines of the big text
rows, cols = size(GoL)
a = makegriddedtext("AGENTS", 110, rows, cols, W, H)
animate(life, [
Scene(life, frame, optarg=(GoL, a), 1:50),
],
framerate=2,
creategif=true,
pathname="/tmp/life.gif")
end
# makeanimation()
function singleframe()
GoL = rand(Bool, W ÷ GRIDSQUARESIZE, H ÷ GRIDSQUARESIZE)
rows, cols = size(GoL)
a = makegriddedtext("AGENTS", 110, rows, cols, W, H)
Mfilt = [true true true; true false true; true true true]
Drawing(W, H, "/tmp/agentsicon.svg")
origin()
squircle(O, W/2, H/2, :clip, rt=0.2)
@layer begin
setline(0.5)
backgroundcolor = "grey10"
sethue(backgroundcolor)
paint()
rows, cols = size(GoL)
update(GoL, Mfilt)
drawcellulargrid(GoL)
t = Tiler(W, H, rows, cols, margin=0)
for (pos, n) in t
if a[n] == 1
sethue("orange")
box(pos, GRIDSQUARESIZE - 2, GRIDSQUARESIZE - 2, :fill)
end
end
end
finish()
preview()
end
singleframe()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment