"""Twelve unit spheres touching a central one in three dimensions, tau(3) = 12, drawn with depth.
The twelve centres are the vertices of a regular icosahedron at distance 2 from the centre: every two of them are at
least 60 degrees apart (inner product of the unit vectors 1/sqrt(5) <= 1/2), so the spheres touch the middle one and do
not overlap each other. Checked below, then drawn back to front in a slight perspective.
"""
import math
from itertools import combinations
from pathlib import Path
import kit
phi = (1 + 5 ** 0.5) / 2
V = []
for a in (-1, 1):
for b in (-phi, phi):
V += [(0, a, b), (a, b, 0), (b, 0, a)]
V = [tuple(c / math.hypot(*v) for c in v) for v in V]
assert len(V) == 12 and len(set(V)) == 12
assert all(sum(p * q for p, q in zip(u, v)) <= 0.5 + 1e-12 for u, v in combinations(V, 2)) # the kissing condition
def view(d, up=(0, 1, 0)):
"""Coordinates seen looking along -d: (right, up, towards the viewer)."""
cross = lambda u, v: (u[1] * v[2] - u[2] * v[1], u[2] * v[0] - u[0] * v[2], u[0] * v[1] - u[1] * v[0])
unit = lambda u: tuple(c / math.hypot(*u) for c in u)
d = unit(d); e1 = unit(cross(up, d)); e2 = cross(d, e1)
return lambda p: tuple(sum(a * b for a, b in zip(p, e)) for e in (e1, e2, d))
# nearly along a 3-fold axis: three balls in front, six around the rim, three hidden behind the middle one
see = view((1, 1.08, 1.18))
R, CX, CY, CAM = 44, kit.W / 2, 132, 16 # ball radius in px, centre of the drawing, camera distance in radii
spheres = [((0, 0, 0), True)] + [(tuple(2 * c for c in see(v)), False) for v in V]
spheres.sort(key=lambda s: s[0][2]) # back to front: the camera looks down -z from z = CAM
# shading, lit from the top left in both themes: the highlight is the light end of the ramp
SHADE = (''
''
'')
body = SHADE
for (x, y, z), centre in spheres:
k = CAM / (CAM - z) # perspective
px, py, r = CX + x * R * k, CY - y * R * k, R * k
if centre: # the fixed ball, solid
body += kit.circle(px, py, r, 'ink', ' style="fill:var(--bg)"') + kit.circle(px, py, r, 'ink', ' style="fill:url(#ball-i)"')
body += kit.circle(px, py, r, 'ink2-s', ' stroke-width="2"')
elif z < 0.6: # around the rim and behind: solid, nearer ones hiding farther ones
body += kit.circle(px, py, r, 'ink', ' style="fill:var(--bg)"') + kit.circle(px, py, r, 'f', ' style="fill:url(#ball-f)"')
body += kit.circle(px, py, r, 'f-s', ' stroke-width="2" stroke-opacity=".6"')
else: # the three in front: dashed outlines, so the middle ball stays in view
body += kit.circle(px, py, r, 'f-s', ' stroke-width="2.4" stroke-dasharray="6 5"')
kit.write(__file__, 'mathematics', body, label='τ(3) = 12 · twelve spheres touch the middle one')