"""Lecture 34, what the bank angle sets: the representative capsule with L/D = 0.3 flown at constant bank angles from 0
to 120 deg (_entry33.lifting, as in anim-34-bank), ending at 10 km height. Plotted: the point reached, as distance
along the starting direction and to the side of it (positive to the right), with the mirror image for banking the other
way; each point labelled with its bank and its largest sensed load. Writes img/fig-34-bank-reach.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, INK, GREY
import _entry33 as X
from flight.constants import R_EARTH

BANKS = [0, 15, 30, 45, 60, 75, 90, 105, 120]
PTS = {}
for bk in BANKS:
    tr = X.lifting(0.3, -2.0, float(bk))                     # heading east from the equator: south is to the right
    PTS[bk] = dict(along=R_EARTH * tr.lon[-1] / 1e3, right=-R_EARTH * tr.lat[-1] / 1e3, load=X.peaks(tr)["load"])
B_CROSS = max(BANKS, key=lambda b: PTS[b]["right"])

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 6))
    y = np.array([PTS[b]["right"] for b in BANKS]); x = np.array([PTS[b]["along"] for b in BANKS])
    for sgn in (1, -1):
        ax.plot(sgn * y, x, color=MAROON if sgn > 0 else GREY, lw=2.4, ls="-" if sgn > 0 else "--")
    ax.plot(y, x, "o", color=MAROON, ms=8)
    ax.plot(-y, x, "o", color=GREY, ms=6)
    for b in (0, 30, 45, 60, 75, 90, 105, 120):
        p = PTS[b]
        ax.text(p["right"] + (14 if b >= 90 else 8), p["along"] + {90: 12, 105: -28, 120: -45}.get(b, 25), f"{b}°: {p['load']:.1f} g", fontsize=11.5, color=INK)
    ax.text(-150, 2000, "banking left\n(mirror image)", fontsize=11.5, color=GREY, ha="right")
    ax.set_xlim(-330, 330); ax.set_ylim(1280, 2950)
    ax.set_xlabel("distance to the right of the starting direction (km)")
    ax.set_ylabel("distance along the starting direction (km)")
    despine(ax)
    save(fig, "fig-34-bank-reach")
