from pathlib import Path
import csv
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import numpy as np
OUT = Path(__file__).resolve().parent
points = [("A", -3, 2), ("B", 1, 4), ("C", 3, 1)]
with (OUT / "coordinates.csv").open("w", newline="", encoding="utf-8") as f:
    w = csv.writer(f)
    w.writerow(["point", "original_x", "original_z", "image_x", "image_z", "units"])
    for label, x, z in points:
        w.writerow([label, x, z, x, -z, "arbitrary"])
plt.rcParams.update({"font.family": "DejaVu Sans", "font.size": 13, "svg.hashsalt": "mirror-reflection-v1"})
fig = plt.figure(figsize=(11, 9), facecolor="#f3f6ff")
ax = fig.add_axes([.10, .18, .65, .65], facecolor="#f3f6ff")
ax.set_xlim(-4.6, 4.6); ax.set_ylim(-4.8, 4.8); ax.set_aspect("equal")
ax.axhline(0, color="#172b4d", lw=3)
ax.axvline(0, color="#8190a8", lw=1, zorder=0)
ax.set_xticks(range(-4,5,2)); ax.set_yticks(range(-4,5,2))
ax.grid(alpha=.16)
for spine in ax.spines.values(): spine.set_visible(False)
xs = [p[1] for p in points] + [points[0][1]]
zs = [p[2] for p in points] + [points[0][2]]
ax.plot(xs, zs, color="#235b91", lw=2, alpha=.7)
ax.plot(xs, [-z for z in zs], color="#8b469c", lw=2, alpha=.7)
for label, x, z in points:
    ax.plot([x,x], [z,-z], "--", color="#8d9bb0", lw=1.4, zorder=0)
    ax.scatter([x], [z], color="#235b91", s=80, zorder=3)
    ax.scatter([x], [-z], color="#8b469c", s=80, zorder=3)
    ax.annotate(f"{label} ({x}, {z})", (x,z), xytext=(8,10), textcoords="offset points", color="#173958", fontsize=12, bbox={"facecolor":"#f3f6ff", "edgecolor":"none", "pad":1})
    ax.annotate(f"{label}′ ({x}, {-z})", (x,-z), xytext=(8,-23), textcoords="offset points", color="#73387e", fontsize=12, bbox={"facecolor":"#f3f6ff", "edgecolor":"none", "pad":1})
ax.set_xlabel("x: parallel to the mirror (arbitrary units)", labelpad=14)
ax.set_ylabel("z: perpendicular to the mirror (arbitrary units)", labelpad=12)
fig.text(.10,.93,"What a flat mirror changes", fontsize=25, weight="bold", color="#172b4d")
fig.text(.10,.885,"Ideal geometry: (x, z) → (x, −z)", fontsize=17, color="#172b4d")
fig.text(.77,.70,"Original points", color="#235b91", weight="bold", fontsize=14)
fig.text(.77,.65,"Same x coordinate\nSame distance\nfrom the mirror", color="#172b4d", linespacing=1.6, va="top")
fig.text(.77,.49,"Mirror: z = 0", color="#172b4d", weight="bold", fontsize=14)
fig.text(.77,.36,"Reflected image", color="#8b469c", weight="bold", fontsize=14)
fig.text(.77,.28,"The sign of z\nreverses.", color="#172b4d", linespacing=1.6, va="top")
fig.text(.10,.085,"The mirror plane is shown edge-on. Dashed lines pair each point with its image.", color="#172b4d", fontsize=12)
fig.text(.10,.048,"Geometrical construction, not measured data or a light-ray simulation. No 3D chirality test.", color="#415574", fontsize=11)
fig.savefig(OUT / "mirror-reflection.png", dpi=180)
fig.savefig(OUT / "mirror-reflection.svg", metadata={"Date": None})
plt.close(fig)
assert all((x,-(-z)) == (x,z) for _,x,z in points)
print("Wrote three exact point pairs and figures; reflection applied twice restores each point.")
