"""Lecture 38: schematics for the pull-up. Left: the aircraft at the bottom of a circular pull-up, with lift larger
than weight and the nose turning at the rate q. Right: because the aircraft rotates about its CG, the tail moves
down, so the air reaches it from further below and its angle of attack grows. Writes img/fig-38-pullup.png."""
import numpy as np
from matplotlib.patches import Arc, Polygon, Ellipse
from _visual import plt, save, MAROON, BLUE, GREEN, AMBER, INK, GREY


def plane(ax, x, y, s=1.0):
    ax.add_patch(Polygon([(x - s, y), (x - 0.65 * s, y + 0.12 * s), (x + 0.75 * s, y + 0.07 * s), (x + 0.95 * s, y + 0.34 * s),
                          (x + 1.05 * s, y + 0.34 * s), (x + 1.0 * s, y), (x - 0.65 * s, y - 0.11 * s)],
                         closed=True, fc="#e7e1cf", ec=GREY, lw=1.2, zorder=3))
    ax.plot(x - 0.1 * s, y, "o", ms=9, mfc="#faf8f2", mec=INK, mew=1.5, zorder=5)


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.2), gridspec_kw=dict(width_ratios=[1, 1.5]))
    R = 3.4
    t = np.radians(np.linspace(225, 315, 100))
    a.plot(R * np.cos(t), R + R * np.sin(t), color=INK, lw=1.4, ls="--")
    plane(a, 0.1, 0.0, 0.9)
    arrow(a, (0.0, 0.05), (0.0, 1.9), BLUE); a.text(0.1, 1.85, "$L = nW$", color=BLUE, fontsize=13)
    arrow(a, (0.0, -0.05), (0.0, -1.0), MAROON); a.text(0.1, -1.0, "$W$", color=MAROON, fontsize=13)
    arrow(a, (-1.0, 0.0), (-2.1, 0.0), INK, 1.8); a.text(-2.05, 0.15, "$V$", fontsize=13)
    a.plot([0, 0], [1.9, R], color=GREY, lw=0.9, ls=":"); a.plot(0, R, "x", color=INK, ms=8)
    a.text(0.12, R - 0.05, "centre of the path", fontsize=11)
    a.add_patch(Arc((0, 0), 2.6, 2.6, theta1=100, theta2=150, color=AMBER, lw=2.4))
    arrow(a, (1.3 * np.cos(np.radians(108)), 1.3 * np.sin(np.radians(108))), (1.3 * np.cos(np.radians(99)), 1.3 * np.sin(np.radians(99))), AMBER)
    a.text(-1.35, 1.15, "$q$", color=AMBER, fontsize=14)
    a.set_xlim(-2.4, 2.6); a.set_ylim(-1.3, 3.7); a.set_aspect("equal"); a.axis("off")
    a.set_title("bottom of a pull-up", fontsize=12)

    b.add_patch(Ellipse((0.0, 0.0), 0.9, 0.12, fc="#d9d1bb", ec=INK, lw=1.2))
    b.text(-0.15, 0.2, "wing", ha="center", fontsize=11)
    b.plot(0.35, 0, "o", ms=11, mfc="#faf8f2", mec=INK, mew=1.6, zorder=5); b.text(0.35, -0.36, "CG", ha="center", fontsize=11)
    b.add_patch(Ellipse((3.4, 0.0), 0.6, 0.09, fc="#d9d1bb", ec=INK, lw=1.2))
    b.text(3.4, 0.2, "tail", ha="center", fontsize=11)
    arrow(b, (3.4, -0.08), (3.4, -0.85), AMBER)
    b.text(3.55, -0.75, "tail moves\ndown at $q\\,l$", color=AMBER, fontsize=12)
    b.annotate("", xy=(3.4, -1.55), xytext=(0.35, -1.55), arrowprops=dict(arrowstyle="<->", color=INK, lw=1.2))
    b.text(1.9, -1.85, "$l=(x_{ac,H}-x_{cg})\\,\\bar c$", ha="center", fontsize=12)
    start, O = np.array([0.2, 1.45]), np.array([3.0, 1.45])
    arrow(b, tuple(start), tuple(O), INK, 1.8); b.text(1.7, 1.17, "air from ahead, $V$", fontsize=11, ha="center")
    arrow(b, tuple(O), tuple(O + [0, 0.85]), AMBER, 1.8); b.text(3.12, 1.8, "$q\\,l$ upwards", color=AMBER, fontsize=11)
    arrow(b, tuple(start), tuple(O + [0, 0.85]), GREEN, 2.4)
    b.text(1.9, 2.55, r"what the tail meets: $\Delta\alpha_H\approx q\,l/V$", color=GREEN, fontsize=12, ha="center")
    b.set_xlim(-0.7, 5.0); b.set_ylim(-2.0, 2.8); b.set_aspect("equal"); b.axis("off")
    b.set_title("what the rotation does to the tail", fontsize=12)
    save(fig, "fig-38-pullup")
