"""Lecture 37: Hull's business jet, CG at 0.30: the pitching-moment line for several elevator angles (trailing edge
down positive). The elevator shifts the line without changing its slope, which moves the trim angle.
Writes img/fig-37-elevator.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREEN, AMBER, INK
import _stab as S
from flight import stability as ST
from flight.constants import DEG

A = np.linspace(-4, 14, 100)
DE = (5.0, 0.0, -5.0, -10.0)
CM0, CMA, CMD = ST.moment_coefficients(S.XCG, S.WING, S.TAIL, S.THRUST)

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 4.8))
    for d, c in zip(DE, (AMBER, INK, BLUE, GREEN)):
        ax.plot(A, CM0 + CMD * d * DEG + CMA * A * DEG, color=c, lw=2.4, label=rf"$\delta_e={d:+.0f}$°" if d else r"$\delta_e=0$°")
        ax.plot(ST.trim_angle(CM0 + CMD * d * DEG, CMA) / DEG, 0, "o", color=c, ms=9)
    ax.axhline(0, color=INK, lw=0.9); ax.axvline(0, color=INK, lw=0.9)
    ax.set_xlabel("angle of attack (°)"); ax.set_ylabel("$C_m$"); ax.set_xlim(-4, 14)
    ax.legend(frameon=False, fontsize=11, loc="upper right")
    despine(ax)
    save(fig, "fig-37-elevator")
