"""Lecture 24: the first 180 degrees of an instantaneous turn at each fighter's own corner speed at 3 km, 9 g or
C_Lmax (Tejas-class and F-16 models, _turns.py), speed held constant (it could not be held in flight: these are not
sustained turns). Dots every second. Writes img/fig-24-corner-plan.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, INK, GREY
import _turns as T
from flight import performance as PF

H = 3000.0
CRAFT = []
for name, v, m, c in (("Tejas-class model", T.F.TEJ, T.F.M_REF, MAROON),
                      ("F-16 model", T.F.F16, T.F.F16.info["vehicle"]["reference_mass"], BLUE)):
    vc = PF.corner_speed(T.fighter_stall(v, m, H), T.N_LIM_FIGHTER)
    rate, radius = PF.turn_rate_radius(vc, T.N_LIM_FIGHTER)
    CRAFT.append(dict(name=name, c=c, V=vc, rate=rate, R=radius, t180=np.pi / rate))

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 5.4))
    for cr in CRAFT:
        ph = np.linspace(0, np.pi, 300)
        ax.plot(cr["R"] * np.sin(ph), cr["R"] * (1 - np.cos(ph)), color=cr["c"], lw=2.6,
                label=f"{cr['name']}: {cr['V']:.0f} m/s, {np.degrees(cr['rate']):.1f}°/s, radius {cr['R']:.0f} m")
        for k in range(1, int(cr["t180"]) + 1):
            p = cr["rate"] * k; x, y = cr["R"] * np.sin(p), cr["R"] * (1 - np.cos(p))
            ax.plot(x, y, "o", color=cr["c"], ms=6); ax.text(x * 1.0 + (25 if cr["c"] == BLUE else -25), y, f"{k}", color=cr["c"], fontsize=11, ha="left" if cr["c"] == BLUE else "right", va="center")
        ax.plot(0, 2 * cr["R"], "s", color=cr["c"], ms=8)
        ax.text(-60, 2 * cr["R"], f"heading reversed after {cr['t180']:.1f} s", ha="right", va="center", fontsize=11.5, color=cr["c"])
    ax.annotate("", xy=(0, 0), xytext=(-800, 0), arrowprops=dict(arrowstyle="-|>", color=INK, lw=2))
    ax.text(-820, 0, "both enter here", ha="right", va="center", fontsize=11.5)
    ax.set_aspect("equal"); ax.set_xlim(-1900, 2300); ax.set_ylim(-200, 2.2 * max(c["R"] for c in CRAFT) + 500)
    ax.set_xlabel("distance (m)"); ax.set_ylabel("distance (m)")
    ax.legend(frameon=False, fontsize=11, loc="upper center", bbox_to_anchor=(0.5, -0.17))
    despine(ax)
    save(fig, "fig-24-corner-plan")
