"""Figure for complications-to-impulsive-transfers-chobotov.qmd (img/fig-ci-geometry.png).

(a) A burn that turns the velocity by the plane-change angle alpha as well as changing its size: the velocity triangle and the law of cosines.
(b) A Hohmann transfer with the plane change split between the two burns, seen along the line of apsides (the common axis of both rotations).
    The three orbit planes appear as lines through the centre. The two choices of sense give the total plane change alpha1 + alpha2 or |alpha1 - alpha2|.
The numbers are those of the solid-motor example: alpha1 = 3.27 deg, alpha2 = 23.26 deg.

Run from the sfm-slides directory:   python3 codes/fig-ci-geometry.py
"""
import numpy as np
import _figfont                                # all figure text at one scale
import matplotlib.pyplot as plt
from matplotlib.patches import Arc
from _manstyle import INK, MAROON, BLUE, GREEN, AMBER, PURPLE, despine
from _complic import *

r1, r2 = RE + 278, RE + 20187
h = hohmann(r1, r2)
dv1 = rocket_dv(4251, 2077, 300); dv2 = rocket_dv(1896, 998, 300)
a1 = plane_angle(h["vc1"], h["vp"], dv1); a2 = plane_angle(h["vc2"], h["va"], dv2)
fig, ax = plt.subplots(1, 2, figsize=(13.6, 6.0), gridspec_kw={"width_ratios": [1, 1.1]})
A = ax[0]
vc, vt, al = 3.87, 2.45, 23.26 * D2R           # second burn: V_c2, V_ATR, alpha2 (drawn to scale)
O = np.array([0.0, 0.0]); P_c = np.array([vc, 0.0]); P_t = vt * np.array([np.cos(al), np.sin(al)])
A.annotate("", xy=P_c, xytext=O, arrowprops=dict(arrowstyle="-|>", color=BLUE, lw=2.4))
A.annotate("", xy=P_t, xytext=O, arrowprops=dict(arrowstyle="-|>", color=MAROON, lw=2.4))
A.annotate("", xy=P_c, xytext=P_t, arrowprops=dict(arrowstyle="-|>", color=GREEN, lw=2.4))
A.text(vc * 0.55, -0.22, r"$V_{c2}$: circular speed, final orbit", color=BLUE, fontsize=11, ha="center")
A.text(P_t[0] * 0.40 - 0.50, P_t[1] * 0.55 + 0.04, r"$V_{ATR}$: speed in the transfer orbit", color=MAROON, fontsize=11, ha="right")
mid = 0.5 * (P_c + P_t)
A.text(mid[0] + 0.12, mid[1] + 0.02, r"$\Delta V_2$", color=GREEN, fontsize=13)
A.add_patch(Arc(O, 1.2, 1.2, theta1=0, theta2=23.26, color=INK, lw=1.4)); A.text(0.68, 0.14, r"$\alpha_2$", fontsize=13)
A.plot([0, 3.0], [0, 0], color=INK, lw=0.6, ls=":")
A.text(0.1, 1.55, r"$\Delta V_2^{\,2}=V_{c2}^{2}+V_{ATR}^{2}-2V_{c2}V_{ATR}\cos\alpha_2$", fontsize=11.5)
A.text(0.1, 1.28, "(a Hohmann arrival: the two velocities are both horizontal,\nthe angle between them is only the plane change)", fontsize=9.5, color=INK)
A.set_xlim(-0.2, 4.3); A.set_ylim(-0.5, 2.0); A.set_aspect("equal"); A.axis("off")
A.set_title("(a)  the velocity triangle of a burn that also changes the plane", fontsize=11.5, loc="left")
B = ax[1]
K = 2.2                                  # the angles are drawn K times too large so that they can be told apart
A1d, A2d = K * a1, K * a2
L = 1.0
def line(ang, c, lw):
    d = np.array([np.cos(ang), np.sin(ang)])
    B.plot([-L * d[0], L * d[0]], [-L * d[1], L * d[1]], color=c, lw=lw)
line(0.0, INK, 2.8); line(A2d, MAROON, 2.4); line(A2d + A1d, BLUE, 2.4); line(A2d - A1d, GREEN, 2.4)
B.add_patch(Arc(O, 1.0, 1.0, theta1=0, theta2=np.degrees(A2d), color=MAROON, lw=1.8)); B.text(0.40, 0.17, r"$\alpha_2$", color=MAROON, fontsize=13)
B.add_patch(Arc(O, 1.5, 1.5, theta1=np.degrees(A2d), theta2=np.degrees(A2d + A1d), color=BLUE, lw=1.8)); B.text(0.50, 0.66, r"$\alpha_1$", color=BLUE, fontsize=13)
B.add_patch(Arc(O, 1.5, 1.5, theta1=np.degrees(A2d - A1d), theta2=np.degrees(A2d), color=GREEN, lw=1.8)); B.text(0.70, 0.43, r"$\alpha_1$", color=GREEN, fontsize=13)
B.scatter([0], [0], color=INK, s=36, zorder=5)
yb = 1.28
B.text(1.30, 0.92, "parking plane, turns in the same sense\n" + r"$\theta_a=\alpha_1+\alpha_2=26.5^\circ$", color=BLUE, fontsize=10.5, va="center")
B.text(1.30, 0.55, "transfer plane", color=MAROON, fontsize=10.5, va="center")
B.text(1.30, 0.17, "parking plane, turns in opposite senses\n" + r"$\theta_b=|\alpha_1-\alpha_2|=20.0^\circ$", color=GREEN, fontsize=10.5, va="center")
B.text(1.30, -0.22, "final-orbit plane", color=INK, fontsize=10.5, va="center")
B.text(-1.45, -1.05, "View along the line of apsides. The two burns are at its two\nends, so both plane changes are turns about this one axis.\nAngles drawn %.1f times too large." % K, fontsize=9.5, ha="left")
B.set_xlim(-1.5, 3.4); B.set_ylim(-1.3, 1.25); B.set_aspect("equal"); B.axis("off")
B.set_title(r"(b)  two plane changes about one axis: they add, or subtract", fontsize=11.5, loc="left")
fig.tight_layout(); fig.savefig("img/fig-ci-geometry.png", dpi=150, bbox_inches="tight")
print("ok", a1 / D2R, a2 / D2R)
