"""Lecture 39, a schematic of why a dropped wing can come back, in three views from behind (not a simulation: the course
has no lateral dynamics, and the arrows show only the sense of the first response). (1) A gust banks the aircraft right
wing down; the air still comes from ahead and there is no rolling moment. (2) The tilted lift and the weight leave a
sideways force, so the aircraft slips towards the low wing and meets air from the right (beta > 0). (3) With dihedral
the low wing then meets the air at a larger angle of attack than the high wing, and the difference in lift rolls the
wings back. Bank and dihedral exaggerated. Writes img/fig-39-wing-drop.png."""
import numpy as np
from matplotlib.patches import Ellipse, Arc
from _visual import plt, save, MAROON, BLUE, GREEN, AMBER, INK, GREY

BANK, DIHEDRAL = np.radians(25), np.radians(12)           # drawing angles, exaggerated


def rot(p):
    """Rotate a body-axes point (right, up) by the bank, right wing down (clockwise seen from behind)."""
    c, s = np.cos(BANK), np.sin(BANK)
    return np.array([c * p[0] + s * p[1], -s * p[0] + c * p[1]])


TIPS = [rot(np.array([sx * 1.6, 1.6 * np.tan(DIHEDRAL)])) for sx in (-1, 1)]       # left tip, right tip
UP = rot(np.array([0.0, 1.0]))


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


def aircraft(ax):
    for tip in TIPS:
        ax.plot([0, tip[0]], [0, tip[1]], color=INK, lw=5, solid_capstyle="round", zorder=3)
    fin = rot(np.array([0.0, 0.55]))
    ax.plot([0, fin[0]], [0, fin[1]], color=INK, lw=3, zorder=3)
    ax.add_patch(Ellipse((0, 0), 0.42, 0.42, facecolor="#d9d1bb", edgecolor=INK, lw=1.4, zorder=4))
    ax.plot([-1.9, 1.9], [0, 0], color=GREY, lw=0.8, ls="--", zorder=1)


if __name__ == "__main__":
    fig, axs = plt.subplots(1, 3, figsize=(10.5, 4.9))
    fig.subplots_adjust(wspace=0.04, left=0.01, right=0.99)
    titles = ["1. a gust banks it", "2. it slips sideways", "3. dihedral rolls it back"]
    notes = ["the air comes from ahead:\nno rolling moment", "the tilted lift no longer\nholds up all the weight", "the low wing meets the\nair at a larger angle"]
    for ax, t, n in zip(axs, titles, notes):
        aircraft(ax)
        ax.set_title(t, fontsize=12)
        ax.text(0, -2.05, n, ha="center", va="top", fontsize=11)
        ax.set_xlim(-2.1, 2.1); ax.set_ylim(-2.7, 2.1); ax.set_aspect("equal"); ax.axis("off")
    for ax in axs[:2]:
        arrow(ax, (0, 0), tuple(1.7 * UP), BLUE); ax.text(1.7 * UP[0] + 0.08, 1.7 * UP[1], "lift", color=BLUE, fontsize=12)
        arrow(ax, (0, 0), (0, -1.55), MAROON); ax.text(-0.12, -1.5, "weight", color=MAROON, fontsize=12, ha="right")
    b = axs[1]
    arrow(b, (0.3, -0.55), (1.5, -0.55), AMBER); b.text(0.95, -0.9, "slip", color=AMBER, fontsize=12, ha="center")
    arrow(b, (2.05, 0.45), (1.25, 0.45), GREEN); b.text(1.75, 0.62, "wind\n$\\beta>0$", color=GREEN, fontsize=11.5, ha="center")
    c = axs[2]
    arrow(c, (2.1, -0.25), (1.65, -0.25), GREEN); c.text(1.9, -0.65, "wind", color=GREEN, fontsize=11.5, ha="center")
    for tip, length, lab in ((TIPS[0], 0.55, "less\nlift"), (TIPS[1], 1.5, "more\nlift")):
        base = 0.72 * tip
        end = base + length * UP
        arrow(c, tuple(base), tuple(end), BLUE)
        c.text(end[0] + 0.05, end[1] + 0.08, lab, color=BLUE, fontsize=11.5, ha="center", va="bottom")
    c.add_patch(Arc((0, 0), 2.7, 2.7, theta1=-70, theta2=-20, color=MAROON, lw=2.8))
    a0, a1 = np.radians(-30), np.radians(-17)
    arrow(c, (1.35 * np.cos(a0), 1.35 * np.sin(a0)), (1.35 * np.cos(a1), 1.35 * np.sin(a1)), MAROON)
    c.text(-0.8, -1.25, "rolling moment\nlifts the low wing", color=MAROON, fontsize=11, ha="center")
    save(fig, "fig-39-wing-drop")
