"""Lecture 4, two ways to split the same aerodynamic force on a slender body at angle of attack alpha: along and
across the body (axial A, normal N) or along and across the velocity (drag D, lift L). Coefficients C_N = 0.20,
C_A = 0.30 at alpha = 12 deg (alpha enlarged for the drawing). Writes img/fig-04-cn-ca.png."""
import numpy as np
from _visual import plt, save, INK, MAROON, BLUE, GREEN, AMBER, GREY
from matplotlib.patches import Polygon

AL = np.radians(12.0)
CN, CA = 0.20, 0.30
CL = CN * np.cos(AL) - CA * np.sin(AL)
CD = CN * np.sin(AL) + CA * np.cos(AL)
kw = dict(arrowstyle="-|>", mutation_scale=18, lw=2.4)

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 5.2))
    b = np.array([np.cos(AL), np.sin(AL)])
    nb = np.array([-np.sin(AL), np.cos(AL)])
    body = np.array([-1.6 * b - 0.12 * nb, 1.2 * b - 0.12 * nb, 1.6 * b, 1.2 * b + 0.12 * nb, -1.6 * b + 0.12 * nb])
    ax.add_patch(Polygon(body, color="#d9d2c3"))
    ax.annotate("", xy=(2.2, 0), xytext=(-2.2, 0), arrowprops=dict(color=GREY, arrowstyle="-|>", lw=1.4, ls="--"))
    ax.text(2.25, -0.05, "$\\mathbf{V}_a$", fontsize=14, color=GREY, va="center")
    s = 4.0
    F = s * (CN * nb - CA * b)
    ax.annotate("", xy=F, xytext=(0, 0), arrowprops=dict(color=INK, **kw))
    ax.text(*(F + np.array([-0.1, 0.07])), "total", fontsize=12, ha="right")
    for vec, lab, c, off in ((s * CN * nb, "$N$ normal", MAROON, (0.06, 0.0)), (-s * CA * b, "$A$ axial", MAROON, (-0.1, -0.2)),
                             (np.array([0, s * CL]), "$L$ lift", BLUE, (0.06, 0.0)), (np.array([-s * CD, 0]), "$D$ drag", BLUE, (-0.1, 0.08))):
        ax.annotate("", xy=vec, xytext=(0, 0), arrowprops=dict(color=c, **kw))
        ax.text(*(vec + np.array(off)), lab, color=c, fontsize=13, ha="right" if off[0] < 0 else "left")
    a = np.linspace(0, AL, 30)
    ax.plot(1.3 * np.cos(a), 1.3 * np.sin(a), color=INK, lw=1)
    ax.text(1.35 * np.cos(AL / 2), 1.35 * np.sin(AL / 2) - 0.04, "$\\alpha$", fontsize=15)
    ax.text(-2.2, -0.75, f"$C_N={CN:.2f}$, $C_A={CA:.2f}$ at $\\alpha={np.degrees(AL):.0f}^\\circ$:  "
                         f"$C_L={CL:.3f}$, $C_D={CD:.3f}$", fontsize=12)
    ax.set_xlim(-2.3, 2.6)
    ax.set_ylim(-0.85, 1.25)
    ax.set_aspect("equal")
    ax.axis("off")
    save(fig, "fig-04-cn-ca")
