"""Lecture 3, the two velocity angles. Left: the vertical plane that contains the velocity, with the flight-path
angle gamma between V and the horizon, and its parts V cos(gamma) along the horizon and V sin(gamma) up. Right:
the horizontal plane seen from above, with the heading chi measured clockwise from north to the horizontal part,
split into north and east. The parts are computed with the engine (frames.ned_from_angles).
Writes img/fig-03-angles.png."""
import numpy as np
from _visual import plt, save, INK, MAROON, BLUE, GREEN, AMBER, GREY
from flight.frames import ned_from_angles

V, GAM, CHI = 1.0, np.radians(25), np.radians(60)
n, e, d = ned_from_angles(V, GAM, CHI)
hor = np.hypot(n, e)
kw = dict(arrowstyle="-|>", mutation_scale=18, lw=2.4)

if __name__ == "__main__":
    fig, (a1, a2) = plt.subplots(1, 2, figsize=(10.5, 4.6))
    # side view
    a1.plot([-0.1, 1.15], [0, 0], color=GREY, lw=1.2)
    a1.text(1.15, -0.1, "local horizon", ha="right", fontsize=11, color=GREY)
    a1.annotate("", xy=(hor, -d), xytext=(0, 0), arrowprops=dict(color=MAROON, **kw))
    a1.text(hor + 0.02, -d + 0.02, "$\\mathbf{V}$", fontsize=15, color=MAROON)
    a1.annotate("", xy=(hor, 0), xytext=(0, 0), arrowprops=dict(color=BLUE, **kw))
    a1.annotate("", xy=(hor, -d), xytext=(hor, 0), arrowprops=dict(color=GREEN, **kw))
    a1.text(hor / 2, -0.1, "$V\\cos\\gamma$", ha="center", fontsize=13, color=BLUE)
    a1.text(hor + 0.04, -d / 2, "$V\\sin\\gamma$\nrate of climb", fontsize=13, color=GREEN, va="center")
    arc = np.linspace(0, GAM, 30)
    a1.plot(0.32 * np.cos(arc), 0.32 * np.sin(arc), color=INK, lw=1)
    a1.text(0.36 * np.cos(GAM / 2), 0.36 * np.sin(GAM / 2), "$\\gamma$", fontsize=15, va="center")
    a1.set_title("side view: the flight-path angle", fontsize=12)
    a1.set_xlim(-0.15, 1.45)
    a1.set_ylim(-0.25, 0.75)
    # top view: x east, y north
    a2.annotate("", xy=(0, 1.0), xytext=(0, 0), arrowprops=dict(color=GREY, arrowstyle="-|>", lw=1.2))
    a2.text(0.03, 1.0, "N", fontsize=12, color=GREY)
    a2.annotate("", xy=(1.0, 0), xytext=(0, 0), arrowprops=dict(color=GREY, arrowstyle="-|>", lw=1.2))
    a2.text(1.0, 0.03, "E", fontsize=12, color=GREY)
    a2.annotate("", xy=(e, n), xytext=(0, 0), arrowprops=dict(color=BLUE, **kw))
    a2.text(e + 0.03, n + 0.02, "$V\\cos\\gamma$", fontsize=13, color=BLUE)
    a2.plot([0, e], [n, n], color=AMBER, lw=1.4, ls="--")
    a2.plot([e, e], [0, n], color=AMBER, lw=1.4, ls="--")
    a2.text(-0.06, n, "$V\\cos\\gamma\\cos\\chi$", ha="right", va="center", fontsize=12, color=AMBER)
    a2.text(e, -0.08, "$V\\cos\\gamma\\sin\\chi$", ha="center", va="top", fontsize=12, color=AMBER)
    arc = np.linspace(np.pi / 2, np.pi / 2 - CHI, 30)
    a2.plot(0.3 * np.cos(arc), 0.3 * np.sin(arc), color=INK, lw=1)
    a2.text(0.33 * np.cos(np.pi / 2 - CHI / 2), 0.33 * np.sin(np.pi / 2 - CHI / 2), "$\\chi$", fontsize=15)
    a2.set_title("top view: the heading, clockwise from north", fontsize=12)
    a2.set_xlim(-0.75, 1.25)
    a2.set_ylim(-0.25, 1.15)
    for a in (a1, a2):
        a.set_aspect("equal")
        a.axis("off")
    save(fig, "fig-03-angles")
