"""Lecture 1, the forces on each kind of vehicle (schematic, not to scale): gravity W, lift L, drag D, thrust T.
Writes img/fig-01-forces.png."""
import numpy as np
from _visual import plt, save, INK, MAROON, BLUE, GREEN, AMBER, PURPLE, GREY

COL = dict(W=PURPLE, L=BLUE, D=MAROON, T=GREEN)
# vehicle: (title, flight-path angle deg, {force: (direction relative to the velocity in deg, length)})
CASES = [("glider", -3, dict(L=(90, 1.0), D=(180, 0.45), W=(None, 1.0))),
         ("airliner", 0, dict(L=(90, 1.0), D=(180, 0.5), T=(0, 0.5), W=(None, 1.0))),
         ("launch vehicle", 70, dict(T=(0, 1.3), D=(180, 0.35), W=(None, 0.8))),
         ("capsule", -10, dict(D=(180, 1.2), L=(90, 0.35), W=(None, 0.5))),
         ("satellite", 0, dict(W=(None, 0.8)))]

fig, axs = plt.subplots(2, 3, figsize=(10, 6.8))
axs = axs.ravel()
axs[-1].axis("off")
for ax, (name, gam, forces) in zip(axs, CASES):
    g = np.radians(gam)
    ev = np.array([np.cos(g), np.sin(g)])
    ax.annotate("", xy=0.9 * ev, xytext=(0, 0), arrowprops=dict(arrowstyle="-|>", color=GREY, lw=1.2, ls="--"))
    nv = np.array([-ev[1], ev[0]])
    ax.text(*(0.75 * ev + 0.17 * nv), "$V$", color=GREY, fontsize=12, ha="center", va="center")
    ax.plot([0], [0], "o", color=INK, ms=9)
    for f, (ang, ln) in forces.items():
        d = np.array([0, -1.0]) if ang is None else np.array([np.cos(g + np.radians(ang)), np.sin(g + np.radians(ang))])
        ax.annotate("", xy=ln * d, xytext=(0, 0), arrowprops=dict(arrowstyle="-|>", color=COL[f], lw=2.2, mutation_scale=14))
        side = np.array([-d[1], d[0]]) * (0.16 if f in ("T", "D") else 0.0)
        ax.text(*(ln * d * 1.15 - side), f"${f}$", color=COL[f], fontsize=13, ha="center", va="center")
    ax.set_title(name, fontsize=12)
    ax.set_xlim(-1.3, 1.5)
    ax.set_ylim(-1.35, 1.45)
    ax.set_aspect("equal")
    ax.axis("off")
save(fig, "fig-01-forces")
