The Erdős–Szekeres happy ending problem
Prove that any 2^{n−2}+1 points in general position in the plane contain a convex n-gon; open from n = 7 on.

| 1 | """f(5) = 9, happening: eight points in general position with no convex pentagon (ink), and a ninth (sand) that makes |
| 2 | one. The eight are found by a seeded random search, then spread out by hill climbing on the smallest triangle and the |
| 3 | closest pair, so that no three look collinear; checked here: no five of them are in convex position. The ninth is the |
| 4 | point of a grid over the box that keeps the nine and their pentagon clearest. Any nine points in general position |
| 5 | contain a convex pentagon, so it completes one; the code draws the one with the least flat corner, and checks that |
| 6 | every convex pentagon among the nine uses the new point.""" |
| 7 | import itertools |
| 8 | import math |
| 9 | import random |
| 10 | from pathlib import Path |
| 11 | |
| 12 | import kit |
| 13 | |
| 14 | AX, AY = 2.4, 1.0 # the box the points live in, drawn at 640 x 304 less the label |
| 15 | |
| 16 | |
| 17 | def cross(o, a, b): |
| 18 | return (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]) |
| 19 | |
| 20 | |
| 21 | def convex_position(pts): |
| 22 | """Every point is a corner of the hull: none lies inside a triangle of the others.""" |
| 23 | for p in pts: |
| 24 | for a, b, c in itertools.combinations([q for q in pts if q is not p], 3): |
| 25 | if (cross(a, b, p) > 0) == (cross(b, c, p) > 0) == (cross(c, a, p) > 0): |
| 26 | return False |
| 27 | return True |
| 28 | |
| 29 | |
| 30 | def no_pentagon(pts): |
| 31 | return not any(convex_position(s) for s in itertools.combinations(pts, 5)) |
| 32 | |
| 33 | |
| 34 | def smallest_triangle(pts): |
| 35 | return min(abs(cross(a, b, c)) / 2 for a, b, c in itertools.combinations(pts, 3)) |
| 36 | |
| 37 | |
| 38 | def clarity(pts): # the smallest triangle and the closest pair, both as large as possible |
| 39 | return min(smallest_triangle(pts) / 0.05, min(math.dist(p, q) for p, q in itertools.combinations(pts, 2)) / 0.4) |
| 40 | |
| 41 | |
| 42 | rng = random.Random(5) |
| 43 | while True: |
| 44 | pts = [(rng.random() * AX, rng.random() * AY) for _ in range(8)] |
| 45 | if no_pentagon(pts): |
| 46 | break |
| 47 | for _ in range(6000): # move one point a little; keep the move when the set stays pentagon-free and gets clearer |
| 48 | i, s = rng.randrange(8), 0.08 |
| 49 | q = list(pts) |
| 50 | q[i] = (min(AX, max(0, q[i][0] + rng.uniform(-s, s))), min(AY, max(0, q[i][1] + rng.uniform(-s, s)))) |
| 51 | if clarity(q) > clarity(pts) and no_pentagon(q): |
| 52 | pts = q |
| 53 | |
| 54 | assert len(pts) == 8 and smallest_triangle(pts) > 0.01 and no_pentagon(pts) |
| 55 | |
| 56 | |
| 57 | def around(c): # the points of a convex set in order around their centre |
| 58 | mx, my = sum(p[0] for p in c) / len(c), sum(p[1] for p in c) / len(c) |
| 59 | return sorted(c, key=lambda p: math.atan2(p[1] - my, p[0] - mx)) |
| 60 | |
| 61 | |
| 62 | def area(c): |
| 63 | h = around(c) |
| 64 | return abs(sum(cross((0, 0), h[k], h[(k + 1) % len(h)]) for k in range(len(h)))) / 2 |
| 65 | |
| 66 | |
| 67 | def corners(c): # how sharp the flattest corner of a convex polygon is: the triangle it makes with its neighbours |
| 68 | h = around(c) |
| 69 | return min(abs(cross(h[k - 1], h[k], h[(k + 1) % len(h)])) / 2 for k in range(len(h))) |
| 70 | |
| 71 | |
| 72 | def pentagons_with(p): |
| 73 | return [c for c in itertools.combinations(pts + [p], 5) if convex_position(c)] |
| 74 | |
| 75 | |
| 76 | def score(p): # the nine stay clear, and the clearest pentagon through p has no flat corner |
| 77 | sharp = corners(max(pentagons_with(p), key=corners)) |
| 78 | return round(min(clarity(pts + [p]), sharp / 0.08), 6), round(sharp, 6), p |
| 79 | |
| 80 | |
| 81 | grid = [(AX * i / 48, AY * j / 20) for i in range(49) for j in range(21)] |
| 82 | new = max((p for p in grid if p not in pts), key=score) |
| 83 | nine = pts + [new] |
| 84 | assert smallest_triangle(nine) > 0.01 |
| 85 | pentagons = pentagons_with(new) |
| 86 | assert pentagons and all(new in c for c in pentagons) |
| 87 | pentagon = around(max(pentagons, key=corners)) |
| 88 | |
| 89 | sx = sy = 232 / AY |
| 90 | x0, y0 = (kit.W - AX * sx) / 2, 16 |
| 91 | |
| 92 | |
| 93 | def at(p): |
| 94 | return x0 + p[0] * sx, y0 + (AY - p[1]) * sy |
| 95 | |
| 96 | |
| 97 | shape = [at(p) for p in pentagon] |
| 98 | body = [kit.polygon(shape, 'f-soft'), kit.polygon(shape, 'f-s', ' stroke-width="3"')] |
| 99 | body += [kit.dot(*at(p), 9) for p in pts] + [kit.dot(*at(new), 11, 'sand')] |
| 100 | kit.write(__file__, 'mathematics', ''.join(body), label='8 points, no convex pentagon · a 9th makes one') |