"""Sketches for the extension problems of orbital-elements-questions.qmd.

img/fig-ea-empty-focus.png (Problem 2(c)): the empty focus F* lies on the circle of radius 2a - r about the
body P, in the direction of r-hat reflected in the tangent line (equal angles with the tangent).
img/fig-ea-north-half.png (Problem 9): the part of the orbit north of the equator runs from the ascending
node to the descending node, i.e. from f = -omega to f = 180 deg - omega. For omega = 90 deg it is the
periapsis half (short), for omega = 270 deg the apoapsis half (long).

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

# ------------------------------------------------------------------ Problem 2(c)
a, e = 1.0, 0.6
p = a * (1 - e * e)
f = np.linspace(0, 2 * np.pi, 400)
r = p / (1 + e * np.cos(f))
fig, ax = plt.subplots(figsize=(7.4, 5.4))
ax.plot(r * np.cos(f), r * np.sin(f), color=BLUE, lw=2)
F, Fs = np.array([0.0, 0.0]), np.array([-2 * a * e, 0.0])
fP = np.radians(110)
rP = p / (1 + e * np.cos(fP)); P = rP * np.array([np.cos(fP), np.sin(fP)])
vdir = np.array([-np.sin(fP), e + np.cos(fP)]); vdir /= np.linalg.norm(vdir)
rhat = P / rP
d = 2 * (rhat @ vdir) * vdir - rhat
ax.plot(*np.array([P - 0.9 * vdir, P + 0.9 * vdir]).T, color=INK, lw=1, ls="--")
ax.text(*(P + 0.92 * vdir + np.array([0.02, 0])), "tangent ($\\hat{\\mathbf{v}}$)", fontsize=10)
ax.plot(*np.array([F, P]).T, color=GREEN, lw=1.8)
ax.plot(*np.array([P, P + (2 * a - rP) * d]).T, color=MAROON, lw=1.8)
ax.annotate("", xy=P + 0.45 * rhat, xytext=P, arrowprops=dict(arrowstyle="-|>", color=GREEN, lw=1.6))
ax.text(*(P + 0.48 * rhat), r"$\hat{\mathbf{r}}$", color=GREEN, fontsize=12)
th = np.linspace(0, 2 * np.pi, 300)
ax.plot(P[0] + (2 * a - rP) * np.cos(th), P[1] + (2 * a - rP) * np.sin(th), color=MAROON, lw=0.9, ls=":")
for X, lab, col in ((F, "$F$", INK), (Fs, "$F^*$", MAROON), (P, "$P$", BLUE)):
    ax.scatter(*X, color=col, s=36, zorder=4)
    ax.text(X[0] + 0.04, X[1] - 0.13, lab, color=col, fontsize=13)
ax.text(*(0.5 * (F + P) + np.array([0.06, 0.0])), "$r$", color=GREEN, fontsize=13)
ax.text(*(P + 0.55 * (2 * a - rP) * d + np.array([-0.2, 0.05])), "$2a-r$", color=MAROON, fontsize=13)
ax.text(P[0] + 0.62, P[1] + 0.95, "circle of radius $2a-r$ about $P$", color=MAROON, fontsize=10)
ax.set_aspect("equal"); ax.axis("off")
ax.set_title(r"Problem 2(c): $F^*$ is $2a-r$ from $P$, along $\hat{\mathbf{r}}$ reflected in the tangent", fontsize=11.5)
fig.tight_layout(); fig.savefig("img/fig-ea-empty-focus.png", dpi=150, bbox_inches="tight"); plt.close(fig)

# ------------------------------------------------------------------ Problem 9
tau_near = (np.arccos(e) - e * np.sqrt(1 - e * e)) / np.pi
fig, axs = plt.subplots(1, 2, figsize=(12, 4.8))
for ax, wd in zip(axs, (90, 270)):
    w = np.radians(wd)
    ax.plot(r * np.cos(f), r * np.sin(f), color="#9a9388", lw=1.2)
    fn = np.linspace(-w, np.pi - w, 300)
    rn = p / (1 + e * np.cos(fn))
    ax.plot(rn * np.cos(fn), rn * np.sin(fn), color=MAROON, lw=3, label="north of the equator")
    for fa, lab in ((-w, "ascending\nnode"), (np.pi - w, "descending\nnode")):
        ra = p / (1 + e * np.cos(fa))
        ax.scatter(ra * np.cos(fa), ra * np.sin(fa), color=AMBER, s=40, zorder=4)
        ax.text(ra * np.cos(fa) + 0.08, ra * np.sin(fa), lab, color=AMBER, fontsize=9, va="center")
    L = 2.4
    ax.plot([-L * np.cos(-w), L * np.cos(-w)], [-L * np.sin(-w), L * np.sin(-w)], color=AMBER, lw=1, ls="--")
    ax.scatter(0, 0, color=BLUE, s=60, zorder=4)
    ax.scatter(p / (1 + e), 0, color=INK, s=20); ax.text(p / (1 + e) + 0.05, -0.12, "periapsis", fontsize=9)
    frac = tau_near if wd == 90 else 1 - tau_near
    ax.set_title(rf"$\omega={wd}^\circ$: north for {100 * frac:.1f}% of the period ($e={e}$)", fontsize=11.5)
    ax.set_aspect("equal"); ax.axis("off"); ax.set_xlim(-2.9, 1.2)
axs[0].legend(frameon=False, fontsize=9, loc="lower left")
fig.suptitle("Problem 9: the north half runs from the ascending node to the descending node", fontsize=12)
fig.tight_layout(); fig.savefig("img/fig-ea-north-half.png", dpi=150, bbox_inches="tight"); plt.close(fig)
print(f"tau_near(e = 0.6) = {tau_near:.4f}; wrote img/fig-ea-empty-focus.png, img/fig-ea-north-half.png")
