Unit circles through three of n points
Show that n points in the plane have only o(n²) unit circles passing through three of them; the best construction gives n^{3/2}.

| 1 | #!/usr/bin/env python3 |
| 2 | """Exact count of unit circles through at least three of n rational points. |
| 3 | |
| 4 | Usage: python3 repro.py <configuration.json> |
| 5 | |
| 6 | Independent of routes/rusirui-rational-verifier in method: this is the chord |
| 7 | construction, not circumcentre enumeration by triples. |
| 8 | |
| 9 | For a circle of radius 1 through two points p and q, the centre lies on the |
| 10 | perpendicular bisector of the chord. Write m for the midpoint, v = q - p with |
| 11 | squared length d2, and v' = (-v_y, v_x) for a perpendicular of the same length. |
| 12 | A centre on the bisector is c = m + s v' with s rational, since m and v' are |
| 13 | rational and c is rational. The radius condition |c - p|^2 = 1 becomes |
| 14 | |
| 15 | |m - p|^2 + s^2 |v'|^2 = 1, so s^2 = (1 - d2/4) / d2 = (4 - d2) / (4 d2), |
| 16 | |
| 17 | because m - p is perpendicular to v'. Hence a pair has rational centres on a |
| 18 | unit circle exactly when s^2 is the square of a rational: |
| 19 | |
| 20 | * d2 = 4: s = 0 and the single centre is m (the points are antipodal); |
| 21 | * 0 < d2 < 4 with (4 - d2) / (4 d2) a perfect square: the two centres |
| 22 | m +/- s v'; |
| 23 | * otherwise: none. |
| 24 | |
| 25 | Completeness on rational input: a unit circle through three input points has |
| 26 | three of its pairs on it, and its rational centre is of the form above for any |
| 27 | of them, so at least one pair yields it here. A line meets a circle in at most |
| 28 | two points, so three points on a circle are never collinear and no case is |
| 29 | missed. |
| 30 | |
| 31 | A first version of this file tested the chord length and the height separately |
| 32 | for being rational, which is wrong: on the unit circle, (1,0) and (0,1) have |
| 33 | chord sqrt(2) and height sqrt(2)/2, neither rational, yet their centres are |
| 34 | (0,0) and (1,1). Only the ratio is rational. The corpus in this route has a |
| 35 | case (three-on-unit-circle) that fails immediately under the wrong test. |
| 36 | |
| 37 | Points must be distinct after reduction and 3 <= n <= 80; coordinates are JSON |
| 38 | integers or strings "p/q" with q > 0. JSON floats, JSON booleans and strings |
| 39 | that are not exactly such a fraction are refused. |
| 40 | """ |
| 41 | import json |
| 42 | import re |
| 43 | import sys |
| 44 | from fractions import Fraction |
| 45 | from itertools import combinations |
| 46 | from math import isqrt, gcd |
| 47 | |
| 48 | RATIONAL = re.compile(r"-?[0-9]{1,32}(?:/[1-9][0-9]{0,31})?\Z") |
| 49 | MAX_POINTS = 80 |
| 50 | |
| 51 | |
| 52 | class Invalid(ValueError): |
| 53 | pass |
| 54 | |
| 55 | |
| 56 | def coordinate(value): |
| 57 | if type(value) is not int: |
| 58 | if not isinstance(value, str) or not RATIONAL.fullmatch(value): |
| 59 | raise Invalid("coordinate is not an integer or a fraction p/q") |
| 60 | value = value |
| 61 | else: |
| 62 | value = str(value) |
| 63 | return Fraction(value) |
| 64 | |
| 65 | |
| 66 | def perfect_square(value): |
| 67 | """Return sqrt(value) as a Fraction when value is the square of a rational, else None.""" |
| 68 | if value <= 0: |
| 69 | return None |
| 70 | numerator, denominator = value.numerator, value.denominator |
| 71 | top, bottom = isqrt(numerator), isqrt(denominator) |
| 72 | if top * top != numerator or bottom * bottom != denominator: |
| 73 | return None |
| 74 | return Fraction(top, bottom) |
| 75 | |
| 76 | |
| 77 | def read(path): |
| 78 | with open(path, "rb") as stream: |
| 79 | raw = stream.read() |
| 80 | payload = json.loads(raw) |
| 81 | if not isinstance(payload, dict) or payload.get("schema") != 1 or type(payload.get("schema")) is not int: |
| 82 | raise Invalid("schema must be the integer 1") |
| 83 | raw_points = payload.get("points") |
| 84 | if not isinstance(raw_points, list) or not 3 <= len(raw_points) <= MAX_POINTS: |
| 85 | raise Invalid(f"expected 3..{MAX_POINTS} points") |
| 86 | points = [] |
| 87 | for entry in raw_points: |
| 88 | if not isinstance(entry, list) or len(entry) != 2: |
| 89 | raise Invalid("each point must be [x, y]") |
| 90 | points.append((coordinate(entry[0]), coordinate(entry[1]))) |
| 91 | if len(set(points)) != len(points): |
| 92 | raise Invalid("points must be distinct after reduction") |
| 93 | return points |
| 94 | |
| 95 | |
| 96 | def centres(points): |
| 97 | found = set() |
| 98 | for (ax, ay), (bx, by) in combinations(points, 2): |
| 99 | dx, dy = bx - ax, by - ay |
| 100 | d2 = dx * dx + dy * dy |
| 101 | if d2 == 0 or d2 > 4: |
| 102 | continue |
| 103 | mx, my = (ax + bx) / 2, (ay + by) / 2 |
| 104 | if d2 == 4: |
| 105 | found.add((mx, my)) |
| 106 | continue |
| 107 | step = perfect_square((4 - d2) / (4 * d2)) |
| 108 | if step is None: |
| 109 | continue |
| 110 | offset_x, offset_y = step * -dy, step * dx |
| 111 | found.add((mx + offset_x, my + offset_y)) |
| 112 | found.add((mx - offset_x, my - offset_y)) |
| 113 | return sorted(c for c in found if sum(1 for p in points if (p[0] - c[0]) ** 2 + (p[1] - c[1]) ** 2 == 1) >= 3) |
| 114 | |
| 115 | |
| 116 | def render(value): |
| 117 | value = Fraction(value) |
| 118 | return str(value.numerator) if value.denominator == 1 else f"{value.numerator}/{value.denominator}" |
| 119 | |
| 120 | |
| 121 | def main(argv): |
| 122 | if len(argv) != 2: |
| 123 | print("usage: repro.py <configuration.json>", file=sys.stderr) |
| 124 | return 1 |
| 125 | try: |
| 126 | points = read(argv[1]) |
| 127 | except (Invalid, ValueError, OSError, json.JSONDecodeError) as exc: |
| 128 | print(f"invalid configuration: {exc}", file=sys.stderr) |
| 129 | return 1 |
| 130 | circles = centres(points) |
| 131 | print(json.dumps({"n": len(points), "unit_circles": len(circles), |
| 132 | "centers": [[render(x), render(y)] for x, y in circles]})) |
| 133 | return 0 |
| 134 | |
| 135 | |
| 136 | if __name__ == "__main__": |
| 137 | sys.exit(main(sys.argv)) |