"""Lecture 2, a turning unit vector: i_B of a frame turning at theta-dot about k (out of the page). The
tip of i_B moves along the unit circle, always at right angles to i_B, along w x i_B (the blue arrow, drawn as
the vector the engine-independent formula gives, w x i_B with w = theta-dot k). Writes img/anim-02-unit-vector.gif."""
import numpy as np
from _visual import plt, save_anim, INK, MAROON, BLUE, GREY

THDOT = 1.0
if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(7.2, 7.2))
    N, fps = 100, 16
    ths = np.linspace(0, np.radians(80), N)
    kw = dict(arrowstyle="-|>", mutation_scale=18, lw=2.2)

    def frame(k):
        ax.clear()
        th = ths[k]
        arc = np.linspace(0, np.radians(100), 200)
        ax.plot(np.cos(arc), np.sin(arc), color="#d9d2c3", lw=1)
        ax.plot(np.cos(ths[:k + 1]), np.sin(ths[:k + 1]), color=MAROON, lw=3, alpha=0.35)
        for v, lab in (((1.2, 0), r"$\mathbf{i}_I$"), ((0, 1.2), r"$\mathbf{j}_I$")):
            ax.annotate("", xy=v, xytext=(0, 0), arrowprops=dict(color=GREY, **kw))
            ax.text(v[0] + 0.03, v[1] + 0.03, lab, fontsize=13, color=GREY)
        i_b = np.array([np.cos(th), np.sin(th), 0.0])
        j_b = np.array([-np.sin(th), np.cos(th), 0.0])
        ax.annotate("", xy=i_b[:2], xytext=(0, 0), arrowprops=dict(color=MAROON, **kw))
        ax.text(*(1.08 * i_b[:2]), r"$\mathbf{i}_B$", fontsize=14, color=MAROON)
        wxi = np.cross([0, 0, THDOT], i_b)
        ax.annotate("", xy=i_b[:2] + 0.45 * wxi[:2], xytext=i_b[:2], arrowprops=dict(color=BLUE, **kw))
        ax.text(*(i_b[:2] + 0.5 * wxi[:2] + np.array([0.02, 0.02])), r"$\boldsymbol{\omega}\times\mathbf{i}_B$", fontsize=14,
                color=BLUE)
        ax.text(-0.35, -0.25, r"$\boldsymbol{\omega}=\dot\theta\,\mathbf{k}$, out of the page", fontsize=12)
        ax.text(-0.35, -0.38, rf"$\theta={np.degrees(th):4.0f}^\circ$", fontsize=12)
        ax.set_xlim(-0.75, 1.7)
        ax.set_ylim(-0.45, 1.6)
        ax.set_aspect("equal")
        ax.axis("off")
        return []

    save_anim(fig, frame, N, fps, "anim-02-unit-vector")
