Eureka/tools/bloom-sheet/grow.py
Eureka a1b19054bb bloom-sheet: a large bare fill is a wash and a heavy line, not a solid
Flat solids read as stickers beside the plant. Small fills stay solid, like a
flower's centre, and dark fills stay cutouts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 15:53:43 +01:00

640 lines
29 KiB
Python

"""Grow the ornament layer of a bloom sheet (see ../SKILL.md).
The page owns its text; this script owns its geometry, in four stages:
1. motifs: a greedy loop borrowed from florilegium
(github.com/kevinraymond/florilegium): find the largest free disc, put a
motif in it, mark it taken, repeat. Big discs get flowers; small ones are
invisible junctions.
2. the tree: a minimum spanning tree over the motifs, so the page is one plant.
3. the vine: the tree cut into a few long paths, each one Catmull-Rom spline
carrying a sine wave sized to the room around it; branches leave their
parent tangentially.
4. scrolls: a rinceau. At every crest a scroll leaves the vine and winds into
a logarithmic spiral (exponentially rising curvature); scrolls shed smaller
counter-winding scrolls, three generations deep, each kept only if it fits.
5. budding: the largest free gap left pulls a branch out of the nearest stroke
that reaches it smoothly; the branch winds into a spiral inside the gap and
can itself be budded from. Repeats down to sub-millimetre gaps.
Every line is a weighted stroke: a closed outline offset from a centreline by
half a width that varies along it, filled rather than stroked. That is
florilegium's `ribbon` (ink.lua, MIT, Kevin Raymond), ported to Python.
Run: python grow.py spec.json layer.tex
spec.json: {"seed": 11, "panels": [[name, x, y, w, h, colour, content macro], ...],
"title_box": [x, y, w, h] or null, "swash": [x0, y0, x1, y1] or null,
"visitors": [[mark, ...], ...]}
All lengths are mm from the page's bottom left. A visitor is florilegium's idea
with the caller's drawing: it takes the largest disc free before the plant
grows, and the plant grows around it. bloom_sheet.py's read_visitor makes the
marks, in a unit disc; they are drawn with the plant's own pen.
"""
import json, math, random, sys
from pathlib import Path
import numpy as np
spec = json.loads(Path(sys.argv[1]).read_text(encoding="utf8"))
SEED = spec["seed"]
rng = random.Random(SEED)
W, H = 210.0, 297.0
GAP = 0.8 # clearance between anything drawn and a panel edge
MARGIN = 1.8 # ornaments keep this far from the paper edge; wide enough that
# the vine can pass a full-width panel through the side margins
# ---------------------------------------------------------------- panels
# name, x, y, w, h (mm from bottom left), colour, content macro
PANELS = [tuple(p) for p in spec["panels"]]
# Gutters are 7 mm: narrower, and a scroll has no room to wind. Leave a hole or two
# open on purpose: the flowers need somewhere to bloom.
TITLE_BOX = spec["title_box"] # the title is text, not a panel, but it is not free space
# Corner radii (tl, tr, br, bl). Text starts top left and may reach bottom
# left, so those stay tight; the right-hand corners open into pockets.
def radii(w, h):
cap = min(w, h) / 3
return tuple(min(cap, rng.uniform(lo, hi)) for lo, hi in ((2, 4), (3, 9), (3, 10.5), (2, 7)))
panels = [(n, x, y, w, h, c, m, radii(w, h)) for n, x, y, w, h, c, m in PANELS]
# ---------------------------------------------------------------- distance field
STEP = 0.4
gx = np.arange(0, W + STEP / 2, STEP)
gy = np.arange(0, H + STEP / 2, STEP)
X, Y = np.meshgrid(gx, gy)
def sd_roundbox(px, py, x, y, w, h, r):
"""Signed distance to a box with one radius per corner (tl, tr, br, bl).
After Inigo Quilez's sdRoundBox, with y pointing up."""
tl, tr, br, bl = r
cx, cy = x + w / 2, y + h / 2
qx, qy = px - cx, py - cy
rr = np.where(qx > 0, np.where(qy > 0, tr, br), np.where(qy > 0, tl, bl))
ax = np.abs(qx) - w / 2 + rr
ay = np.abs(qy) - h / 2 + rr
return np.minimum(np.maximum(ax, ay), 0) + np.hypot(np.maximum(ax, 0), np.maximum(ay, 0)) - rr
wall = np.minimum.reduce([X - MARGIN, W - MARGIN - X, Y - MARGIN, H - MARGIN - Y])
solid = np.full_like(X, np.inf)
for n, x, y, w, h, c, m, r in panels:
solid = np.minimum(solid, sd_roundbox(X, Y, x, y, w, h, r))
if TITLE_BOX:
tx, ty, tw, th = TITLE_BOX
solid = np.minimum(solid, sd_roundbox(X, Y, tx, ty, tw, th, (0, 0, 0, 0)))
clear = np.minimum(solid - GAP, wall) # room to draw, ignoring ornaments
free = clear.copy() # room to draw, ornaments included
def clear_at(px, py):
i = int(round(py / STEP)); j = int(round(px / STEP))
if not (0 <= i < clear.shape[0] and 0 <= j < clear.shape[1]): return -1
return clear[i, j]
# ---------------------------------------------------------------- weighted strokes
def normals(pts):
out = []
for i in range(len(pts)):
a = pts[max(i - 1, 0)]; b = pts[min(i + 1, len(pts) - 1)]
dx, dy = b[0] - a[0], b[1] - a[1]
L = math.hypot(dx, dy) or 1
out.append((-dy / L, dx / L))
return out
def ribbon(pts, width):
"""Closed outline of a stroke along pts whose full width at fraction u of
its length is width(u)."""
nrm = normals(pts)
s = [0.0]
for i in range(1, len(pts)):
s.append(s[-1] + math.dist(pts[i], pts[i - 1]))
L = s[-1] or 1
left, right = [], []
for p, n, si in zip(pts, nrm, s):
hw = width(si / L) / 2
left.append((p[0] + n[0] * hw, p[1] + n[1] * hw))
right.append((p[0] - n[0] * hw, p[1] - n[1] * hw))
return left + right[::-1]
def arc_pts(p, q, bend, n=24):
"""Circular arc from p to q; bend is the sagitta as a fraction of the chord."""
(x0, y0), (x1, y1) = p, q
mx, my = (x0 + x1) / 2, (y0 + y1) / 2
dx, dy = x1 - x0, y1 - y0
L = math.hypot(dx, dy)
if abs(bend) < 1e-3 or L < 1e-6:
return [(x0 + dx * t / n, y0 + dy * t / n) for t in range(n + 1)]
s = bend * L
R = (L * L / 4 + s * s) / (2 * abs(s))
nx, ny = -dy / L, dx / L
sign = 1 if s > 0 else -1
cx, cy = mx - nx * sign * (R - abs(s)), my - ny * sign * (R - abs(s))
a0 = math.atan2(y0 - cy, x0 - cx); a1 = math.atan2(y1 - cy, x1 - cx)
da = (a1 - a0 + math.pi) % (2 * math.pi) - math.pi
# take the arc on the bulge side
midang = a0 + da / 2
if (math.cos(midang) * nx + math.sin(midang) * ny) * sign < 0:
da = da - 2 * math.pi if da > 0 else da + 2 * math.pi
return [(cx + R * math.cos(a0 + da * t / n), cy + R * math.sin(a0 + da * t / n)) for t in range(n + 1)]
def vesica(c, ang, length, fat, n=9):
"""A pointed petal or leaf: two circular arcs from c to the tip."""
tip = (c[0] + length * math.cos(ang), c[1] + length * math.sin(ang))
a = arc_pts(c, tip, fat, n)
b = arc_pts(tip, c, fat, n)
return a + b[1:]
def path(poly):
return " -- ".join(f"({x:.2f},{y:.2f})" for x, y in poly) + " -- cycle"
out = []
def neon(colour, poly, core=0.85):
out.append(f"\\neonfill{{{colour}}}{{{int(core * 100)}}}{{{path(poly)}}}")
def neonline(colour, pts, w0, w1, core=0.9):
neon(colour, ribbon(pts, lambda u: w0 + (w1 - w0) * u), core)
def ring(colour, c, r, w):
pts = [(c[0] + r * math.cos(2 * math.pi * k / 24), c[1] + r * math.sin(2 * math.pi * k / 24)) for k in range(25)]
# a ring breathes: heavier on one side, like a pen circle
ph = rng.uniform(0, 2 * math.pi)
neon(colour, ribbon(pts, lambda u: w * (0.55 + 0.45 * math.cos(2 * math.pi * u + ph))), 0.8)
def dot(colour, c, r):
out.append(f"\\neondot{{{colour}}}{{{c[0]:.2f}}}{{{c[1]:.2f}}}{{{r:.2f}}}")
# ---------------------------------------------------------------- motifs
PALETTE = [("neonorange", 5), ("neonsky", 3), ("neonviolet", 3), ("neonrose", 2)]
def pick():
t = rng.uniform(0, sum(w for _, w in PALETTE))
for c, w in PALETTE:
t -= w
if t <= 0: return c
return PALETTE[0][0]
def rosette(c, r, col):
n = rng.choice((5, 6, 6, 8))
ph = rng.uniform(0, math.pi)
for k in range(n):
a = ph + 2 * math.pi * k / n
petal = vesica(c, a, r, 0.24 if n < 8 else 0.19)
out.append(f"\\petalwash{{{col}}}{{{path(petal)}}}")
# the petal's outline as one weighted stroke: fat at the belly, thin at the tip
neonline_closed(col, petal, 0.34 * min(1, r / 5) + 0.12)
ring(pick(), c, r * 0.3, 0.32)
dot("neonwhite", c, max(0.35, r * 0.09))
def neonline_closed(col, poly, w):
neon(col, ribbon(poly, lambda u: w * (0.25 + 0.75 * math.sin(math.pi * ((2 * u) % 1)) ** 2)), 0.9)
def curl(c, r, col):
turns = rng.uniform(1.2, 1.9)
a0 = rng.uniform(0, 2 * math.pi)
sgn = rng.choice((-1, 1))
k = math.log(9) / (turns * 2 * math.pi) # r falls to a ninth over the turns
n = int(22 * turns)
pts = []
for i in range(n + 1):
t = i / n * turns * 2 * math.pi
rad = r * math.exp(-k * t)
pts.append((c[0] + rad * math.cos(a0 + sgn * t), c[1] + rad * math.sin(a0 + sgn * t)))
neonline(col, pts, 0.55 * min(1, r / 3) + 0.15, 0.08)
dot(col, pts[-1], max(0.28, r * 0.09))
def sparkle(c, r, col):
ph = rng.uniform(0, math.pi / 4)
for k in range(4):
a = ph + k * math.pi / 2
L = r if k % 2 == 0 else r * 0.6
tip = (c[0] + L * math.cos(a), c[1] + L * math.sin(a))
neon(col, ribbon([c, ((c[0] + tip[0]) / 2, (c[1] + tip[1]) / 2), tip], lambda u: 0.5 * min(1, r / 2.5) * (1 - u) ** 1.4 + 0.02))
dot("neonwhite", c, max(0.22, r * 0.1))
def place(c, r):
"""Choose a motif for a disc of radius r; return (colour, drawn radius,
draw). Drawing waits until the vine is known, so a motif the vine could
not reach is never drawn. Discs too small for a flower are junctions of
the vine and draw nothing: the scrolls carry the small-scale detail."""
col = pick()
if r >= 3.0:
return col, r, lambda: rosette(c, r, col)
return col, 0.0, lambda: None
# ---------------------------------------------------------------- visitors
visitors = [] # (cx, cy, r, marks)
for number, marks in enumerate(spec.get("visitors", []), 1):
i, j = np.unravel_index(np.argmax(free), free.shape)
r = min(float(free[i, j]), 15.0)
if r < 4.0:
print(f"left out: visitor {number}: no free disc wider than {2 * r:.0f} mm")
continue
c = (float(gx[j]), float(gy[i]))
visitors.append((c[0], c[1], r, marks))
np.minimum(free, np.hypot(X - c[0], Y - c[1]) - r - 1.0, out=free)
def draw_visitor(cx, cy, r, marks):
a = math.radians(rng.uniform(-18, 18))
spot = lambda pts: [(cx + r * 0.95 * (x * math.cos(a) - y * math.sin(a)),
cy + r * 0.95 * (x * math.sin(a) + y * math.cos(a))) for x, y in pts]
def loop(col, pts, w): # a closed line breathes like a ring
ph = rng.uniform(0, 2 * math.pi)
neon(col, ribbon(pts + pts[:1], lambda u: w * (0.75 + 0.25 * math.cos(2 * math.pi * u + ph))), 0.9)
for m in marks:
if "rings" in m:
rings = [spot(ring_) for ring_ in m["rings"]]
whole = " ".join(path(ring_) for ring_ in rings)
if m["outlined"]:
out.append(f"\\begin{{scope}}[even odd rule]\\petalwash{{{m['colour']}}}{{{whole}}}\\end{{scope}}")
for ring_ in rings:
loop(m["colour"], ring_, 0.6)
else:
out.append(f"\\begin{{scope}}[even odd rule]\\neonfill{{{m['colour']}}}{{85}}{{{whole}}}\\end{{scope}}")
continue
pts = spot(m["pts"])
if m["wash"]: out.append(f"\\petalwash{{{m['wash']}}}{{{path(pts)}}}")
# the caller's weight, kept within the plant's; an open line swells between thin ends
w = max(0.14, min(0.7, m["width"] * r * 0.95))
if m["closed"]:
loop(m["colour"], pts, w)
else:
neon(m["colour"], ribbon(pts, lambda u: w * (0.4 + 0.6 * math.sin(math.pi * u) ** 0.7)), 0.9)
# ---------------------------------------------------------------- 1. motifs
nodes = [] # (cx, cy, drawn radius, colour)
draws = []
RMAX = 8.5
while True:
i, j = np.unravel_index(np.argmax(free), free.shape)
r = float(free[i, j])
if r < 1.0: break
r = min(r, RMAX)
c = (float(gx[j]), float(gy[i]))
col, rd, draw = place(c, r * 0.92) # drawn a hair smaller than its disc
draws.append(draw)
# flowers keep a stem's length apart, so the vine between them shows
pad = 1.8 if rd >= 3.0 else 0.5
np.minimum(free, np.hypot(X - c[0], Y - c[1]) - r - pad, out=free)
nodes.append((c[0], c[1], rd, col))
# ---------------------------------------------------------------- 2. the tree
# Kruskal's minimum spanning tree over every pair of motifs a clear arc can
# join, shortest gap first. A tree, so the page is one plant and no gutter
# holds two parallel stems. It fixes topology only; the vine is drawn in 3.
parent = list(range(len(nodes)))
def root(a):
while parent[a] != a:
parent[a] = parent[parent[a]]; a = parent[a]
return a
cands = []
for a in range(len(nodes)):
ax, ay, ar, _ = nodes[a]
for b in range(a + 1, len(nodes)):
bx, by, br, _ = nodes[b]
gap = math.hypot(bx - ax, by - ay) - ar - br
if 0.4 <= gap <= 24: cands.append((gap, a, b))
cands.sort()
edges = []
for gap, a, b in cands:
if root(a) == root(b): continue
ax, ay, ar, _ = nodes[a]; bx, by, br, _ = nodes[b]
ang = math.atan2(by - ay, bx - ax)
p0 = (ax + ar * math.cos(ang), ay + ar * math.sin(ang))
p1 = (bx - br * math.cos(ang), by - br * math.sin(ang))
if any(all(clear_at(*q) > 0.4 for q in arc_pts(p0, p1, bend, 12)) for bend in (0.0, 0.1, -0.1)):
edges.append((a, b)); parent[root(a)] = root(b)
# One plant: whatever the tree could not reach is left out, not drawn alone.
from collections import Counter
sizes = Counter(root(k) for k in range(len(nodes)))
main = sizes.most_common(1)[0][0]
for g, n in sizes.most_common()[1:]:
print("left out:", n, "motif(s) near", [(round(nodes[k][0]), round(nodes[k][1])) for k in range(len(nodes)) if root(k) == g][:3])
keep = [root(k) == main for k in range(len(nodes))]
for k in range(len(nodes)):
if keep[k]: draws[k]()
adj = {k: [] for k in range(len(nodes)) if keep[k]}
for a, b in edges:
if keep[a]: adj[a].append(b); adj[b].append(a)
# ---------------------------------------------------------------- 3. the vine
# The tree is cut into as few long paths as it allows: each walk goes on to
# the neighbour that turns least, and stops only at a leaf or a hairpin.
# Every path is one Catmull-Rom spline in one colour, so the vine is
# tangent-continuous; a branch path borrows its parent's previous node as a
# phantom control point, so it leaves the parent tangentially, like a shoot.
def turn(p, q, r):
a1 = math.atan2(q[1] - p[1], q[0] - p[0]); a2 = math.atan2(r[1] - q[1], r[0] - q[0])
return abs((a2 - a1 + math.pi) % (2 * math.pi) - math.pi)
P = lambda k: nodes[k][:2]
used = set()
paths = [] # (phantom node or None, [node ids])
def walk(phantom, start, first):
seq = [start, first]; used.add(frozenset((start, first)))
while True:
cur, prev = seq[-1], seq[-2]
nxt = [n for n in adj[cur] if frozenset((cur, n)) not in used]
if not nxt: break
n = min(nxt, key=lambda n: turn(P(prev), P(cur), P(n)))
if turn(P(prev), P(cur), P(n)) > math.radians(100): break
seq.append(n); used.add(frozenset((cur, n)))
paths.append((phantom, seq))
leaves = [k for k in adj if len(adj[k]) == 1]
start = max(leaves, key=lambda k: nodes[k][0] - nodes[k][1]) if leaves else next(iter(adj))
walk(None, start, adj[start][0])
pending = True
while pending:
pending = False
for phantom_, seq in list(paths):
for idx, k in enumerate(seq):
for n in adj[k]:
if frozenset((k, n)) in used: continue
# phantom: the path neighbour of k the branch continues most smoothly from
nbrs = [seq[i] for i in (idx - 1, idx + 1) if 0 <= i < len(seq)]
ph = min(nbrs, key=lambda m: turn(P(m), P(k), P(n))) if nbrs else None
walk(ph, k, n); pending = True
def catmull(ctrl, per):
out = []
for i in range(1, len(ctrl) - 2):
p0, p1, p2, p3 = ctrl[i - 1], ctrl[i], ctrl[i + 1], ctrl[i + 2]
for k in range(per):
t = k / per; t2 = t * t; t3 = t2 * t
out.append(tuple(0.5 * (2 * p1[j] + (-p0[j] + p2[j]) * t + (2 * p0[j] - 5 * p1[j] + 4 * p2[j] - p3[j]) * t2
+ (-p0[j] + 3 * p1[j] - 3 * p2[j] + p3[j]) * t3) for j in (0, 1)))
out.append(ctrl[-2])
return out
def mirror(a, b): # a phantom beyond b, continuing a -> b straight on
return (2 * b[0] - a[0], 2 * b[1] - a[1])
def resample(curve, ds):
out = [curve[0]]; carry = 0.0
for p, q in zip(curve, curve[1:]):
seg = math.dist(p, q); t = ds - carry
while t <= seg:
out.append((p[0] + (q[0] - p[0]) * t / seg, p[1] + (q[1] - p[1]) * t / seg)); t += ds
carry = seg - (t - ds)
if math.dist(out[-1], curve[-1]) > 1e-6: out.append(curve[-1])
return out
def heading(curve, i):
a = curve[max(i - 1, 0)]; b = curve[min(i + 1, len(curve) - 1)]
return math.atan2(b[1] - a[1], b[0] - a[0])
def wavy(centre, ph0):
"""Lay a sine wave along a smooth centreline. Wavelength and amplitude
follow the room at each point (phase is integrated, so a changing
wavelength never kinks), and the wave fades to nothing at both ends so
paths meet their junctions exactly. Returns the curve and its crests as
(index, side), side +1 for a crest on the left of travel."""
pts = resample(centre, 0.2)
L = 0.2 * (len(pts) - 1)
out, crests = [], []
ph = ph0; last = math.cos(ph)
for i, p in enumerate(pts):
c = clear_at(*p)
s = 0.2 * i
fade = max(0.0, min(1.0, s / 1.6, (L - s) / 1.6))
amp = max(0.0, min(1.4, 0.55 * (c - 0.45))) * fade
th = heading(pts, i)
off = amp * math.sin(ph)
out.append((p[0] - math.sin(th) * off, p[1] + math.cos(th) * off))
ph += 2 * math.pi * 0.2 / max(5.0, min(11.0, 4.2 * c))
now = math.cos(ph)
if fade > 0.6 and amp > 0.2 and (last > 0) != (now > 0):
crests.append((i, 1 if last > 0 else -1))
last = now
return out, crests
pos = {k: P(k) for k in adj}
room = clear + 0.5 # scrolls are hairlines: they may come closer to a panel than the vine
def mark(pts, rad):
"""Take a band of radius rad around a polyline out of the free room."""
dense = []
for p, q in zip(pts, pts[1:]):
n = max(1, int(math.dist(p, q) / 0.3))
dense += [(p[0] + (q[0] - p[0]) * t / n, p[1] + (q[1] - p[1]) * t / n) for t in range(n)]
dense.append(pts[-1])
k = int(math.ceil((rad + 0.5) / STEP))
for x, y in dense:
i, j = int(round(y / STEP)), int(round(x / STEP))
win = (slice(max(i - k, 0), i + k + 1), slice(max(j - k, 0), j + k + 1))
np.minimum(room[win], np.hypot(X[win] - x, Y[win] - y) - rad, out=room[win])
def room_at(x, y):
i = int(round(y / STEP)); j = int(round(x / STEP))
if not (0 <= i < room.shape[0] and 0 <= j < room.shape[1]): return -1
return room[i, j]
for vx, vy, vr, _ in visitors:
mark([(vx, vy)], vr)
vines = [] # (pts, colour, w0, w1)
for n_path, (phantom, seq) in enumerate(paths):
pts_n = [pos[k] for k in seq]
head = pos[phantom] if phantom is not None else mirror(pts_n[1], pts_n[0])
ctrl = [head] + pts_n + [mirror(pts_n[-2], pts_n[-1])]
curve, crests = wavy(catmull(ctrl, 8), rng.uniform(0, 2 * math.pi))
col = nodes[seq[0]][3] if phantom is None else pick()
w0 = 0.5 if phantom is None else 0.38
vines.append((curve, col, w0, 0.2, crests))
for k in adj:
if nodes[k][2] > 0: mark([P(k)], nodes[k][2] + 0.3)
for curve, col, w0, w1, crests in vines:
mark(curve, w0 / 2 + 0.25)
# ---------------------------------------------------------------- 4. scrolls
# The fractal part, after the rinceau: a scroll leaves its parent almost
# tangentially and winds into a logarithmic spiral, because its curvature
# grows exponentially along it. Partway along, it sheds smaller scrolls that
# wind the other way, and those shed smaller ones again: nested S-curves, all
# tangent-continuous with what they grow from. Each is kept only where it fits.
scrolls = [] # (pts, colour, w0)
leaves_out = []
def scroll_pts(base, theta, turn_dir, L, ds=0.18):
R0 = 0.5 * L
g = math.log(10)
pts = [base]; th = theta; x, y = base
s = 0.0; heads = [theta]
while s < L:
k = math.exp(g * s / L) / R0
th += turn_dir * k * ds
x += ds * math.cos(th); y += ds * math.sin(th)
pts.append((x, y)); heads.append(th); s += ds
return pts, heads
def scroll(base, theta, turn_dir, L, w0, depth, col, skip=0.55):
fit = None
for scale in (1.0, 0.75, 0.55, 0.4):
Ls = L * scale
if Ls < 0.9: return False
for td in (turn_dir, -turn_dir) if depth == 3 else (turn_dir,):
pts, heads = scroll_pts(base, theta, td, Ls)
wmax = w0 / 2 + 0.08
if all(room_at(*q) > wmax for q in pts if math.dist(q, base) > skip):
fit = (pts, heads, Ls, td); break
if fit: break
if not fit: return False
pts, heads, Ls, td = fit
width = lambda u: w0 * (1 - 0.85 * u) + 0.035
scrolls.append((pts[::2] + [pts[-1]], col, w0))
mark(pts[1:], w0 / 2 + 0.2)
if rng.random() < 0.35:
dot(rng.choice(("neonwhite", col)), pts[-1], 0.2)
if depth > 1:
n = len(pts)
for frac in (0.2, 0.42):
i = int(frac * n)
# peel off on the outside of the bend and wind the other way
scroll(pts[i], heads[i] - td * math.radians(18), -td, Ls * 0.5, width(i / n) * 0.85, depth - 1, col)
i = int(0.62 * n)
if rng.random() < 0.5:
la = heads[i] + td * math.radians(rng.uniform(35, 55))
ll = min(1.6, Ls * 0.3)
lt = (pts[i][0] + ll * math.cos(la), pts[i][1] + ll * math.sin(la))
if room_at(*lt) > 0.15 and room_at((pts[i][0] + lt[0]) / 2, (pts[i][1] + lt[1]) / 2) > 0.3:
leaves_out.append((col, vesica(pts[i], la, ll, 0.32))); mark([pts[i], lt], 0.35)
return True
order = list(range(len(vines)))
rng.shuffle(order)
for n_path in order:
curve, col, w0, w1, crests = vines[n_path]
# Rinceau: at each crest a scroll carries straight on and winds back
# toward the stem, into the bay the wave curves around.
for i, side in crests:
th = heading(curve, i)
# a scroll the size of the bay it winds into
amp = max(0.0, min(1.4, 0.55 * (clear_at(*curve[i]) - 0.45)))
L = max(1.8, min(7.0, 1.2 + 3.4 * amp))
# it leaves at an angle: a scroll tangent to its parent runs alongside
# it for a millimetre and reads as a thickening, not a branch
scroll(curve[i], th + side * math.radians(22), -side, L, 0.3, 3, col, skip=0.9)
# and the vine's end does not stop: it winds up into a scroll of its own
th = heading(curve, len(curve) - 1)
L = max(2.0, min(6.0, 2.0 * clear_at(*curve[-1])))
scroll(curve[-1], th, rng.choice((-1, 1)), L, w1 + 0.05, 3, col)
# ---------------------------------------------------------------- 5. budding
# Growing outward from the vine always leaves holes it happened to miss.
# So the gaps call the growth instead, florilegium's grown loop: take the
# largest free disc left, and bud a branch into it from the existing stroke
# that reaches it most smoothly; the branch winds up into a spiral inside the
# disc. Every branch becomes somewhere later branches can bud from, so big
# holes fill first and small ones last, as deep as the space allows.
anchors = [] # (x, y, heading, width, colour)
def add_anchors(pts, col, wfun):
n = len(pts)
for i in range(0, n, 2):
anchors.append((pts[i][0], pts[i][1], heading(pts, i), wfun(i / max(n - 1, 1)), col))
for curve, col, w0, w1, crests in vines:
add_anchors(curve, col, lambda u, w0=w0, w1=w1: w0 + (w1 - w0) * u)
for pts, col, w0 in scrolls:
add_anchors(pts, col, lambda u, w0=w0: w0 * (1 - 0.85 * u) + 0.035)
def bezier(p0, p1, p2, p3, n):
out = []
for k in range(n + 1):
t = k / n; a = (1 - t) ** 3; b = 3 * (1 - t) ** 2 * t; c = 3 * (1 - t) * t * t; d = t ** 3
out.append((a * p0[0] + b * p1[0] + c * p2[0] + d * p3[0], a * p0[1] + b * p1[1] + c * p2[1] + d * p3[1]))
return out
def bud_into(cx, cy, r):
"""Try to grow a branch from a nearby stroke into the disc (c, r)."""
A = np.array([(a[0], a[1]) for a in anchors])
d = np.hypot(A[:, 0] - cx, A[:, 1] - cy)
near = np.argsort(d)[:24]
tries = []
for k in near:
ax, ay, th, aw, col = anchors[k]
to_c = math.atan2(cy - ay, cx - ax)
for flip in (0, math.pi): # a stroke can be budded from either way along it
dev = (to_c - (th + flip) + math.pi) % (2 * math.pi) - math.pi
if abs(dev) > math.radians(115): continue
tries.append((d[k] * (1 + 0.9 * abs(dev) / math.pi), k, th + flip, dev))
tries.sort()
for score, k, th, dev in tries[:16]:
ax, ay, _, aw, col = anchors[k]
# leave the parent at no more than ~50 degrees, toward the disc
t0 = th + max(-1, min(1, dev / math.radians(50))) * math.radians(min(50, abs(math.degrees(dev))))
w = max(0.06, min(aw * 0.8, 0.1 + 0.11 * r))
rho0 = 0.72 * r
u = math.atan2(cy - ay, cx - ax)
for sense in (1 if dev > 0 else -1, -1 if dev > 0 else 1):
phi0 = u - sense * math.pi / 2
pe = (cx + rho0 * math.cos(phi0), cy + rho0 * math.sin(phi0))
L = math.dist((ax, ay), pe)
# a bud reaches its gap from close by; a long stalk reads as a stray wire
if L < 0.3 or L > 1.0 + 2.5 * r: continue
kk = L / 3
stalk = bezier((ax, ay), (ax + kk * math.cos(t0), ay + kk * math.sin(t0)),
(pe[0] - kk * math.cos(u), pe[1] - kk * math.sin(u)), pe, max(4, int(L / 0.25)))
spiral = []
for i in range(1, 25):
t = i / 24
ph = phi0 + sense * 2.3 * math.pi * t
rho = rho0 * math.exp(-math.log(7) * t)
spiral.append((cx + rho * math.cos(ph), cy + rho * math.sin(ph)))
pts = stalk + spiral
if all(room_at(*q) > w / 2 + 0.04 for q in pts if math.dist(q, (ax, ay)) > max(0.45, aw)):
return pts, col, w
return None
buds = 0
while True:
i, j = np.unravel_index(np.argmax(room), room.shape)
r = float(room[i, j])
if r < 0.42: break
cx, cy = float(gx[j]), float(gy[i])
got = bud_into(cx, cy, min(r, 6.0))
if got is None:
# nothing can reach this hole: close its middle so the loop moves on
mark([(cx, cy)], min(r, 6.0) * 0.35)
continue
pts, col, w = got
scrolls.append((pts, col, w))
mark(pts[1:], w / 2 + 0.16)
add_anchors(pts, col, lambda u, w=w: w * (1 - 0.85 * u) + 0.035)
if r > 0.9 and rng.random() < 0.4:
dot(rng.choice(("neonwhite", col)), pts[-1], min(0.25, 0.12 + 0.05 * r))
buds += 1
# ---------------------------------------------------------------- strokes out
stem_out = []
for curve, col, w0, w1, crests in vines:
stem_out.append((col, ribbon(curve[::2] + [curve[-1]], lambda u, w0=w0, w1=w1: w0 + (w1 - w0) * u)))
for pts, col, w0 in scrolls:
stem_out.append((col, ribbon(pts, lambda u, w0=w0: w0 * (1 - 0.85 * u) + 0.035)))
stem_out += leaves_out
nodes = [n for n, kk in zip(nodes, keep) if kk]
# ---------------------------------------------------------------- title swash
# One long weighted stroke under the title, ending in a curl.
swash_out = []
if spec.get("swash"):
sx0, sy0, sx1, sy1 = spec["swash"]
swash = arc_pts((sx0, sy0), (sx1, sy1), -0.012, 60)
swash_out = [ribbon(swash, lambda u: 0.15 + 0.85 * math.sin(math.pi * u ** 0.8) ** 1.5)]
# ---------------------------------------------------------------- write
lines = [f"% generated by {Path(__file__).name}, seed {SEED}. Do not edit; run the script."]
lines.append("\\begin{scope}% stems and leaves")
for col, poly in stem_out:
lines.append(f"\\neonfill{{{col}}}{{75}}{{{path(poly)}}}")
lines.append("\\end{scope}")
lines.append("\\begin{scope}% panel frames")
for n, x, y, w, h, c, m, (tl, tr, br, bl) in panels:
p = (f"({x + bl:.2f},{y:.2f}) -- ({x + w - br:.2f},{y:.2f}) arc[start angle=-90, end angle=0, radius={br:.2f}] "
f"-- ({x + w:.2f},{y + h - tr:.2f}) arc[start angle=0, end angle=90, radius={tr:.2f}] "
f"-- ({x + tl:.2f},{y + h:.2f}) arc[start angle=90, end angle=180, radius={tl:.2f}] "
f"-- ({x:.2f},{y + bl:.2f}) arc[start angle=180, end angle=270, radius={bl:.2f}] -- cycle")
lines.append(f"\\neonframe{{{c}}}{{{p}}}")
lines.append("\\end{scope}")
for poly in swash_out:
lines.append(f"\\neonfill{{neonorange}}{{90}}{{{path(poly)}}}")
for vx, vy, vr, marks in visitors:
draw_visitor(vx, vy, vr, marks)
lines.extend(out)
for n, x, y, w, h, c, m, r in panels:
lines.append(f"\\panel{{{x}}}{{{y}}}{{{w}}}{{{h}}}{{{c}}}{{{m}}}")
dst = Path(sys.argv[2])
dst.write_text("\n".join(lines) + "\n", encoding="utf8")
print(f"seed {SEED}: {len(nodes)} motifs, {len(paths)} vine paths, {len(scrolls)} scrolls ({buds} budded) -> {dst.name}")