"""Lecture 6, how fast the ground falls away below a flat plane: the drop of the Earth's surface below the
horizontal plane through the start, R_E (1 - cos(s/R_E)), against the distance s along the ground, with the
small-distance form s^2/(2 R_E). Writes img/fig-06-drop.png."""
import numpy as np
from _visual import plt, save, despine, BLUE, MAROON, GREY, INK
from flight.constants import A_EARTH as R

S = np.linspace(0, 2000e3, 300)
DROP = R * (1 - np.cos(S / R))
MARKS = [("BrahMos range, 290 km", 290e3), ("1000 km", 1000e3), ("2000 km", 2000e3)]

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 4.8))
    ax.plot(S / 1e3, DROP / 1e3, color=BLUE, lw=2.4, label="$R_E(1-\\cos(s/R_E))$")
    ax.plot(S / 1e3, S**2 / (2 * R) / 1e3, color=MAROON, lw=1.6, ls="--", label="$s^2/(2R_E)$")
    for lab, s in MARKS:
        d = R * (1 - np.cos(s / R))
        ax.plot(s / 1e3, d / 1e3, "o", color=INK)
        right = s < 500e3
        ax.annotate(f"{lab}: {d / 1e3:.1f} km", (s / 1e3, d / 1e3), xytext=(12 if right else -10, 12), textcoords="offset points",
                    fontsize=11, ha="left" if right else "right")
    ax.set_xlabel("distance along the ground $s$ (km)")
    ax.set_ylabel("drop below the starting plane (km)")
    ax.legend(fontsize=11, frameon=False, loc="upper left")
    despine(ax)
    save(fig, "fig-06-drop")
