Refine petal spacing and project continuous spherical ribbons

This commit is contained in:
2026-12-06 03:47:24 +08:00
parent 6b26e8698f
commit 35a14dbd4f
2 changed files with 62 additions and 54 deletions
+61 -41
View File
@@ -1,47 +1,64 @@
"""Draw the Bedrock ribbon study with Python's standard library."""
"""Render a ribbon study as editable SVG, using only the standard library."""
from math import cos, sin, pi
from pathlib import Path
BACKGROUND = "#431744"
# Coordinates are relative to the centre of the reference mark.
CAP = "M-91 -78 C-67 -111 -34 -121 0 -121 C33 -121 70 -107 90 -80 C105 -59 82 -52 63 -53 C37 -52 14 -63 0 -74 C-23 -58 -47 -52 -69 -53 C-91 -53 -100 -63 -91 -78Z"
LEFT = "M-91 -78 C-58 -107 -28 -99 0 -74 C-23 -58 -47 -52 -69 -53 C-91 -53 -100 -63 -91 -78Z"
RIGHT = "M0 -74 C37 -104 65 -98 90 -80 C105 -59 82 -52 63 -53 C37 -52 14 -63 0 -74Z"
def path(points, color):
coords = " ".join(f"{x:.2f},{y:.2f}" for x, y in points)
return f'<polygon points="{coords}" fill="{color}"/>'
def gradient(name, colors):
stops = ''.join(f'<stop offset="{i/(len(colors)-1):.2f}" stop-color="{color}"/>'
for i, color in enumerate(colors))
return f'<linearGradient id="{name}" x2="100%" y2="24%">{stops}</linearGradient>'
def trefoil():
# Two faces share a crossing inside a rounded segment of the silhouette.
layers = []
for angle, base, left, right in [
(0, "#beabb6", "#a35f81", "#817e92"),
(120, "#ffdabd", "#8a8599", "#ffa086"),
(240, "#ffac91", "#fc887b", "#a8617c"),
]:
layers.append(f'''<g transform="rotate({angle})">
<path fill="{base}" d="M-80 -51 C-86 -63 -49 -96 0 -96 C46 -96 85 -65 80 -51 C75 -35 32 -35 0 -56 C-31 -35 -72 -34 -80 -51Z"/>
<path fill="{left}" d="M-80 -51 C-83 -58 -59 -76 -36 -73 C-21 -72 -9 -63 0 -56 C-31 -35 -72 -34 -80 -51Z"/>
<path fill="{right}" d="M0 -56 C24 -74 51 -82 76 -60 Q83 -54 80 -51 C75 -35 32 -35 0 -56Z"/>
</g>''')
return "".join(layers)
palettes = [
("#d6c9c6", "#917687", "#a35c78", "#874969", "#99909d", "#766d82"),
("#ffe0bf", "#ffc0a2", "#8e8e9b", "#726d84", "#ffb295", "#ff8c76"),
("#ffb390", "#ff917c", "#ff9983", "#ff846f", "#b57184", "#8c506f"),
]
for index, colors in enumerate(palettes):
parts = []
for face, outline in enumerate((CAP, LEFT, RIGHT)):
name = f"petal-{index}-{face}"
parts.append(gradient(name, colors[2*face:2*face+2]))
parts.append(f'<path fill="url(#{name})" d="{outline}"/>')
layers.append(f'<g transform="rotate({120*index})">{"".join(parts)}</g>')
return ''.join(layers)
def globe(count, wave, tilt, phase):
front = ["#ffd9ba", "#ff947f", "#b8c6c3", "#e6a2a2"]
rear = ["#99607b", "#766780", "#ad798b", "#7e708a"]
faces = []
for back in (True, False):
for i in range(count):
center = -pi / 2 + (i + .5) * pi / count
edges = []
for side in (-1, 1):
edge = []
for j in range(97):
lon = (j / 96 + int(back)) * pi
lat = center + (side * .33 * pi / count
+ wave * sin(2 * lon + phase) * sin(lon) ** 2 * cos(center))
edge.append((96 * cos(lat) * cos(lon), -96 * sin(lat)))
edges.append(edge)
faces.append(path(edges[0] + edges[1][::-1], (rear if back else front)[i % 4]))
return f'<g transform="rotate({tilt})">' + "".join(faces) + "</g>"
def coil(turns, width, pitch, tilt, prefix):
"""Project a spherical helix. Draw its far half-turns before its near ones."""
palettes = [
[("#7b566f", "#aa7889"), ("#a4b4bb", "#79748c"), ("#834764", "#b77987")],
[("#ffa58a", "#ff8876"), ("#ffdebb", "#f2b9a8"), ("#c2d6d0", "#a39daa")],
]
layers = [[], []]
for half in range(2 * turns):
near = half % 2
start = half*pi
name = f"{prefix}-{half}"
def point(t, side):
u = t / (2*pi*turns)
lat = -pi/2 + pi*u + side*width*pi/(2*turns)*sin(pi*u)**.55
lat += pitch*sin(t)*cos(lat) + .12*sin(2*t)*cos(lat)
return f"{100*cos(lat)*cos(t):.2f},{-100*sin(lat):.2f}"
# Follow the actual circular limb between both edges at each turn.
points = [point(start + pi*j/80, -1) for j in range(81)]
points += [point(start + pi, -1+j/8) for j in range(1,17)]
points += [point(start + pi*j/80, 1) for j in reversed(range(80))]
points += [point(start, 1-j/8) for j in range(1,17)]
layers[near].append(gradient(name, palettes[near][(half//2)%3]))
opacity = .90 if near else .84
layers[near].append(f'<polygon opacity="{opacity}" points="{" ".join(points)}" fill="url(#{name})"/>')
return f'<g transform="rotate({tilt})">{"".join(layers[0]+layers[1])}</g>'
def place(shape, x, y, scale):
@@ -49,13 +66,16 @@ def place(shape, x, y, scale):
def draw():
marks = [place(trefoil(), 239, 223, 1.58)]
for x, y, spec in [(536, 126, (3, .34, -17, .5)), (713, 126, (5, .16, 12, 1)),
(536, 319, (4, .24, -10, 2)), (713, 319, (3, .28, 83, .4))]:
marks.append(place(globe(*spec), x, y, .70))
return ('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 840 448" width="840" height="448">'
'<title>Bedrock — ribbon form studies</title><rect width="840" height="448" fill="#421840"/>'
+ "".join(marks) + '</svg>\n')
marks = [place(trefoil(), 214, 238, 1.20)]
for index, (x, y, turns, width, pitch, tilt) in enumerate([
(502, 88, 2, .77, .45, -14), (673, 88, 3, .91, .40, 46),
(502, 239, 4, .85, .21, 4), (673, 239, 4, .46, .10, -10),
(502, 391, 3, .65, .39, -12), (673, 391, 3, .82, .21, 82),
]):
marks.append(place(coil(turns, width, pitch, tilt, f"s{index}"), x, y, .48))
return ('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 800 480" width="800" height="480">'
f'<title>Bedrock ribbon studies</title><rect width="800" height="480" fill="{BACKGROUND}"/>'
+ ''.join(marks) + '</svg>\n')
if __name__ == "__main__":