"""f(5) = 9, happening: eight points in general position with no convex pentagon (ink), and a ninth (sand) that makes one. The eight are found by a seeded random search, then spread out by hill climbing on the smallest triangle and the closest pair, so that no three look collinear; checked here: no five of them are in convex position. The ninth is the point of a grid over the box that keeps the nine and their pentagon clearest. Any nine points in general position contain a convex pentagon, so it completes one; the code draws the one with the least flat corner, and checks that every convex pentagon among the nine uses the new point.""" import itertools import math import random from pathlib import Path import kit AX, AY = 2.4, 1.0 # the box the points live in, drawn at 640 x 304 less the label def cross(o, a, b): return (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]) def convex_position(pts): """Every point is a corner of the hull: none lies inside a triangle of the others.""" for p in pts: for a, b, c in itertools.combinations([q for q in pts if q is not p], 3): if (cross(a, b, p) > 0) == (cross(b, c, p) > 0) == (cross(c, a, p) > 0): return False return True def no_pentagon(pts): return not any(convex_position(s) for s in itertools.combinations(pts, 5)) def smallest_triangle(pts): return min(abs(cross(a, b, c)) / 2 for a, b, c in itertools.combinations(pts, 3)) def clarity(pts): # the smallest triangle and the closest pair, both as large as possible return min(smallest_triangle(pts) / 0.05, min(math.dist(p, q) for p, q in itertools.combinations(pts, 2)) / 0.4) rng = random.Random(5) while True: pts = [(rng.random() * AX, rng.random() * AY) for _ in range(8)] if no_pentagon(pts): break for _ in range(6000): # move one point a little; keep the move when the set stays pentagon-free and gets clearer i, s = rng.randrange(8), 0.08 q = list(pts) q[i] = (min(AX, max(0, q[i][0] + rng.uniform(-s, s))), min(AY, max(0, q[i][1] + rng.uniform(-s, s)))) if clarity(q) > clarity(pts) and no_pentagon(q): pts = q assert len(pts) == 8 and smallest_triangle(pts) > 0.01 and no_pentagon(pts) def around(c): # the points of a convex set in order around their centre mx, my = sum(p[0] for p in c) / len(c), sum(p[1] for p in c) / len(c) return sorted(c, key=lambda p: math.atan2(p[1] - my, p[0] - mx)) def area(c): h = around(c) return abs(sum(cross((0, 0), h[k], h[(k + 1) % len(h)]) for k in range(len(h)))) / 2 def corners(c): # how sharp the flattest corner of a convex polygon is: the triangle it makes with its neighbours h = around(c) return min(abs(cross(h[k - 1], h[k], h[(k + 1) % len(h)])) / 2 for k in range(len(h))) def pentagons_with(p): return [c for c in itertools.combinations(pts + [p], 5) if convex_position(c)] def score(p): # the nine stay clear, and the clearest pentagon through p has no flat corner sharp = corners(max(pentagons_with(p), key=corners)) return round(min(clarity(pts + [p]), sharp / 0.08), 6), round(sharp, 6), p grid = [(AX * i / 48, AY * j / 20) for i in range(49) for j in range(21)] new = max((p for p in grid if p not in pts), key=score) nine = pts + [new] assert smallest_triangle(nine) > 0.01 pentagons = pentagons_with(new) assert pentagons and all(new in c for c in pentagons) pentagon = around(max(pentagons, key=corners)) sx = sy = 232 / AY x0, y0 = (kit.W - AX * sx) / 2, 16 def at(p): return x0 + p[0] * sx, y0 + (AY - p[1]) * sy shape = [at(p) for p in pentagon] body = [kit.polygon(shape, 'f-soft'), kit.polygon(shape, 'f-s', ' stroke-width="3"')] body += [kit.dot(*at(p), 9) for p in pts] + [kit.dot(*at(new), 11, 'sand')] kit.write(__file__, 'mathematics', ''.join(body), label='8 points, no convex pentagon ยท a 9th makes one')