"""Lecture 36: the forces and moment that make the pitching moment about the centre of gravity: the wing-body lift and
moment at its aerodynamic centre, the tail lift at the tail's, the weight at the CG. Positions along the length are
those of Hull's business jet; arrow lengths are not to scale. Writes img/fig-36-forces.png."""
import numpy as np
from matplotlib.patches import FancyArrowPatch
from _visual import plt, save, MAROON, BLUE, GREEN, AMBER, INK, GREY
import _stab as S
import _airplane as AP

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 4.4))
    xw, xt, xc = S.WING.x_ac, S.TAIL.x_ac, 0.44       # CG drawn aft of its true 0.30 to separate the arrows, still ahead of x_np
    AP.body(ax, xw, xt)
    AP.arrow(ax, xw, 0.0, 1.0, BLUE, "$L_{wb}$", va="bottom", dx=-0.1)
    AP.arrow(ax, xt, 0.30, 0.5, GREEN, "$L_H$")
    AP.arrow(ax, xc, -0.02, -0.85, MAROON, "$W$")
    ax.add_patch(FancyArrowPatch((xw - 0.32, -0.28), (xw - 0.32, 0.30), connectionstyle="arc3,rad=-0.7", arrowstyle="-|>",
                                 mutation_scale=15, color=AMBER, lw=2.2, zorder=5))
    ax.text(xw - 0.85, 0.34, "$M_{ac}$\n(drawn nose up)", color=AMBER, fontsize=11, ha="center")
    AP.cg_mark(ax, xc, label="")
    ax.text(xc + 0.1, 0.34, "CG", fontsize=12)
    for x, lab, y in ((xw, "$x_{ac}$", -0.62), (xc, "$x_{cg}$", -1.07), (xt, "$x_{ac,H}$", -0.62)):
        ax.plot([x, x], [-0.04, y], color=GREY, lw=0.8, ls=":")
        ax.text(x, y - 0.03, lab, ha="center", va="top", fontsize=12)
    ax.set_xlim(-1.9, xt + 1.1); ax.set_ylim(-1.35, 1.35); ax.set_aspect("equal"); ax.axis("off")
    save(fig, "fig-36-forces")
