Files
Bedrock/brand/ribbon-study/generate.py
T

63 lines
2.6 KiB
Python

"""Draw the Bedrock ribbon study with Python's standard library."""
from math import cos, sin, pi
from pathlib import Path
def path(points, color):
coords = " ".join(f"{x:.2f},{y:.2f}" for x, y in points)
return f'<polygon points="{coords}" fill="{color}"/>'
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)
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 place(shape, x, y, scale):
return f'<g transform="translate({x} {y}) scale({scale})">{shape}</g>'
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')
if __name__ == "__main__":
Path(__file__).with_name("preview.svg").write_text(draw(), encoding="utf-8")