"""Figure for two-body-questions.qmd (img/fig-tutorial-dependencies.png): what each quantity of the state-to-orbit
algorithm (Problem 4) really depends on.  The numbered steps are an order of computation, not a chain:
h and epsilon are independent, a needs only epsilon, and e can come either from the vector formula or from
h and epsilon (dashed).

Run from the sfm-slides directory:   python3 codes/fig-tutorial-dependencies.py
"""
import _figfont                                # all figure text at one scale
import matplotlib.pyplot as plt
from _manstyle import INK, MAROON, BLUE, GREEN, AMBER, PURPLE

N = {"rv": (0.0, 2.0, r"$(\mathbf{r},\mathbf{v})$", INK),
     "mag": (1.6, 3.2, r"$r,\ v$", BLUE), "rdotv": (1.6, 0.6, r"$\mathbf{r}\cdot\mathbf{v}$", BLUE),
     "h": (1.6, 2.0, r"$\mathbf{h},\ h$", GREEN),
     "eps": (3.4, 3.2, r"$\varepsilon$", GREEN), "e": (3.4, 1.5, r"$\mathbf{e},\ e$", GREEN),
     "p": (4.3, 2.55, r"$p$", MAROON), "a": (5.2, 3.2, r"$a$", MAROON),
     "rp": (5.2, 1.9, r"$r_p$", MAROON), "gam": (5.2, 0.6, r"$\gamma$", MAROON)}
E = [("rv", "mag"), ("rv", "rdotv"), ("rv", "h"), ("mag", "eps"), ("h", "p"), ("eps", "a"),
     ("rv", "e"), ("h", "e"), ("p", "rp"), ("e", "rp"), ("rdotv", "gam"), ("h", "gam")]
D = [("eps", "e")]                                      # e = sqrt(1 + 2 eps h^2 / mu^2), with h

fig, ax = plt.subplots(figsize=(10, 4.8))
for k, (x, y, lab, col) in N.items():
    ax.text(x, y, lab, ha="center", va="center", fontsize=15, color=col,
            bbox=dict(boxstyle="round,pad=0.35", facecolor="#fcfbf7", edgecolor=col, lw=1.4))
for (a, b), ls in [(e, "-") for e in E] + [(d, (0, (4, 3))) for d in D]:
    xa, ya = N[a][:2]; xb, yb = N[b][:2]
    ax.annotate("", xy=(xb, yb), xytext=(xa, ya),
                arrowprops=dict(arrowstyle="-|>", color="#6f685e", lw=1.3, ls=ls, shrinkA=22, shrinkB=22,
                                connectionstyle={("rv", "e"): "arc3,rad=0.12", ("eps", "e"): "arc3,rad=-0.35"}.get((a, b), "arc3,rad=0")))
ax.text(1.6, 0.05, "inputs", color=BLUE, ha="center", fontsize=10)
ax.text(0.0, 1.35, "state", color=INK, ha="center", fontsize=10)
ax.text(3.4, 3.75, "integrals of motion", color=GREEN, ha="center", fontsize=10)
ax.text(5.2, 3.75, "geometry", color=MAROON, ha="center", fontsize=10)
ax.text(5.9, 1.25, "dashed: $e$ also follows from $h$ and $\\varepsilon$", color="#6f685e", fontsize=9.5, ha="right")
ax.set_xlim(-0.6, 6.0); ax.set_ylim(-0.3, 4.0); ax.axis("off")
ax.set_title("What each quantity needs: the order of the steps is not a chain", fontsize=12)
fig.tight_layout()
fig.savefig("img/fig-tutorial-dependencies.png", dpi=150, bbox_inches="tight")
print("wrote img/fig-tutorial-dependencies.png")
