import numpy as np
import matplotlib.pyplot as plt
import os

os.makedirs("img", exist_ok=True)

fig, ax = plt.subplots(figsize=(7.5, 4.6))

A = np.array([0.0, 0.0])
B = np.array([4.0, 2.0])
t = np.linspace(0, 1, 200)
line = np.outer(1 - t, A) + np.outer(t, B)

# Three distinct paths from A to B
p1 = line.copy()                                              # straight
p2 = line.copy(); p2[:, 1] += 1.6 * np.sin(np.pi * t)         # arc above
p3 = line.copy(); p3[:, 1] -= 1.2 * np.sin(np.pi * t) * (1 + 0.4 * np.sin(3 * np.pi * t))  # wiggle below

ax.plot(p1[:, 0], p1[:, 1], color="#2563eb", lw=2.2)
ax.plot(p2[:, 0], p2[:, 1], color="#16a34a", lw=2.2)
ax.plot(p3[:, 0], p3[:, 1], color="#dc2626", lw=2.2)

# labels anchored to each curve so they follow the tilted line
mid = 0.5 * (A + B)
i2 = int(np.argmax(p2[:, 1])); i3 = int(np.argmin(p3[:, 1]))
ax.text(mid[0], mid[1] - 0.45, "Path 1", color="#2563eb", fontsize=12, ha="center")
ax.text(p2[i2, 0], p2[i2, 1] + 0.28, "Path 2", color="#16a34a", fontsize=12, ha="center")
ax.text(p3[i3, 0], p3[i3, 1] - 0.35, "Path 3", color="#dc2626", fontsize=12, ha="center")

for pt, name, off in [(A, "A", (-0.30, 0.12)), (B, "B", (0.14, 0.12))]:
    ax.scatter(*pt, s=90, color="black", zorder=5)
    ax.text(pt[0] + off[0], pt[1] + off[1], name, fontsize=14, weight="bold")

y_bottom = p3[:, 1].min() - 0.9
ax.text(mid[0], y_bottom, r"$W_1 = W_2 = W_3 = \Phi(A) - \Phi(B)$",
        fontsize=13, ha="center", color="#2f2a24")

ax.set_xlim(-0.8, 4.8)
ax.set_ylim(y_bottom - 0.4, p2[:, 1].max() + 0.6)
ax.set_aspect("equal")
ax.axis("off")
plt.tight_layout()
fig.savefig("img/fig-conservative-paths.png", dpi=160, bbox_inches="tight")
plt.close(fig)
