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 | """Independent exact enumeration of unit circles through rational point sets. |
| 3 | |
| 4 | No code from either reproduced PR is used. For each non-collinear triple, |
| 5 | solve the two linear equations for its circumcenter relative to its first |
| 6 | point, and keep it only if its squared circumradius is exactly one. All |
| 7 | three chord lengths must be at most two, so an exact prefilter is safe. |
| 8 | |
| 9 | Input is an object with a ``points`` list of distinct coordinate pairs. |
| 10 | Coordinates are integers or strings understood by Fraction (e.g. "3/5" |
| 11 | or "0.25"); booleans and binary floats are deliberately rejected. The CLI |
| 12 | reads JSON decimal literals as exact strings. Extra fields, including a |
| 13 | claimed circle count, are ignored by solve(). |
| 14 | |
| 15 | This is a finite rational-input reproduction oracle, not a general-position |
| 16 | or extremal bound. Worst-case enumeration takes O(n^3) arithmetic operations, |
| 17 | whose bit cost depends on the input coordinate sizes, and O(n^2) storage. |
| 18 | """ |
| 19 | |
| 20 | from __future__ import annotations |
| 21 | |
| 22 | import argparse |
| 23 | from fractions import Fraction |
| 24 | import json |
| 25 | from pathlib import Path |
| 26 | import sys |
| 27 | |
| 28 | |
| 29 | def _coordinate(value: object) -> Fraction: |
| 30 | if type(value) not in (int, str): |
| 31 | raise ValueError("coordinates must be integers or rational strings") |
| 32 | try: |
| 33 | return Fraction(value) |
| 34 | except (ValueError, ZeroDivisionError) as exc: |
| 35 | raise ValueError(f"invalid rational coordinate: {value!r}") from exc |
| 36 | |
| 37 | |
| 38 | def _points(config: object) -> list[tuple[Fraction, Fraction]]: |
| 39 | if not isinstance(config, dict) or not isinstance(config.get("points"), list): |
| 40 | raise ValueError("input must be an object with a points list") |
| 41 | points = [] |
| 42 | for index, pair in enumerate(config["points"]): |
| 43 | if not isinstance(pair, (list, tuple)) or len(pair) != 2: |
| 44 | raise ValueError(f"point {index} must contain exactly two coordinates") |
| 45 | points.append((_coordinate(pair[0]), _coordinate(pair[1]))) |
| 46 | if len(set(points)) != len(points): |
| 47 | raise ValueError("duplicate point coordinates are not a point set") |
| 48 | return points |
| 49 | |
| 50 | |
| 51 | def solve(config: object) -> dict: |
| 52 | """Return every unit circle and its incident input indices, exactly.""" |
| 53 | points = _points(config) |
| 54 | n = len(points) |
| 55 | |
| 56 | # All endpoints on a radius-one circle are at squared distance <= 4. |
| 57 | # Store forward adjacency sets to enumerate each eligible triple once. |
| 58 | nearby: list[set[int]] = [set() for _ in points] |
| 59 | for i, (ax, ay) in enumerate(points): |
| 60 | for j in range(i + 1, n): |
| 61 | bx, by = points[j] |
| 62 | if (bx - ax) ** 2 + (by - ay) ** 2 <= 4: |
| 63 | nearby[i].add(j) |
| 64 | |
| 65 | incident: dict[tuple[Fraction, Fraction], set[int]] = {} |
| 66 | for i, (ax, ay) in enumerate(points): |
| 67 | for j in sorted(nearby[i]): |
| 68 | bx, by = points[j] |
| 69 | ux, uy = bx - ax, by - ay |
| 70 | half_u_norm = (ux * ux + uy * uy) / 2 |
| 71 | for k in sorted(nearby[i] & nearby[j]): |
| 72 | cx, cy = points[k] |
| 73 | vx, vy = cx - ax, cy - ay |
| 74 | determinant = ux * vy - uy * vx |
| 75 | if determinant == 0: |
| 76 | continue |
| 77 | half_v_norm = (vx * vx + vy * vy) / 2 |
| 78 | ox = (half_u_norm * vy - uy * half_v_norm) / determinant |
| 79 | oy = (ux * half_v_norm - half_u_norm * vx) / determinant |
| 80 | if ox * ox + oy * oy != 1: |
| 81 | continue |
| 82 | center = (ax + ox, ay + oy) |
| 83 | incident.setdefault(center, set()).update((i, j, k)) |
| 84 | |
| 85 | # A circle with >=3 distinct points has no collinear triple, and every |
| 86 | # one of its points participates in a triple, so these unions are its |
| 87 | # complete incidence lists; no second scan or approximate test is used. |
| 88 | circles = [ |
| 89 | {"center": [str(x), str(y)], "point_indices": sorted(incident[(x, y)])} |
| 90 | for x, y in sorted(incident) |
| 91 | ] |
| 92 | return {"n": n, "unit_circles": len(circles), "circles": circles} |
| 93 | |
| 94 | |
| 95 | def main() -> int: |
| 96 | parser = argparse.ArgumentParser(description=__doc__) |
| 97 | parser.add_argument("configuration", type=Path) |
| 98 | args = parser.parse_args() |
| 99 | try: |
| 100 | config = json.loads(args.configuration.read_text(), parse_float=str) |
| 101 | result = solve(config) |
| 102 | except (OSError, ValueError) as exc: |
| 103 | print(f"ERROR: {exc}", file=sys.stderr) |
| 104 | return 1 |
| 105 | print(json.dumps(result, indent=2)) |
| 106 | return 0 |
| 107 | |
| 108 | |
| 109 | if __name__ == "__main__": |
| 110 | raise SystemExit(main()) |