Skip to content

Instantly share code, notes, and snippets.

@cormullion
Last active October 19, 2021 14:35
Show Gist options
  • Save cormullion/9c26aeeb86471ba1a37fa0f06f5c8972 to your computer and use it in GitHub Desktop.
Save cormullion/9c26aeeb86471ba1a37fa0f06f5c8972 to your computer and use it in GitHub Desktop.
little graph network that unfortunately looks a bit like a chemical molecule
using Luxor, LightGraphs, Random, Colors
function lighten(col::Colorant, f)
c = convert(RGB, col)
return RGB(f * c.r, f* c.g, f * c.b)
end
function julia_sphere(pt, w;
col = 1,
action = :none,
colors = [Luxor.julia_red, Luxor.julia_green, Luxor.julia_purple, Luxor.julia_blue])
setmesh(mesh(
makebezierpath(box(pt, w * 1.5, w * 1.5)),
[
lighten(RGB(colors[col]...), .5),
lighten(RGB(colors[col]...), 1.75),
lighten(RGB(colors[col]...), 1.25),
lighten(RGB(colors[col]...), .6),
]))
circle(pt, w, action)
end
function main()
Drawing(600, 600, "/tmp/julia-graphs.png")
origin()
translate(0, 10)
squircle(O, 298, 298, :clip, rt=0.2)
background("black")
# make some nodes
pts = vcat(
ngon(O, 85, 3, π/6, vertices=true),
ngon(O, 200, 5, 0, vertices=true),
ngon(O, 260, 10, π/2, vertices=true),
)
n = length(pts)
# build graph
g = SimpleGraph(n)
for i in 1:n
for j in i:n
if i != j
# nodes are connected if within this distance
if 60 < distance(pts[i], pts[j]) < 160
add_edge!(g, i, j)
end
end
end
end
Random.seed!(10)
# find colouring that avoids adjacent same colours
colorsequence = Random.shuffle!(collect(1:length(pts)))
colouring = LightGraphs.perm_greedy_color(g, colorsequence)
if maximum(colouring.colors) > 4
@warn "$(length(colouring.colors)) needed, we had to reuse colors"
else
@info "$(length(colouring.colors)) we have enough colours"
end
# draw all the edges first
sethue("grey60")
setline(12)
for (n, e) in enumerate(edges(g))
s, d = src(e), dst(e)
dist = distance(pts[s], pts[d])
line(pts[s], pts[d], :stroke)
end
# find and draw a path
sethue("gold3")
setline(18)
for e in a_star(g, 10, 15)
s, d = src(e), dst(e)
line(pts[s], pts[d], :stroke)
end
# draw the nodes
for (n, v) in enumerate(vertices(g))
s = distance(O, pts[v])
col = colouring.colors[n]
if s < 100
# scale as get further away
julia_sphere(pts[v], rescale(s, 0, 250, 80, 30), action=:fillpreserve, col = mod1(col, 4))
else
julia_sphere(pts[v], rescale(s, 0, 250, 70, 20), action=:fillpreserve, col = mod1(col, 4))
end
# sethue("white")
# text(string(v), pts[v])
end
finish()
preview()
end
main()
@cormullion
Copy link
Author

Screenshot 2021-10-19 at 15 34 33

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment