"""Q10: circle r1 = p and the target ellipse; the two tangential routes."""
import os, sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from _style import *

e = .3
p = 1.0
rp, ra = p/(1 + e), p/(1 - e)
fig, ax = plt.subplots(figsize=(9.5, 7.2))
ax.set_aspect('equal'); ax.axis('off')
t = np.linspace(0, 2*np.pi, 400)
ax.plot(np.cos(t), np.sin(t), color=BLUE, lw=2)
ax.plot(*conic(p, e, t), color=INK, lw=2)
body(ax, (0, 0), s=300, color='#4f86c6', edge='#28527a')
# route (i): periapsis 1 at angle 0, apoapsis ra at angle pi (upper half)
a1 = (1 + ra)/2; e1 = (ra - 1)/(ra + 1)
f = np.linspace(0, np.pi, 200)
ax.plot(*conic(a1*(1 - e1*e1), e1, f), color=GREEN, lw=1.8, ls='--')
# route (ii): apoapsis 1 at angle pi, periapsis rp at angle 0 (lower half)
a2 = (1 + rp)/2; e2 = (1 - rp)/(1 + rp)
f = np.linspace(np.pi, 2*np.pi, 200)
ax.plot(*conic(a2*(1 - e2*e2), e2, f), color=RED, lw=1.8, ls='--')
for X, c in [((1, 0), GREEN), ((-ra, 0), GREEN), ((-1, 0), RED), ((rp, 0), RED)]:
    ax.scatter(*X, marker='*', s=160, color=c, zorder=7)
for s in (+1, -1):
    ax.scatter(0, s, s=70, color=PURPLE, zorder=7)
ax.text(.06, 1.06, r'X ($f = 90^\circ$)', color=PURPLE, fontsize=12)
ax.text(.06, -1.14, r'X$^\prime$ ($f = -90^\circ$)', color=PURPLE, fontsize=12)
ax.plot([-ra - .15, 1.2], [0, 0], color=GREY, lw=.8)
ax.plot([0, 0], [0, 1], color=PURPLE, lw=1, ls=':')
ax.text(.05, .5, r'$p = r_1$', color=PURPLE, fontsize=13)
ax.text(.83, .75, r'circle $r_1$', color=BLUE, fontsize=12)
ax.text(-1.55, .72, 'target ellipse\n$e = 0.3$', color=INK, fontsize=12, ha='right')
ax.text(-.4, .98, '(i)', color=GREEN, fontsize=14, fontweight='bold')
ax.text(-.25, -.62, '(ii)', color=RED, fontsize=14, fontweight='bold')
ax.text(rp - .03, .06, r'$r_p$', fontsize=12, ha='right'); ax.text(-ra - .04, .07, r'$r_a$', fontsize=12, ha='right')
ax.text(1.28, -.95, '★  burn points', fontsize=11, color=INK)
ax.set_xlim(-1.95, 1.65); ax.set_ylim(-1.3, 1.3)
finish(fig, 'fig_crossing')
