Created
August 19, 2026 08:36
-
-
Save cormullion/bbaa3e9ea76cdd8a93187f31b81c1fee to your computer and use it in GitHub Desktop.
tree
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
| using Luxor | |
| using Colors | |
| function whiten(col::Colorant, f = 0.5) | |
| hsl = convert(HSL, col) | |
| h, s, l = hsl.h, hsl.s, hsl.l | |
| return convert(RGB, HSL(h, s, f)) | |
| end | |
| function drawball( | |
| pos, ballradius, col::Colorant; | |
| fromlum = 0.2, | |
| tolum = 1.0 | |
| ) | |
| @layer begin | |
| translate(pos) | |
| for i in ballradius:-0.5:1 | |
| sethue(whiten(col, rescale(i, ballradius, 0.5, fromlum, tolum))) | |
| offset = rescale(i, ballradius, 0.5, 0, -ballradius / 2) | |
| circle(O + (-offset, offset), i, :fill) | |
| end | |
| end | |
| return | |
| end | |
| function branch(π’::Turtle, step, depth, angle) | |
| # rescale according to current depth (0 to 4) | |
| angle = rescale(depth, 4, 1, 30, 14) | |
| step = rescale(step, 4, 1, 8, 4) | |
| setline(rescale(depth, 4, 1, 6, 2)) | |
| if depth > 0 | |
| Forward(π’, step) | |
| Turn(π’, angle) | |
| # recursive right subtree | |
| branch(π’, 0.8 * step, depth - 1, angle) | |
| Turn(π’, -2angle) | |
| # recursive left subtree | |
| branch(π’, 0.8 * step, depth - 1, angle) | |
| Turn(π’, angle) | |
| Forward(π’, -step) | |
| end | |
| # green to blue hues | |
| sethue(Oklch(0.5, 0.5, rescale(depth, 1, 4, 150, 230))) | |
| if depth != 4 | |
| drawball(Point(π’.xpos, π’.ypos), 25, getcolor()) | |
| end | |
| return | |
| end | |
| function main(fname) | |
| Drawing(600, 600, fname) | |
| origin() | |
| # border | |
| squircle(O, 298, 298, rt = 0.65, action = :path) | |
| sethue("black") | |
| fillpreserve() | |
| squircle(O, 276, 276, rt = 0.65, reversepath = true, action = :path) | |
| clip() | |
| tiles = Tiler(600, 600, 2, 2, margin = 0) | |
| for (pos, n) in tiles | |
| sethue([Luxor.julia_red, Luxor.julia_green, Luxor.julia_blue, Luxor.julia_purple][mod1(n, 4)]) | |
| box(pos, tiles.tilewidth, tiles.tilewidth, :fill) | |
| end | |
| clipreset() | |
| # tree | |
| translate(Point(0, 190)) | |
| rotate(-Ο / 2) | |
| π’ = Turtle() | |
| Pencolor(π’, "white") | |
| # ... step, iterations, angle | |
| branch(π’, 62, 4, 0) | |
| finish() | |
| return preview() | |
| end | |
| main("/tmp/tree.png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment