"""Lecture 2, the sidereal and the solar day: after one turn relative to the stars (23 h 56 min) the Earth has
moved along its orbit, so it must turn a little more to face the Sun again (24 h). The orbital step is
exaggerated 25 times. Writes img/fig-02-sidereal.png."""
import numpy as np
from _visual import plt, save, INK, AMBER, BLUE, MAROON, GREY
from matplotlib.patches import Circle

STEP = np.radians(360 / 365.25) * 25                      # one day of orbit, exaggerated
if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 5.6))
    ax.add_patch(Circle((0, 0), 0.5, color=AMBER))
    ax.text(0, -0.85, "Sun", ha="center", fontsize=12, color=AMBER)
    D = 4.0
    th = np.linspace(-0.15, STEP + 0.25, 100)
    ax.plot(D * np.cos(th), D * np.sin(th), color=GREY, lw=1, ls="--")
    for k, (a, lab) in enumerate(((0.0, "day 0, noon"), (STEP, "one sidereal day later"))):
        c = np.array([D * np.cos(a), D * np.sin(a)])
        ax.add_patch(Circle(c, 0.35, facecolor="#dfe7ef", edgecolor=BLUE, lw=1.2))
        ax.annotate("", xy=c + 1.3 * np.array([-1, 0]), xytext=c, arrowprops=dict(arrowstyle="-|>", color=MAROON, lw=2.2))
        if k == 1:
            to_sun = -c / np.linalg.norm(c)
            ax.annotate("", xy=c + 1.3 * to_sun, xytext=c, arrowprops=dict(arrowstyle="-|>", color=AMBER, lw=2.2))
            ax.text(*(c + np.array([-1.9, 0.55])), "a little more turning\n(about 4 min) to face the Sun", fontsize=11, color=INK)
        ax.text(*(c + np.array([0.45, -0.15])), lab, fontsize=11)
    ax.text(-0.5, 2.6, "red arrow: always towards the same distant star", fontsize=11, color=MAROON)
    ax.set_xlim(-1.2, 6.2)
    ax.set_ylim(-1.2, 3.2)
    ax.set_aspect("equal")
    ax.axis("off")
    save(fig, "fig-02-sidereal")
