"""Lecture 36: three straight pitching-moment curves against angle of attack: stable and trimmed at a positive angle,
stable but trimmed at a negative angle, and unstable. Schematic. Writes img/fig-36-two-slopes.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREEN, INK
from flight import stability as ST

LINES = [(0.06, -0.9, BLUE, "A: $C_{m0}>0$, falling"), (-0.05, -0.9, GREEN, "B: $C_{m0}<0$, falling"),
         (-0.02, 0.8, MAROON, "C: rising")]

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 4.6))
    a = np.linspace(-6, 10, 100)
    for c0, ca, c, lab in LINES:
        ax.plot(a, c0 + ca * np.radians(a), color=c, lw=2.6, label=lab)
        ax.plot(np.degrees(ST.trim_angle(c0, ca)), 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$ (nose up positive)"); ax.set_xlim(-6, 10); ax.set_ylim(-0.22, 0.2)
    fig.legend(frameon=False, fontsize=11, ncol=3, loc="upper center", bbox_to_anchor=(0.5, 0.0))
    despine(ax)
    save(fig, "fig-36-two-slopes")
