ExperimentsFable 5.2 Python art (30 images)
Image 26 of 30
Orrery diagram
The Python script
Open script.py ↗ (opens in a new tab)#!/usr/bin/env python3
"""26 Orrery diagram.
A brass orrery (a clockwork model of the solar system) seen from above,
laid on a midnight-blue star chart, with a distance ruler below it.
Fable 5.2 Python art experiment: every pixel comes from the code below.
Rings, gears and arms are distance formulas, shaded like turned brass
(bright bands that sweep round the centre, bevelled edges, drop shadows).
The planets are small lit spheres with procedural surfaces. Numerals and
planet symbols are built from line strokes; no font is used. The ruler
shows the real mean distances from the Sun on a logarithmic scale.
Run: .venv/bin/python scripts/26-orrery-diagram.py
"""
import os, time, math
import numpy as np
from PIL import Image, ImageDraw
from scipy import ndimage as ndi
W, H = 1600, 2000
SS = 2
SEED = 26
T0 = time.time()
HERE = os.path.dirname(os.path.abspath(__file__))
OUT = os.path.join(HERE, '..', 'images', '26-orrery-diagram.png')
F = np.float32
rng = np.random.default_rng(SEED)
CX, CY = 800.0, 875.0
def smooth(a, b, x):
t = np.clip((x - a) / (b - a + 1e-9), 0, 1)
return t * t * (3 - 2 * t)
def aa(d):
"""Signed distance in pixels (negative inside) -> smooth coverage 0..1."""
return np.clip(0.5 - d, 0, 1)
def vnoise(shape, cell, r):
h, w = shape
g = r.random((int(h / cell) + 4, int(w / cell) + 4)).astype(F)
z = ndi.zoom(g, cell, order=3 if cell >= 3 else 1)
o = int(cell * 1.5)
return z[o:o + h, o:o + w]
def fbm(shape, cell, octaves, r):
out = np.zeros(shape, F)
amp, tot = 1.0, 0.0
for _ in range(octaves):
out += amp * vnoise(shape, max(1.0, cell), r)
tot += amp
amp *= 0.5
cell /= 2
return out / tot
BR_YEL = np.array([0.86, 0.66, 0.27], F)
BR_PALE = np.array([0.93, 0.79, 0.44], F)
BR_DARK = np.array([0.66, 0.45, 0.19], F)
INK = np.array([0.16, 0.10, 0.04], F)
NAVY = np.array([0.040, 0.065, 0.135], F)
GOLD = np.array([0.92, 0.76, 0.42], F)
img = None
YY, XX = np.mgrid[0:H, 0:W].astype(F)
SCR = fbm((H, W), 3, 2, np.random.default_rng(2)) # fine scratches
LATHE = np.random.default_rng(3).random(2000).astype(F) # turning marks by radius
def box(cx, cy, r, pad=34):
x0, x1 = max(0, int(cx - r - pad)), min(W, int(cx + r + pad))
y0, y1 = max(0, int(cy - r - pad)), min(H, int(cy + r + pad))
return (slice(y0, y1), slice(x0, x1))
def put(slc, mask, colour, shadow=0.0, off=(9, 12), sig=7.0):
if shadow > 0:
sh = ndi.gaussian_filter(ndi.shift(mask, off, order=1), sig)
img[slc] *= (1 - shadow * sh)[..., None]
img[slc] = img[slc] * (1 - mask[..., None]) + colour * mask[..., None]
def brass(slc, mask, base, centre=None, theta0=0.6, conic=True, bevel=1.0):
"""Shade a flat brass part: sweeping sheen, lathe marks, scratches and bevelled edges."""
X, Y = XX[slc], YY[slc]
cx, cy = centre if centre is not None else (CX, CY)
r = np.hypot(X - cx, Y - cy)
th = np.arctan2(Y - cy, X - cx)
if conic:
sheen = 0.40 + 0.50 * np.abs(np.cos(th - theta0)) ** 3 + 0.34 * np.abs(np.cos(th - theta0 - 1.1)) ** 10
sheen = sheen + 0.07 * (LATHE[np.clip((r * 1.6).astype(int), 0, 1999)] - 0.5)
else:
sheen = 0.72 + 0.16 * np.sin((X * 0.6 + Y * 0.8) * 0.01 + theta0)
sheen = sheen + 0.05 * (SCR[slc] - 0.5)
sm = ndi.gaussian_filter(mask, 1.6)
gx = ndi.sobel(sm, 1); gy = ndi.sobel(sm, 0)
bev = np.clip((gx * 0.6 + gy * 0.8) * 0.55 * bevel, -0.42, 0.5)
col = base[None, None, :] * (sheen + bev)[..., None]
col = col + np.clip(bev, 0, 1)[..., None] * 0.25
return col
def ring(r0, r1, base, theta0, shadow=0.45, centre=None, extra=None):
cx, cy = centre if centre is not None else (CX, CY)
slc = box(cx, cy, r1)
r = np.hypot(XX[slc] - cx, YY[slc] - cy)
mask = aa(r - r1) * aa(r0 - r)
if extra is not None:
mask = extra(mask, slc)
put(slc, mask, brass(slc, mask, base, (cx, cy), theta0), shadow)
return slc, mask
def gear(cx, cy, R, teeth, base, theta0, phase=0.0, hub=0.0, spokes=0, shadow=0.5, tooth=None):
slc = box(cx, cy, R + 12)
X, Y = XX[slc] - cx, YY[slc] - cy
r = np.hypot(X, Y); th = np.arctan2(Y, X)
tooth = tooth if tooth is not None else max(5.0, R * 0.11)
prof = smooth(-0.35, 0.35, np.sin(teeth * (th + phase)))
mask = aa(r - (R - tooth + tooth * prof))
if spokes:
r_in, r_out = R * 0.34, R - tooth - R * 0.16
window = aa(r - r_out) * aa(r_in - r)
sp = np.abs(np.sin(spokes * 0.5 * (th + phase)))
window *= smooth(0.16, 0.24, sp * (0.5 + 0.5 * r / r_out))
mask = mask * (1 - window)
if hub > 0:
mask *= 1 - aa(r - hub)
put(slc, mask, brass(slc, mask, base, (cx, cy), theta0), shadow)
def sphere(cx, cy, R, tex, shadow=0.55, spec=0.5, light=(-0.50, -0.62, 0.60)):
slc = box(cx, cy, R, 30)
X, Y = (XX[slc] - cx) / R, (YY[slc] - cy) / R
rr = X * X + Y * Y
mask = aa((np.sqrt(rr) - 1) * R)
Z = np.sqrt(np.clip(1 - rr, 0, 1))
L = np.array(light, F); L /= np.linalg.norm(L)
ndl = np.clip(X * L[0] + Y * L[1] + Z * L[2], 0, 1)
alb = tex(X, Y, Z)
Hx, Hy, Hz = L[0], L[1], L[2] + 1.0
hn = math.sqrt(Hx * Hx + Hy * Hy + Hz * Hz)
ndh = np.clip((X * Hx + Y * Hy + Z * Hz) / hn, 0, 1)
col = alb * (0.10 + 0.95 * ndl ** 0.9)[..., None] + (spec * ndh ** 40)[..., None]
col = col + ((1 - Z) ** 3 * 0.10)[..., None] * np.array([0.5, 0.6, 0.9], F) # faint rim from the blue ground
put(slc, mask, col, shadow, off=(R * 0.25 + 5, R * 0.3 + 6), sig=max(4.0, R * 0.22))
NTEX = fbm((512, 1024), 90, 5, np.random.default_rng(8))
NTEX2 = fbm((512, 1024), 30, 4, np.random.default_rng(9))
def lonlat(X, Y, Z, tilt=0.35, spin=0.0):
ca, sa = math.cos(tilt), math.sin(tilt)
y2 = Y * ca - X * sa
x2 = X * ca + Y * sa
lon = np.arctan2(x2, Z) + spin
lat = np.arcsin(np.clip(y2, -1, 1))
u = ((lon / (2 * math.pi)) % 1.0) * 1023
v = (lat / math.pi + 0.5) * 511
return u.astype(int), v.astype(int), lat
def col3(*c):
return np.array(c, F)[None, None, :]
def tex_mercury(X, Y, Z):
u, v, lat = lonlat(X, Y, Z, 0.2, 1.0)
n = NTEX2[v, u]
return col3(0.55, 0.50, 0.46) * (0.65 + 0.7 * n)[..., None]
def tex_venus(X, Y, Z):
u, v, lat = lonlat(X, Y, Z, 0.3, 2.0)
n = NTEX[(v + (NTEX2[v, u] * 60).astype(int)) % 512, u]
return col3(0.93, 0.80, 0.52) * (0.75 + 0.4 * n)[..., None]
def tex_earth(X, Y, Z):
u, v, lat = lonlat(X, Y, Z, 0.41, 0.7)
n = NTEX[v, u] + 0.12 * NTEX2[v, u]
land = smooth(0.56, 0.59, n)[..., None]
c = col3(0.10, 0.28, 0.62) * (1 - land) + col3(0.30, 0.50, 0.22) * land
cloud = smooth(0.50, 0.75, NTEX[(v + 200) % 512, (u * 2 + 300) % 1024])[..., None]
ice = smooth(1.15, 1.3, np.abs(lat))[..., None]
c = c * (1 - 0.8 * cloud) + col3(0.95, 0.96, 0.98) * 0.8 * cloud
return c * (1 - ice) + col3(0.95, 0.97, 1.0) * ice
def tex_mars(X, Y, Z):
u, v, lat = lonlat(X, Y, Z, 0.44, 4.0)
n = NTEX[v, u]
c = col3(0.80, 0.38, 0.20) * (0.7 + 0.5 * n)[..., None]
dark = smooth(0.55, 0.68, NTEX2[v, u])[..., None]
c = c * (1 - 0.45 * dark)
ice = smooth(1.2, 1.32, lat)[..., None] + smooth(1.25, 1.35, -lat)[..., None]
return c * (1 - ice) + col3(0.96, 0.95, 0.93) * ice
def tex_jupiter(X, Y, Z):
u, v, lat = lonlat(X, Y, Z, 0.10, 0.3)
wob = (NTEX2[v, u] - 0.5) * 0.16
b = np.sin((lat + wob) * 11.0) * 0.5 + 0.5
b2 = np.sin((lat + wob * 0.6) * 23.0 + 1.0) * 0.5 + 0.5
c = col3(0.88, 0.78, 0.62) * b[..., None] + col3(0.64, 0.42, 0.26) * (1 - b[..., None])
c = c * (0.86 + 0.14 * b2[..., None])
lon = u / 1023 * 2 * math.pi
spot = np.exp(-(((lon - 3.3) / 0.30) ** 2 + ((lat - 0.38) / 0.13) ** 2))[..., None]
return c * (1 - spot) + col3(0.78, 0.30, 0.18) * spot
def tex_saturn(X, Y, Z):
u, v, lat = lonlat(X, Y, Z, 0.45, 0.3)
b = np.sin(lat * 13.0) * 0.5 + 0.5
return col3(0.90, 0.80, 0.56) * (0.84 + 0.16 * b)[..., None]
def tex_uranus(X, Y, Z):
u, v, lat = lonlat(X, Y, Z, 1.4, 0.0)
return col3(0.62, 0.86, 0.88) * (0.92 + 0.08 * np.sin(lat * 6))[..., None]
def tex_neptune(X, Y, Z):
u, v, lat = lonlat(X, Y, Z, 0.5, 0.0)
b = np.sin(lat * 9.0 + NTEX2[v, u] * 2) * 0.5 + 0.5
c = col3(0.20, 0.36, 0.86) * (0.8 + 0.25 * b)[..., None]
streak = smooth(0.70, 0.78, NTEX2[(v * 3) % 512, u])[..., None] * 0.35
return c * (1 - streak) + col3(0.9, 0.95, 1.0) * streak
def tex_moon(X, Y, Z):
u, v, lat = lonlat(X, Y, Z, 0.1, 2.0)
return col3(0.78, 0.77, 0.74) * (0.7 + 0.5 * NTEX2[v, u])[..., None]
# name, orbit radius (px), arm angle (deg), size (px), texture, mean distance (AU)
PLANETS = [('mercury', 122, 38, 17, tex_mercury, 0.39), ('venus', 172, 158, 25, tex_venus, 0.72),
('earth', 230, 287, 27, tex_earth, 1.00), ('mars', 288, 212, 20, tex_mars, 1.52),
('jupiter', 374, 338, 54, tex_jupiter, 5.20), ('saturn', 458, 103, 42, tex_saturn, 9.58),
('uranus', 524, 250, 33, tex_uranus, 19.2), ('neptune', 580, 22, 32, tex_neptune, 30.05)]
def stroke_mask(draw_fn):
im = Image.new('L', (W * SS, H * SS), 0)
d = ImageDraw.Draw(im)
draw_fn(d)
return np.asarray(im.resize((W, H), Image.BOX), F) / 255.0
def L2(d, pts, w):
d.line([(x * SS, y * SS) for x, y in pts], fill=255, width=max(1, int(round(w * SS))), joint='curve')
def arc_pts(cx, cy, r, a0, a1, n=24, ry=None):
ry = r if ry is None else ry
return [(cx + r * math.cos(a), cy + ry * math.sin(a)) for a in np.linspace(math.radians(a0), math.radians(a1), n)]
def symbol(d, name, cx, cy, s, w):
"""Astronomical symbols, drawn with strokes."""
def P(x, y):
return (cx + x * s, cy + y * s)
def circ(x, y, r, a0=0, a1=360):
L2(d, arc_pts(cx + x * s, cy + y * s, r * s, a0, a1, 40), w)
if name == 'sun':
circ(0, 0, 0.62)
d.ellipse([(cx - 0.12 * s) * SS, (cy - 0.12 * s) * SS, (cx + 0.12 * s) * SS, (cy + 0.12 * s) * SS], fill=255)
elif name == 'mercury':
circ(0, -0.08, 0.30); circ(0, -0.66, 0.28, 10, 170)
L2(d, [P(0, 0.22), P(0, 0.78)], w); L2(d, [P(-0.24, 0.52), P(0.24, 0.52)], w)
elif name == 'venus':
circ(0, -0.28, 0.36); L2(d, [P(0, 0.08), P(0, 0.78)], w); L2(d, [P(-0.27, 0.45), P(0.27, 0.45)], w)
elif name == 'earth':
circ(0, 0, 0.58); L2(d, [P(0, -0.58), P(0, 0.58)], w); L2(d, [P(-0.58, 0), P(0.58, 0)], w)
elif name == 'mars':
circ(-0.14, 0.18, 0.38); L2(d, [P(0.13, -0.09), P(0.60, -0.56)], w)
L2(d, [P(0.24, -0.58), P(0.62, -0.58), P(0.62, -0.20)], w)
elif name == 'jupiter':
L2(d, arc_pts(cx - 0.42 * s, cy - 0.30 * s, 0.26 * s, -150, 60, 20) + [P(-0.52, 0.22)], w)
L2(d, [P(-0.56, 0.22), P(0.50, 0.22)], w); L2(d, [P(0.22, -0.62), P(0.22, 0.72)], w)
elif name == 'saturn':
L2(d, [P(-0.26, -0.72), P(-0.26, 0.30)], w); L2(d, [P(-0.50, -0.46), P(-0.02, -0.46)], w)
L2(d, arc_pts(cx + 0.02 * s, cy + 0.10 * s, 0.28 * s, 180, 370, 20) + [P(0.16, 0.50), P(0.30, 0.66), P(0.46, 0.58)], w)
elif name == 'uranus':
circ(0, 0.46, 0.22); L2(d, [P(0, 0.24), P(0, -0.66)], w)
L2(d, [P(-0.38, -0.66), P(-0.38, 0.02)], w); L2(d, [P(0.38, -0.66), P(0.38, 0.02)], w); L2(d, [P(-0.38, -0.32), P(0.38, -0.32)], w)
elif name == 'neptune':
L2(d, [P(0, -0.70), P(0, 0.76)], w); L2(d, [P(-0.24, 0.48), P(0.24, 0.48)], w)
L2(d, arc_pts(cx, cy - 0.58 * s, 0.42 * s, 0, 180, 24, ry=0.52 * s), w)
for sx in (-0.42, 0.0, 0.42):
L2(d, [P(sx - 0.10, -0.56 if sx else -0.60), P(sx, -0.74), P(sx + 0.10, -0.56 if sx else -0.60)], w * 0.8)
def engrave(mask, strength=0.72):
"""Cut dark lines into the metal with a thin light edge below them."""
global img
lit = np.clip(ndi.shift(mask, (1.3, 1.0), order=1) - mask, 0, 1)
img = img * (1 - strength * mask[..., None]) + INK * (strength * mask[..., None]) * 0.6
img += lit[..., None] * 0.16 * BR_PALE
def main():
global img
# ------------------------------------------------------------ star chart ground
g = fbm((H, W), 260, 4, rng)
img = NAVY[None, None, :] * (0.72 + 0.6 * g)[..., None]
img += (fbm((H, W), 2, 1, rng) - 0.5)[..., None] * 0.012
d0 = np.hypot(XX - CX, YY - CY)
img *= (1.15 - 0.35 * smooth(300, 1200, d0))[..., None]
srng = np.random.default_rng(14)
n = 900
sx = srng.uniform(70, W - 70, n); sy = srng.uniform(70, H - 70, n); mag = srng.random(n) ** 2.6
def chart(d):
# faint coordinate lines
for rr in range(120, 1300, 118):
pts = arc_pts(CX, CY, rr + 26, 0, 360, 240)
L2(d, pts, 0.6)
for a in range(0, 360, 15):
L2(d, [(CX + 705 * math.cos(math.radians(a)), CY + 705 * math.sin(math.radians(a))),
(CX + 1500 * math.cos(math.radians(a)), CY + 1500 * math.sin(math.radians(a)))], 0.6)
grat = stroke_mask(chart)
img += grat[..., None] * 0.085 * np.array([0.55, 0.70, 1.0], F)
def figures(d):
# imaginary constellations: bright stars joined by thin lines
bright = np.argsort(-mag)[:150]
used = set()
for seed in bright[:26]:
if seed in used:
continue
chain = [seed]; used.add(seed)
for _ in range(srng.integers(3, 6)):
cur = chain[-1]
dd = np.hypot(sx[bright] - sx[cur], sy[bright] - sy[cur])
order = np.argsort(dd)
nxt = next((bright[j] for j in order[1:6] if bright[j] not in used and 40 < dd[j] < 190), None)
if nxt is None:
break
chain.append(nxt); used.add(nxt)
if len(chain) > 2:
L2(d, [(sx[i], sy[i]) for i in chain], 1.0)
fig = stroke_mask(figures)
img += fig[..., None] * 0.30 * GOLD
star = np.zeros((H, W), F)
for i in range(n):
r = 0.7 + 2.5 * mag[i]
y0, x0 = int(sy[i]), int(sx[i])
ys, xs = slice(y0 - 8, y0 + 9), slice(x0 - 8, x0 + 9)
dd = np.hypot(XX[ys, xs] - sx[i], YY[ys, xs] - sy[i])
star[ys, xs] = np.maximum(star[ys, xs], aa(dd - r) * (0.45 + 0.55 * mag[i]))
img += (star + 0.6 * ndi.gaussian_filter(star, 4))[..., None] * np.array([1.0, 0.92, 0.72], F)
# ------------------------------------------------------------ frame
def frame(d):
for m_, w_ in ((46, 3.0), (60, 1.2), (66, 1.2)):
L2(d, [(m_, m_), (W - m_, m_), (W - m_, H - m_), (m_, H - m_), (m_, m_)], w_)
for (px, py, ax, ay) in ((66, 66, 1, 1), (W - 66, 66, -1, 1), (66, H - 66, 1, -1), (W - 66, H - 66, -1, -1)):
for rr in (70, 96, 104):
a0 = math.degrees(math.atan2(ay, ax)) - 45
L2(d, arc_pts(px, py, rr, a0, a0 + 90, 30), 1.4)
L2(d, [(px + ax * 122, py), (px + ax * 138, py + ay * 9), (px + ax * 154, py), (px + ax * 138, py - ay * 9), (px + ax * 122, py)], 1.2)
L2(d, [(px, py + ay * 122), (px + ax * 9, py + ay * 138), (px, py + ay * 154), (px - ax * 9, py + ay * 138), (px, py + ay * 122)], 1.2)
fr = stroke_mask(frame)
inside = ((XX > 44) & (XX < W - 44) & (YY > 44) & (YY < H - 44))
img = img * (1 - 0.9 * fr[..., None]) + GOLD * 0.9 * fr[..., None] * (0.75 + 0.5 * g)[..., None]
print('ground done %.1fs' % (time.time() - T0))
# ------------------------------------------------------------ outer calendar ring
ring(632, 704, BR_YEL, 0.7, shadow=0.55)
ring(606, 622, BR_PALE, 2.2, shadow=0.45)
def cal(d):
for k in range(360):
a = math.radians(k)
r0 = 690 if k % 10 == 0 else (694 if k % 5 == 0 else 697)
L2(d, [(CX + r0 * math.cos(a), CY + r0 * math.sin(a)), (CX + 702 * math.cos(a), CY + 702 * math.sin(a))], 1.8 if k % 10 == 0 else 1.0)
for rr in (688, 638):
L2(d, arc_pts(CX, CY, rr, 0, 360, 360), 1.3)
for k in range(12):
a = math.radians(k * 30 - 90)
L2(d, [(CX + 638 * math.cos(a), CY + 638 * math.sin(a)), (CX + 688 * math.cos(a), CY + 688 * math.sin(a))], 1.8)
# Roman numeral for the month, built from strokes
num = ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X', 'XI', 'XII'][k]
am = math.radians(k * 30 - 90 + 15)
rad = np.array([math.cos(am), math.sin(am)]); tng = np.array([-rad[1], rad[0]])
hh = 14.0
wid = {'I': 9.0, 'V': 19.0, 'X': 19.0}
total = sum(wid[c] for c in num) + 4.0 * (len(num) - 1)
u = -total / 2
base = np.array([CX, CY]) + rad * 663
def Q(uu, vv):
p = base + tng * uu + rad * vv
return (p[0], p[1])
for c in num:
wc = wid[c]; um = u + wc / 2
if c == 'I':
L2(d, [Q(um, -hh), Q(um, hh)], 2.6)
elif c == 'V':
L2(d, [Q(um - 7, hh), Q(um, -hh), Q(um + 7, hh)], 2.4)
else:
L2(d, [Q(um - 7, hh), Q(um + 7, -hh)], 2.4); L2(d, [Q(um - 7, -hh), Q(um + 7, hh)], 2.4)
u += wc + 4.0
L2(d, [Q(-total / 2 - 3, hh), Q(total / 2 + 3, hh)], 1.6)
L2(d, [Q(-total / 2 - 3, -hh), Q(total / 2 + 3, -hh)], 1.6)
for k in range(72): # beads on the inner ring
a = math.radians(k * 5 + 2.5)
px, py = CX + 614 * math.cos(a), CY + 614 * math.sin(a)
d.ellipse([(px - 2.3) * SS, (py - 2.3) * SS, (px + 2.3) * SS, (py + 2.3) * SS], fill=255)
engrave(stroke_mask(cal))
# ------------------------------------------------------------ orbit rings
for i, (name, orb, ang, size, tex, au) in enumerate(PLANETS[::-1]):
ring(orb - 3.2, orb + 3.2, BR_PALE if i % 2 else BR_YEL, 0.4 + i * 0.5, shadow=0.38)
def orbit_ticks(d):
for (name, orb, ang, size, tex, au) in PLANETS:
for k in range(0, 360, 10):
a = math.radians(k)
L2(d, [(CX + (orb - 3) * math.cos(a), CY + (orb - 3) * math.sin(a)), (CX + (orb + 3) * math.cos(a), CY + (orb + 3) * math.sin(a))], 1.0)
engrave(stroke_mask(orbit_ticks), 0.6)
# ------------------------------------------------------------ a small gear train beside the hub
gear(CX - 118, CY + 96, 46, 22, BR_DARK, 1.2, 0.1, hub=7, spokes=5, shadow=0.5)
gear(CX - 178, CY + 62, 24, 12, BR_PALE, 2.0, 0.26, hub=5, shadow=0.5)
gear(CX + 132, CY - 70, 38, 18, BR_DARK, 0.2, 0.0, hub=7, spokes=4, shadow=0.5)
# ------------------------------------------------------------ arms and planets (outer ones lie lowest)
for (name, orb, ang, size, tex, au) in PLANETS[::-1]:
a = math.radians(ang)
ex, ey = CX + orb * math.cos(a), CY + orb * math.sin(a)
slc = box((CX + ex) / 2, (CY + ey) / 2, orb / 2 + 60, 40)
X, Y = XX[slc] - CX, YY[slc] - CY
dx, dy = ex - CX, ey - CY
tail = min(0.32, 70.0 / orb)
t = np.clip((X * dx + Y * dy) / (orb * orb), -tail, 1)
dist = np.hypot(X - t * dx, Y - t * dy)
wid = 10.5 - 4.0 * np.clip(t, 0, 1)
mask = aa(dist - wid)
mask = np.maximum(mask, aa(np.hypot(X + tail * dx, Y + tail * dy) - 14)) # counterweight
mask = np.maximum(mask, aa(np.hypot(X - dx, Y - dy) - (size + 7))) # cup that holds the planet
colr = brass(slc, mask, BR_DARK * 1.05, None, ang * 0.1, conic=False, bevel=1.5)
along = (X * -dy + Y * dx) / orb
colr = colr * (0.92 + 0.14 * np.cos(along / 8.5 * 1.3))[..., None]
put(slc, mask, colr, 0.5, off=(10, 13), sig=6)
if name == 'saturn': # the ring of Saturn, tilted
sl2 = box(ex, ey, size * 2.3, 20)
u = (XX[sl2] - ex) * math.cos(-0.45) + (YY[sl2] - ey) * math.sin(-0.45)
v = -(XX[sl2] - ex) * math.sin(-0.45) + (YY[sl2] - ey) * math.cos(-0.45)
rr = np.hypot(u / (size * 2.15), v / (size * 0.62))
rm = smooth(0.58, 0.61, rr) * (1 - smooth(0.985, 1.0, rr)) * (1 - 0.85 * smooth(0.80, 0.815, rr) * (1 - smooth(0.845, 0.86, rr)))
rc = np.array([0.86, 0.76, 0.56], F)[None, None, :] * (0.8 + 0.2 * np.sin(rr * 90))[..., None]
put(sl2, rm * (v < 0), rc * 0.8, 0.3)
sphere(ex, ey, size, tex)
put(sl2, rm * (v >= 0), rc, 0.0)
else:
sphere(ex, ey, size, tex)
if name == 'earth': # the Moon on its own little arm
mx, my = ex + 46 * math.cos(a + 1.9), ey + 46 * math.sin(a + 1.9)
sl2 = box((ex + mx) / 2, (ey + my) / 2, 30)
Xm, Ym = XX[sl2] - ex, YY[sl2] - ey
tm = np.clip((Xm * (mx - ex) + Ym * (my - ey)) / (46 * 46), 0, 1)
mm = aa(np.hypot(Xm - tm * (mx - ex), Ym - tm * (my - ey)) - 2.2) * (1 - aa(np.hypot(Xm, Ym) - size))
put(sl2, mm, brass(sl2, mm, BR_PALE, None, 0, conic=False), 0.4, off=(4, 5), sig=3)
sphere(mx, my, 8.0, tex_moon, spec=0.2)
if name == 'jupiter':
for k, (rr_, aa_) in enumerate(((72, 0.4), (80, 2.3), (76, 3.7), (88, 5.2))):
sphere(ex + rr_ * math.cos(aa_), ey + rr_ * math.sin(aa_), 4.2 + (k % 2), tex_moon, shadow=0.4, spec=0.2)
# ------------------------------------------------------------ hub: gears, rays and the Sun
gear(CX, CY, 92, 44, BR_YEL, 0.3, 0.0, spokes=6, shadow=0.6)
gear(CX, CY, 66, 30, BR_PALE, 1.7, 0.05, shadow=0.55)
# sun rays plate: sixteen points
slc = box(CX, CY, 84)
X, Y = XX[slc] - CX, YY[slc] - CY
r = np.hypot(X, Y); th = np.arctan2(Y, X)
tri = np.abs(((th * 16 / (2 * math.pi)) % 1.0) - 0.5) * 2
long_ = (np.floor(th * 16 / (2 * math.pi)) % 2 == 0)
rmax = 44 + np.where(long_, 36, 24) * (1 - tri) ** 1.0
mask = aa(r - rmax)
put(slc, mask, brass(slc, mask, BR_PALE * 1.05, None, 2.6, bevel=1.4), 0.5, off=(5, 7), sig=4)
gran = fbm((H, W), 7, 3, np.random.default_rng(21))
def tex_sun(X_, Y_, Z_):
gsl = gran[box(CX, CY, 40, 30)]
return col3(1.0, 0.80, 0.30) * (0.9 + 0.5 * gsl)[..., None] * (0.72 + 0.5 * Z_)[..., None] + col3(0.9, 0.5, 0.1) * 0.25
sphere(CX, CY, 40, lambda X_, Y_, Z_: tex_sun(X_, Y_, Z_) * 1.05, shadow=0.0, spec=0.5)
glow = np.exp(-np.clip(d0 - 38, 0, None) / 46.0) * 0.30 + np.exp(-np.clip(d0 - 38, 0, None) / 190.0) * 0.10
img += glow[..., None] * np.array([1.0, 0.72, 0.30], F)
print('orrery done %.1fs' % (time.time() - T0))
# ------------------------------------------------------------ distance ruler with planet symbols
RX0, RX1, RY = 235.0, 1440.0, 1812.0
la, lb = math.log10(0.3), math.log10(40.0)
def xof(au):
return RX0 + (math.log10(au) - la) / (lb - la) * (RX1 - RX0)
slc = (slice(int(RY - 40), int(RY + 46)), slice(int(RX0 - 60), int(RX1 + 60)))
X, Y = XX[slc], YY[slc]
qx = np.abs(X - (RX0 + RX1) / 2) - (RX1 - RX0) / 2 - 22
qy = np.abs(Y - RY) - 15
mask = aa(np.maximum(qx, qy))
put(slc, mask, brass(slc, mask, BR_YEL, None, 1.0, conic=False, bevel=1.4), 0.5, off=(7, 9), sig=6)
names = ['sun'] + [p[0] for p in PLANETS]
xs_sym = [140.0] + [xof(p[5]) for p in PLANETS]
for x in xs_sym: # medallions
sl2 = box(x, 1700, 33)
rr = np.hypot(XX[sl2] - x, YY[sl2] - 1700)
mm = aa(rr - 33)
put(sl2, mm, brass(sl2, mm, BR_PALE, (x, 1700), 0.8 + x * 0.01, bevel=1.3), 0.5, off=(6, 8), sig=5)
def legend(d):
ticks = [0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9] + list(range(1, 10)) + [10, 20, 30, 40]
for tk in ticks:
x = xof(tk)
major = tk in (1, 10)
L2(d, [(x, RY - 15), (x, RY - (2 if major else 7))], 2.0 if major else 1.2)
L2(d, [(RX0 - 14, RY + 6), (RX1 + 14, RY + 6)], 1.2)
for name, x in zip(names, xs_sym):
symbol(d, name, x, 1700, 19, 2.6)
L2(d, arc_pts(x, 1700, 28, 0, 360, 60), 1.2)
for p in PLANETS: # a dot on the ruler for every planet
x = xof(p[5])
d.ellipse([(x - 3.4) * SS, (RY + 6 - 3.4) * SS, (x + 3.4) * SS, (RY + 6 + 3.4) * SS], fill=255)
engrave(stroke_mask(legend), 0.78)
def leaders(d):
for p in PLANETS:
x = xof(p[5])
L2(d, [(x, 1736), (x, RY - 19)], 1.1)
L2(d, [(140, 1736), (140, RY), (RX0 - 26, RY)], 1.1)
ld = stroke_mask(leaders)
img = img * (1 - 0.8 * ld[..., None]) + GOLD * 0.8 * ld[..., None]
for p in PLANETS: # the planets again, in a row under the ruler
sphere(xof(p[5]), 1876, max(7.0, p[3] * 0.5), p[4], shadow=0.4)
sphere(152, 1866, 24, lambda X_, Y_, Z_: col3(1.0, 0.78, 0.28) * (1.1 + 0.5 * Z_)[..., None], shadow=0.3, spec=0.8)
# ------------------------------------------------------------ finish
hi = np.clip(img - 0.85, 0, 3)
img += 0.35 * ndi.gaussian_filter(hi, (6, 6, 0)) + 0.2 * ndi.gaussian_filter(hi, (24, 24, 0))
vig = 1 - 0.30 * smooth(0.5, 1.2, np.hypot((XX - W / 2) / W, (YY - H / 2) / H) * 1.75)
img *= vig[..., None]
img = np.clip(img, 0, 1) ** (1 / 1.08)
img += (rng.random((H, W, 1)).astype(F) - 0.5) * 0.014
Image.fromarray((np.clip(img, 0, 1) * 255 + 0.5).astype(np.uint8)).save(OUT)
print('saved %s %.1fs' % (os.path.abspath(OUT), time.time() - T0))
if __name__ == '__main__':
main()

