Packing equal circles in a square
Pack N equal circles in a unit square with the largest possible radius: beat a Packomania record, or prove optimality for N = 31.

| 1 | """The best known packing of N = 31 equal circles in a square, the smallest case without a proof of optimality. |
| 2 | |
| 3 | Coordinates: Packomania, http://www.packomania.com/csq/txt/csq31.txt (E. Specht), in a unit square centred at the |
| 4 | origin, radius 0.089338333351. Checked below: every circle inside the square, no two overlapping (to the 12 digits |
| 5 | given). A circle is drawn in sand when it touches neither the walls nor another circle: it is free to move, so it does |
| 6 | not hold the radius down. |
| 7 | """ |
| 8 | import math |
| 9 | from pathlib import Path |
| 10 | |
| 11 | import kit |
| 12 | |
| 13 | R = 0.089338333351 |
| 14 | C = [ |
| 15 | (-0.410661666649, -0.410661666649), |
| 16 | (-0.231984999946, -0.410661666649), |
| 17 | (-0.053308333244, -0.410661666649), |
| 18 | (0.254662533649, -0.410661666649), |
| 19 | (0.410661666649, -0.323543231590), |
| 20 | (0.100677100202, -0.320031442605), |
| 21 | (0.257215322250, -0.232003236973), |
| 22 | (-0.410661666649, -0.231984999946), |
| 23 | (-0.231984999946, -0.231984999946), |
| 24 | (-0.053326570270, -0.229432211345), |
| 25 | (0.103229888804, -0.141373012929), |
| 26 | (0.410661666649, -0.140463242356), |
| 27 | (-0.229432211345, -0.053326570270), |
| 28 | (-0.410661666649, -0.053308333244), |
| 29 | (-0.050773781669, -0.050773781669), |
| 30 | (0.256676233203, -0.049833018312), |
| 31 | (0.410661666649, 0.040797205733), |
| 32 | (0.078847039992, 0.078847040001), |
| 33 | (-0.320031442605, 0.100677100202), |
| 34 | (-0.141373012929, 0.103229888804), |
| 35 | (0.255180873553, 0.128837390888), |
| 36 | (0.405733726638, 0.226685239992), |
| 37 | (-0.410661666649, 0.254662533649), |
| 38 | (0.128837390888, 0.255180873553), |
| 39 | (-0.049833018312, 0.256676233203), |
| 40 | (-0.232003236973, 0.257215322250), |
| 41 | (0.405733726644, 0.405733726644), |
| 42 | (0.226685239996, 0.405733726649), |
| 43 | (-0.323543231590, 0.410661666649), |
| 44 | (-0.140463242356, 0.410661666649), |
| 45 | (0.040797205733, 0.410661666649), |
| 46 | ] |
| 47 | EPS = 1e-11 # the coordinates carry 12 digits |
| 48 | assert len(C) == 31 |
| 49 | assert all(abs(c) <= 0.5 - R + EPS for p in C for c in p) |
| 50 | assert all(math.dist(p, q) >= 2 * R - EPS for i, p in enumerate(C) for q in C[i + 1:]) |
| 51 | |
| 52 | |
| 53 | def touches(i): |
| 54 | walls = any(abs(abs(c) - (0.5 - R)) < 1e-9 for c in C[i]) |
| 55 | return walls or any(j != i and math.dist(C[i], C[j]) < 2 * R + 1e-9 for j in range(len(C))) |
| 56 | |
| 57 | |
| 58 | free = [i for i in range(len(C)) if not touches(i)] |
| 59 | assert len(free) == 4, free |
| 60 | |
| 61 | S, X0, Y0 = 244, (kit.W - 244) / 2, 12 |
| 62 | at = lambda p: (X0 + (p[0] + 0.5) * S, Y0 + (0.5 - p[1]) * S) |
| 63 | body = kit.polygon([(X0, Y0), (X0 + S, Y0), (X0 + S, Y0 + S), (X0, Y0 + S)], 'faint-s', ' stroke-width="2"') |
| 64 | for i, p in enumerate(C): |
| 65 | x, y = at(p) |
| 66 | if i in free: |
| 67 | body += kit.circle(x, y, R * S - 1, 'sand', ' fill-opacity=".16"') + kit.circle(x, y, R * S - 1, 'sand-s', ' stroke-width="2"') |
| 68 | else: |
| 69 | body += kit.circle(x, y, R * S - 1, 'f-soft') + kit.circle(x, y, R * S - 1, 'f-s', ' stroke-width="2"') |
| 70 | kit.write(__file__, 'mathematics', body, label='N = 31 · r = 0.0893 · four circles free to move') |