"""Lecture 38: seen from behind, an aircraft banked 60 deg in a level turn. By the right-hand rule the turn rate of a right
turn is a vector pointing DOWN;
its parts along the axis out of the right wing (pitch rate q = chi_dot sin(sigma), positive nose-up) and along the
aircraft's own downward axis
(yaw rate chi_dot cos(sigma)). Schematic. Writes img/fig-38-turn-vector.png."""
import numpy as np
from _visual import plt, save, MAROON, BLUE, GREEN, INK, GREY

SIG = np.radians(60.0)
WING = np.array([np.cos(SIG), -np.sin(SIG)])            # towards the right (lower) wing tip
UP = np.array([np.sin(SIG), np.cos(SIG)])               # the aircraft's own upward direction


def parts(sig=SIG):
    """(pitch part, yaw part) of a unit turn rate at bank sig."""
    return np.sin(sig), np.cos(sig)


if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 5.2))
    L = 2.3
    ax.plot(*np.array([-L * WING, L * WING]).T, color=INK, lw=6, solid_capstyle="round")
    ax.add_patch(plt.Circle((0, 0), 0.28, facecolor="#e6e1d6", edgecolor=INK, lw=2, zorder=3))
    ax.plot(*np.array([0 * UP, 0.9 * UP]).T, color=INK, lw=3)
    Om = np.array([0.0, -2.6])
    p, y = parts()
    pitch_vec = 2.6 * p * WING; yaw_vec = -2.6 * y * UP
    for vec, c, lw in ((Om, MAROON, 3.4), (pitch_vec, BLUE, 2.8), (yaw_vec, GREEN, 2.8)):
        ax.annotate("", xy=vec, xytext=(0, 0), arrowprops=dict(arrowstyle="-|>", color=c, lw=lw, shrinkA=0, shrinkB=0), zorder=4)
    for vec in (pitch_vec, yaw_vec):
        ax.plot([vec[0], Om[0]], [vec[1], Om[1]], color=GREY, lw=1, ls=":")
    ax.text(0.0, -2.95, "turn rate $\\dot\\chi$ of a right turn (points down)", color=MAROON, fontsize=13, ha="center")
    ax.text(pitch_vec[0] + 0.15, pitch_vec[1], "along the right wing:\npitch rate, nose up\n$q=\\dot\\chi\\sin\\sigma$ = 0.87 $\\dot\\chi$", color=BLUE, fontsize=12.5, ha="left", va="center")
    ax.text(yaw_vec[0] - 0.15, yaw_vec[1], "along the aircraft's own downward axis:\nyaw rate, nose right\n$\\dot\\chi\\cos\\sigma$ = 0.50 $\\dot\\chi$", color=GREEN, fontsize=12.5, ha="right", va="center")
    ax.axhline(0, color=GREY, lw=0.8, ls="--", zorder=0)
    ax.text(2.0, -0.45, "$\\sigma$ = 60°", fontsize=13)
    ax.text(0, 2.55, "seen from behind, in a right turn", ha="center", fontsize=12)
    ax.set_xlim(-5.2, 5.0); ax.set_ylim(-3.2, 2.9); ax.set_aspect("equal"); ax.axis("off")
    save(fig, "fig-38-turn-vector")
