"""Lecture 39: the lateral angles and moments with their signs. Left: view from above, sideslip beta with the wind
from the right, and the fin's side force turning the nose into the wind. Right: view from behind, a wing with
dihedral and the sideways velocity of a sideslip. Writes img/fig-39-angles.png."""
import numpy as np
from matplotlib.patches import Polygon, Arc, Ellipse, Circle
from _visual import plt, save, MAROON, BLUE, GREEN, AMBER, INK, GREY


def arrow(ax, p0, p1, c, lw=2.4):
    ax.annotate("", xy=p1, xytext=p0, arrowprops=dict(arrowstyle="-|>", color=c, lw=lw, shrinkA=0, shrinkB=0), zorder=6)


if __name__ == "__main__":
    fig, (a, b) = plt.subplots(1, 2, figsize=(11, 5.0), gridspec_kw=dict(width_ratios=[1, 1.2]))
    fus = [(0, 2.0), (0.17, 1.6), (0.2, 0.2), (0.07, -1.7), (-0.07, -1.7), (-0.2, 0.2), (-0.17, 1.6)]
    a.add_patch(Polygon(fus, closed=True, fc="#e7e1cf", ec=GREY, lw=1.2, zorder=2))
    for s in (1, -1):
        a.add_patch(Polygon([(s * 0.18, 0.65), (s * 1.8, 0.05), (s * 1.8, -0.2), (s * 0.18, 0.1)], closed=True, fc="#d9d1bb", ec=INK, lw=1.0, zorder=1))
        a.add_patch(Polygon([(s * 0.07, -1.2), (s * 0.7, -1.55), (s * 0.7, -1.7), (s * 0.07, -1.55)], closed=True, fc="#d9d1bb", ec=INK, lw=1.0, zorder=1))
    a.plot([0, 0], [-1.7, -1.1], color=INK, lw=4, zorder=3)
    a.plot(0, 0.3, "o", ms=10, mfc="#faf8f2", mec=INK, mew=1.6, zorder=7)
    a.plot([0, 0], [2.0, 3.1], color=GREY, ls="--", lw=1)
    beta = np.radians(28)
    tip = np.array([0.0, 2.05]); tail = tip + 1.5 * np.array([np.sin(beta), np.cos(beta)])
    arrow(a, tuple(tail), tuple(tip), BLUE); a.text(tail[0] + 0.1, tail[1] - 0.75, "relative\nwind", color=BLUE, fontsize=11)
    a.add_patch(Arc(tuple(tip), 1.7, 1.7, theta1=90 - 28, theta2=90, color=INK, lw=1.3))
    a.text(0.13, 3.05, r"$\beta>0$", fontsize=12)
    arrow(a, (-0.05, -1.42), (-1.15, -1.42), MAROON); a.text(-1.2, -1.28, "fin side force", color=MAROON, fontsize=11, ha="center")
    a.add_patch(Arc((0, 0.3), 1.9, 1.9, theta1=40, theta2=78, color=AMBER, lw=2.2))
    arrow(a, (0.95 * np.cos(np.radians(48)), 0.3 + 0.95 * np.sin(np.radians(48))), (0.95 * np.cos(np.radians(37)), 0.3 + 0.95 * np.sin(np.radians(37))), AMBER)
    a.text(0.85, 0.85, r"$N>0$" + "\n(nose right)", color=AMBER, fontsize=11)
    a.set_xlim(-2.1, 2.1); a.set_ylim(-2.0, 3.6); a.set_aspect("equal"); a.axis("off")
    a.text(-2.0, 2.55, "from above", fontsize=12)

    G = np.radians(14)
    for s in (1, -1):
        b.plot([s * 0.3, s * 2.4], [0, 2.1 * np.tan(G)], color=INK, lw=5, solid_capstyle="round")
    b.add_patch(Circle((0, 0), 0.32, fc="#e7e1cf", ec=GREY, lw=1.4, zorder=3))
    b.plot([0, 0], [0.32, 1.3], color=INK, lw=3)
    b.plot([-2.4, -1.3], [0, 0], color=GREY, ls=":", lw=1)
    b.add_patch(Arc((-0.3, 0), 3.4, 3.4, theta1=166, theta2=180, color=INK, lw=1.2)); b.text(-2.35, 0.12, r"$\Gamma$", fontsize=13)
    for yy in (-0.25, -0.7):
        arrow(b, (3.5, yy), (2.6, yy), BLUE, 2.0)
    b.text(3.05, -1.35, "sideways wind\nin a sideslip", color=BLUE, fontsize=11, ha="center")
    arrow(b, (1.55, 1.25 * np.tan(G) + 0.05), (1.55 - 1.15 * np.sin(G), 1.25 * np.tan(G) + 0.05 + 1.15 * np.cos(G)), GREEN)
    b.text(1.75, 1.45, "more lift", color=GREEN, fontsize=12)
    arrow(b, (-1.55, 1.25 * np.tan(G) + 0.05), (-1.55 + 0.55 * np.sin(G), 1.25 * np.tan(G) + 0.05 + 0.55 * np.cos(G)), GREY)
    b.text(-1.75, 1.15, "less lift", color=GREY, fontsize=12, ha="center")
    b.add_patch(Arc((0, 0), 2.2, 2.2, theta1=68, theta2=112, color=AMBER, lw=2.2))
    arrow(b, (1.1 * np.cos(np.radians(104)), 1.1 * np.sin(np.radians(104))), (1.1 * np.cos(np.radians(115)), 1.1 * np.sin(np.radians(115))), AMBER)
    b.text(0.0, 2.05, r"$C_l<0$ (rolls left)", color=AMBER, fontsize=12, ha="center")
    b.text(2.25, -0.02, "right wing", fontsize=11, ha="center")
    b.set_xlim(-2.8, 3.9); b.set_ylim(-1.5, 2.4); b.set_aspect("equal"); b.axis("off")
    b.set_title("from behind: a wing with dihedral", fontsize=12)
    save(fig, "fig-39-angles")
