# Draw A = G·P (Z[phi], Minkowski plane) as a small SVG.
import json, sys
d = json.load(open(sys.argv[1]))
pts, X = d["pts"], d["X"]
W, M = 640, 40                       # canvas, margin
lo, hi = -620, 620                   # data window, same on both axes
s = (W - 2 * M) / (hi - lo)
tx = lambda x: M + (x - lo) * s
ty = lambda y: W - M - (y - lo) * s

out = [f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {W} {W}" width="{W}" height="{W}">',
       f'<rect width="{W}" height="{W}" fill="#F6F3EC"/>',
       f'<g stroke="#c9c2b0" stroke-width="1">'
       f'<line x1="{tx(lo)}" y1="{ty(0)}" x2="{tx(hi)}" y2="{ty(0)}"/>'
       f'<line x1="{tx(0)}" y1="{ty(lo)}" x2="{tx(0)}" y2="{ty(hi)}"/></g>']
# the two hyperbola branches |xy| = X^2 that multiplication by units walks along
for sign in (1, -1):
    path = []
    for i in range(1, 400):
        x = 20 + i * 1.4
        y = sign * X * X / x
        if lo <= y <= hi:
            path.append(f"{'M' if not path else 'L'}{tx(x):.1f},{ty(y):.1f}")
    out.append(f'<path d="{" ".join(path)}" fill="none" stroke="#b8a77a" stroke-width="1" stroke-dasharray="3 4"/>')
for x, y in pts:
    out.append(f'<circle cx="{tx(x):.1f}" cy="{ty(y):.1f}" r="1.1" fill="#1d3557"/>')
out.append(f'<text x="{M}" y="{M - 14}" font-family="Georgia, serif" font-size="13" fill="#444">'
           f'A = G·P in Z[φ], seen through its two real embeddings  ·  |A| = {len(pts)}</text>')
out.append('</svg>')
open(sys.argv[2], "w").write("\n".join(out))
print("ok", len(pts))
