"""The best known packing of N = 31 equal circles in a square, the smallest case without a proof of optimality. Coordinates: Packomania, http://www.packomania.com/csq/txt/csq31.txt (E. Specht), in a unit square centred at the origin, radius 0.089338333351. Checked below: every circle inside the square, no two overlapping (to the 12 digits given). A circle is drawn in sand when it touches neither the walls nor another circle: it is free to move, so it does not hold the radius down. """ import math from pathlib import Path import kit R = 0.089338333351 C = [ (-0.410661666649, -0.410661666649), (-0.231984999946, -0.410661666649), (-0.053308333244, -0.410661666649), (0.254662533649, -0.410661666649), (0.410661666649, -0.323543231590), (0.100677100202, -0.320031442605), (0.257215322250, -0.232003236973), (-0.410661666649, -0.231984999946), (-0.231984999946, -0.231984999946), (-0.053326570270, -0.229432211345), (0.103229888804, -0.141373012929), (0.410661666649, -0.140463242356), (-0.229432211345, -0.053326570270), (-0.410661666649, -0.053308333244), (-0.050773781669, -0.050773781669), (0.256676233203, -0.049833018312), (0.410661666649, 0.040797205733), (0.078847039992, 0.078847040001), (-0.320031442605, 0.100677100202), (-0.141373012929, 0.103229888804), (0.255180873553, 0.128837390888), (0.405733726638, 0.226685239992), (-0.410661666649, 0.254662533649), (0.128837390888, 0.255180873553), (-0.049833018312, 0.256676233203), (-0.232003236973, 0.257215322250), (0.405733726644, 0.405733726644), (0.226685239996, 0.405733726649), (-0.323543231590, 0.410661666649), (-0.140463242356, 0.410661666649), (0.040797205733, 0.410661666649), ] EPS = 1e-11 # the coordinates carry 12 digits assert len(C) == 31 assert all(abs(c) <= 0.5 - R + EPS for p in C for c in p) assert all(math.dist(p, q) >= 2 * R - EPS for i, p in enumerate(C) for q in C[i + 1:]) def touches(i): walls = any(abs(abs(c) - (0.5 - R)) < 1e-9 for c in C[i]) return walls or any(j != i and math.dist(C[i], C[j]) < 2 * R + 1e-9 for j in range(len(C))) free = [i for i in range(len(C)) if not touches(i)] assert len(free) == 4, free S, X0, Y0 = 244, (kit.W - 244) / 2, 12 at = lambda p: (X0 + (p[0] + 0.5) * S, Y0 + (0.5 - p[1]) * S) body = kit.polygon([(X0, Y0), (X0 + S, Y0), (X0 + S, Y0 + S), (X0, Y0 + S)], 'faint-s', ' stroke-width="2"') for i, p in enumerate(C): x, y = at(p) if i in free: body += kit.circle(x, y, R * S - 1, 'sand', ' fill-opacity=".16"') + kit.circle(x, y, R * S - 1, 'sand-s', ' stroke-width="2"') else: body += kit.circle(x, y, R * S - 1, 'f-soft') + kit.circle(x, y, R * S - 1, 'f-s', ' stroke-width="2"') kit.write(__file__, 'mathematics', body, label='N = 31 · r = 0.0893 · four circles free to move')