"""Lecture 7, the forces in a steady glide and a steady climb (vertical plane, quasi-steady): in the glide,
L, D and W close a triangle and tan(gamma) = -1/E; in the climb the thrust T along the path pays for the drag and
for W sin(gamma). Angles from the engine's flight.performance (E = 6 and T/W = 0.4, E = 6, chosen large enough
to see). Writes img/fig-07-glide-climb.png."""
import numpy as np
from _visual import plt, save, INK, MAROON, BLUE, GREEN, PURPLE, GREY
from flight.performance import glide_angle, climb_angle

E = 6.0
CASES = [("steady glide, $T=0$", glide_angle(E), 0.0), ("steady climb", climb_angle(0.4, E), 0.4)]
kw = dict(arrowstyle="-|>", mutation_scale=16, lw=2.4)

if __name__ == "__main__":
    fig, axs = plt.subplots(1, 2, figsize=(10.5, 5.2))
    for ax, (title, g, tw) in zip(axs, CASES):
        ev = np.array([np.cos(g), np.sin(g)])
        nv = np.array([-np.sin(g), np.cos(g)])
        W = 1.0
        L = W * np.cos(g)
        D = L / E
        ax.plot(*np.array([-1.2 * ev, 1.3 * ev]).T, color=GREY, ls="--", lw=1)
        ax.text(*(-1.2 * ev + np.array([0.0, 0.08])), "path", color=GREY, fontsize=11)
        ax.plot([-1.3, 1.4], [0, 0], color=GREY, lw=0.8)
        for vec, lab, c in ((L * nv, "$L$", BLUE), (-D * ev, "$D$", MAROON), (np.array([0, -W]), "$W$", PURPLE)) + \
                (((tw * ev, "$T$", GREEN),) if tw else ()):
            ax.annotate("", xy=vec, xytext=(0, 0), arrowprops=dict(color=c, **kw))
            off = {"$D$": (-0.12, -0.14), "$T$": (0.02, 0.1)}.get(lab, (0.05, 0.0))
            ax.text(*(vec * 1.12 + np.array(off)), lab, color=c, fontsize=15)
        if tw == 0:                                                  # the closed triangle L + D + W = 0
            p0 = np.array([1.5, -0.35])
            pts = [p0, p0 + L * nv * 0.9, p0 + (L * nv - D * ev) * 0.9, p0 + (L * nv - D * ev + np.array([0, -W])) * 0.9]
            for a, b, c in zip(pts[:-1], pts[1:], (BLUE, MAROON, PURPLE)):
                ax.annotate("", xy=b, xytext=a, arrowprops=dict(color=c, arrowstyle="-|>", lw=1.6))
            ax.text(p0[0], p0[1] - 0.15, "the forces close:\n$L+D+W=0$", fontsize=11, color=INK, ha="center", va="top")
        a = np.linspace(min(0, g), max(0, g), 20)
        ax.plot(0.6 * np.cos(a), 0.6 * np.sin(a), color=INK, lw=1)
        ax.text(0.4, -0.42, f"$\\gamma$ = {np.degrees(g):.1f}°", fontsize=12)
        ax.set_title(title, fontsize=12)
        ax.set_xlim(-1.3, 2.0)
        ax.set_ylim(-1.25, 1.25)
        ax.set_aspect("equal")
        ax.axis("off")
    save(fig, "fig-07-glide-climb")
