"""Number-free sketches for plane-changes.qmd: the two planes and their line of nodes, the velocity
triangle of a pure turn, and the triangle of a turn combined with a change of speed.
Run from sfm-slides/."""
import numpy as np
import _figfont                                # all figure text at one scale
import matplotlib.pyplot as plt
from _manstyle import INK, MAROON, BLUE, GREEN, AMBER


def arrow(ax, p, q, col, lw=2.6, ms=16):
    ax.annotate("", xy=q, xytext=p, arrowprops=dict(arrowstyle="-|>", color=col, lw=lw, shrinkA=0, shrinkB=0,
                                                    mutation_scale=ms))


def save(fig, name):
    fig.tight_layout()
    fig.savefig(f"img/{name}.png", dpi=160, bbox_inches="tight")
    plt.close(fig)
    print(f"wrote img/{name}.png")


# ---- two orbit planes and the line where they meet --------------------------
az, el = np.radians(-62.0), np.radians(24.0)


def view(v):
    """Oblique view of a 3-D point, as (x, y) on the page."""
    x, y, z = v
    return np.array([x*np.cos(az) + y*np.sin(az),
                     (-x*np.sin(az) + y*np.cos(az))*np.sin(el) + z*np.cos(el)])


def on_plane(th, tilt):
    """Unit circle in a plane turned by tilt about the x axis, which is the line of nodes."""
    return np.array([np.cos(th), np.sin(th)*np.cos(tilt), np.sin(th)*np.sin(tilt)])


fig, ax = plt.subplots(figsize=(7.6, 5.4))
tilt = np.radians(32.0)
th = np.linspace(0, 2*np.pi, 500)
for t, col in ((0.0, BLUE), (tilt, GREEN)):
    pts = np.array([view(on_plane(a, t)) for a in th])
    ax.plot(pts[:, 0], pts[:, 1], color=col, lw=2.6)
# line of nodes, extended beyond the orbits
a, b = view((-1.28, 0, 0)), view((1.28, 0, 0))
ax.plot([a[0], b[0]], [a[1], b[1]], color=INK, lw=1.2, ls="--")
ax.plot(*view((0, 0, 0)), "o", color=INK, ms=8)
for x in (1.0, -1.0):
    ax.plot(*view((x, 0, 0)), "o", color=AMBER, ms=11, zorder=6)
# the velocities at one node, and the angle between them
node = np.array((1.0, 0, 0))
L = 0.62
v1, v2 = node + L*np.array((0, 1, 0)), node + L*np.array((0, np.cos(tilt), np.sin(tilt)))
arrow(ax, view(node), view(v1), BLUE)
arrow(ax, view(node), view(v2), GREEN)
arc = np.array([view(node + 0.36*np.array((0, np.cos(s), np.sin(s)))) for s in np.linspace(0, tilt, 30)])
ax.plot(arc[:, 0], arc[:, 1], color=INK, lw=1.2)
lab = view(node + 0.46*np.array((0, np.cos(tilt/2), np.sin(tilt/2))))
ax.text(lab[0] + 0.015, lab[1], r"$\Delta i$", fontsize=14, va="center", ha="left")
tip1, tip2 = view(v1), view(v2)
ax.text(tip1[0] - 0.03, tip1[1] + 0.01, r"$\mathbf{v}_1$", color=BLUE, fontsize=14, va="center", ha="right")
ax.text(tip2[0] + 0.03, tip2[1] + 0.02, r"$\mathbf{v}_2$", color=GREEN, fontsize=14, va="center")
p = view(on_plane(np.radians(215), 0.0))
ax.text(p[0] - 0.06, p[1] - 0.03, "initial orbit", color=BLUE, fontsize=12.5, ha="right", va="top")
ax.text(-0.97, 0.62, "final orbit", color=GREEN, fontsize=12.5, ha="center", va="center")
ax.text(a[0] - 0.03, a[1], "line of nodes", fontsize=12, ha="right", va="center")
n2 = view((-1.0, 0, 0))
ax.text(n2[0] - 0.05, n2[1] + 0.07, "node", color=AMBER, fontsize=12, ha="right", va="bottom")
n1 = view(node)
ax.text(n1[0] - 0.056, n1[1] - 0.14, "node", color=AMBER, fontsize=12, ha="left", va="top")
ax.set_aspect("equal"); ax.axis("off")
save(fig, "fig-man-plane-nodes")

# ---- a turn at constant speed ------------------------------------------------
fig, ax = plt.subplots(figsize=(5.8, 4.2))
ang = np.radians(38.0)
A, B = np.array((1.0, 0.0)), np.array((np.cos(ang), np.sin(ang)))
arrow(ax, (0, 0), A, BLUE)
arrow(ax, (0, 0), B, GREEN)
arrow(ax, A, B, AMBER)
s = np.linspace(0, ang, 30)
ax.plot(0.3*np.cos(s), 0.3*np.sin(s), color=INK, lw=1.2)
ax.text(0.36*np.cos(ang/2), 0.36*np.sin(ang/2), r"$\Delta i$", fontsize=14, va="center")
ax.text(0.5, -0.07, r"$\mathbf{v}_1$, length $v$", color=BLUE, fontsize=13, ha="center", va="top")
ax.text(0.42*np.cos(ang) - 0.05, 0.42*np.sin(ang) + 0.06, r"$\mathbf{v}_2$, length $v$", color=GREEN, fontsize=13,
        ha="right", va="bottom")
m = (A + B)/2
ax.text(m[0] + 0.05, m[1], r"$\Delta\mathbf{v}$", color=AMBER, fontsize=14, va="center")
ax.set_xlim(-0.45, 1.3); ax.set_ylim(-0.2, 0.75)
ax.set_aspect("equal"); ax.axis("off")
save(fig, "fig-man-plane-turn")

# ---- a turn combined with a change of speed ----------------------------------
fig, ax = plt.subplots(figsize=(6.4, 4.2))
ang = np.radians(30.0)
u, w = 0.55, 1.1                              # the two speeds, in no particular units
P, Q = np.array((u, 0.0)), w*np.array((np.cos(ang), np.sin(ang)))
arrow(ax, (0, 0), P, BLUE)
arrow(ax, (0, 0), Q, GREEN)
arrow(ax, P, Q, AMBER)
s = np.linspace(0, ang, 30)
ax.plot(0.26*np.cos(s), 0.26*np.sin(s), color=INK, lw=1.2)
ax.text(0.31*np.cos(ang/2), 0.31*np.sin(ang/2), r"$\Delta i$", fontsize=14, va="center")
ax.text(u/2, -0.05, r"$\mathbf{v}_{t2}$", color=BLUE, fontsize=14, ha="center", va="top")
ax.text(0.5*Q[0] - 0.03, 0.5*Q[1] + 0.04, r"$\mathbf{v}_{c2}$", color=GREEN, fontsize=14, ha="right", va="bottom")
m = (P + Q)/2
ax.text(m[0] + 0.05, m[1] - 0.02, r"$\Delta\mathbf{v}_2$", color=AMBER, fontsize=14, va="center")
ax.set_xlim(-0.2, 1.3); ax.set_ylim(-0.16, 0.66)
ax.set_aspect("equal"); ax.axis("off")
save(fig, "fig-man-plane-combined")
