Created
October 11, 2019 14:16
-
-
Save Datseris/6874b2a60249499a223b35912cf6bccb to your computer and use it in GitHub Desktop.
WIP code to generate the logo of Agents.jl
This file contains 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
using Luxor, DSP; | |
cd(@__DIR__); | |
const AGENTSTEXT = "Agents.jl" | |
# const FONT = "Montserrat SemiBold" | |
const FONT = "Tamil MN Bold" | |
const FONTSIZE = 200 | |
using Random | |
Random.seed!(9) | |
# dimensions drawing | |
const W = 1000 | |
const H = 1000 | |
const GRIDSQUARESIZE = 10 | |
function randomjuliacolor() | |
c=[Luxor.julia_green, Luxor.julia_red,Luxor.julia_purple, Luxor.julia_blue] | |
c[rand(1:4)] | |
end | |
function drawcellulargrid(a) | |
rows, cols = size(a) | |
t = Tiler(W, H, rows, cols, margin=0) | |
for (pos, n) in t | |
if a[n] | |
sethue(randomjuliacolor()) | |
box(pos, t.tilewidth - 1, t.tilewidth - 1, :fill) | |
end | |
end | |
end | |
function update!(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 makegriddedtext(str, fontsz, rows, cols, width, height) | |
Drawing(width, height, "temp.png") | |
origin() | |
fontsize(fontsz) | |
fontface(FONT) | |
textoutlines(str, O, :path, halign=:center, valign=:middle) | |
thetextpaths = pathtopoly() | |
a = zeros(Bool, rows, cols) | |
t = Tiler(width, height, rows, cols, margin=0) | |
holes = (2, 4, 5, 7) | |
for (pos, n) in t | |
for (i, p) in enumerate(thetextpaths) | |
if isinside(pos, p; allowonedge=true) && i ∉ holes # dat "A"'s got a hole :( | |
a[n] = 1 | |
continue | |
elseif isinside(pos, p;allowonedge=true) && i ∈ holes | |
a[n] = 0 | |
end | |
end | |
end | |
finish() | |
return a | |
end | |
function frame(GoL, a) | |
Mfilt = [true true true; true false true; true true true] | |
squircle(O, W/2, H/6, :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 singleframe() | |
GoL = rand(Bool, W ÷ GRIDSQUARESIZE, H ÷ GRIDSQUARESIZE) | |
rows, cols = size(GoL) | |
a = makegriddedtext(AGENTSTEXT, FONTSIZE, rows, cols, W, H) | |
Drawing(W, H, "agentsicon.png") | |
origin() | |
frame(GoL, a) | |
finish() | |
preview() | |
end | |
singleframe() | |
function makeanimation(N = 50; framerate = 2) | |
life = Movie(W, H, "life") | |
GoL = rand(Bool, W ÷ GRIDSQUARESIZE, H ÷ GRIDSQUARESIZE) | |
# create the outlines of the big text | |
rows, cols = size(GoL) | |
a = makegriddedtext(AGENTSTEXT, FONTSIZE, rows, cols, W, H) | |
# single frame of animation | |
function animframe(scene, framenumber) | |
GoL, a = scene.opts | |
frame(GoL, a) | |
end | |
animate(life, [ | |
Scene(life, animframe, optarg=(GoL, a), 1:N), | |
], | |
framerate=framerate, | |
creategif=true, | |
pathname="agents_animated.gif") | |
end | |
makeanimation(100; framerate = 2) |
cormullion
commented
Oct 11, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment