"""Lecture 2, the velocity sum v_I = v_B + omega x r (eq-vel) on the merry-go-round of anim-02-merry-go-round: a ball
rolls outwards in a straight line at 1 m/s from the centre of a platform that turns anticlockwise once every 4 s.
Seen from the ground at five instants: v_I (constant, along the straight path), omega x r (the velocity of the
platform under the ball, at right angles to the radius and growing with it) and v_B = v_I - omega x r (what a
rider measures). Writes img/fig-02-velocity-sum.png."""
import numpy as np
from matplotlib.patches import Circle
from _visual import plt, save, INK, MAROON, BLUE, GREY

OMEGA, V, RAD = 2 * np.pi / 4.0, 1.0, 2.5
TIMES = np.array([0.5, 1.0, 1.5, 2.0, 2.4])
W = np.array([0.0, 0.0, OMEGA])


def vectors(t):
    """(r, v_I, omega x r, v_B) at time t, ground components."""
    r = np.array([V * t, 0.0, 0.0])
    vI = np.array([V, 0.0, 0.0])
    wr = np.cross(W, r)
    return r, vI, wr, vI - wr


ROWS = [vectors(t) for t in TIMES]
SPEED_B = np.array([np.linalg.norm(v[3]) for v in ROWS])        # the rider's speed grows: V sqrt(1 + (omega t)^2)

if __name__ == "__main__":
    fig, (a1, a2) = plt.subplots(1, 2, figsize=(10.5, 5.2), gridspec_kw=dict(width_ratios=[1, 1.25]))
    fig.subplots_adjust(wspace=0.08)
    kw = dict(arrowstyle="-|>", lw=2.2, mutation_scale=16)
    # left: the platform seen from the ground, the straight path, and the floor's velocity under the ball
    a1.add_patch(Circle((0, 0), RAD, facecolor="#efe9dd", edgecolor=GREY, lw=1))
    a1.plot([0, RAD], [0, 0], color=MAROON, lw=2.2)
    for t, (r, vI, wr, vB) in zip(TIMES[[0, 2, 4]], [ROWS[0], ROWS[2], ROWS[4]]):
        a1.plot(*r[:2], "o", color=MAROON, ms=8, zorder=5)
        a1.annotate("", xy=r[:2] + 0.3 * wr[:2], xytext=r[:2], arrowprops=dict(color=BLUE, **kw))
        a1.text(r[0], -0.35, f"{t:g} s", ha="center", fontsize=11)
    a1.annotate("", xy=(1.9 * np.cos(1.9), 1.9 * np.sin(1.9)), xytext=(1.9 * np.cos(1.1), 1.9 * np.sin(1.1)),
                arrowprops=dict(arrowstyle="->", color=GREY, lw=1.5, connectionstyle="arc3,rad=0.25"))
    a1.text(0, -3.05, "seen from the ground", ha="center", fontsize=12)
    a1.set_xlim(-2.8, 2.8); a1.set_ylim(-3.2, 2.8); a1.set_aspect("equal"); a1.axis("off")
    # right: the triangle v_B + omega x r = v_I at the same three instants
    for j, (t, (r, vI, wr, vB)) in enumerate(zip(TIMES[[0, 2, 4]], [ROWS[0], ROWS[2], ROWS[4]])):
        o = np.array([1.6 * j, 0.0])
        a2.annotate("", xy=o + vB[:2], xytext=o, arrowprops=dict(color=INK, **kw))
        a2.annotate("", xy=o + vB[:2] + wr[:2], xytext=o + vB[:2], arrowprops=dict(color=BLUE, **kw))
        a2.annotate("", xy=o + vI[:2], xytext=o, arrowprops=dict(color=MAROON, **kw))
        a2.text(o[0] + 0.5, 0.35, f"{t:g} s", ha="center", fontsize=11)
    a2.text(0.0, -4.9, "$\\mathbf{v}_B$, what the rider measures", fontsize=12, color=INK)
    a2.text(0.0, -5.5, "$+\\ \\boldsymbol{\\omega}\\times\\mathbf{r}$, the floor under the ball", fontsize=12, color=BLUE)
    a2.text(0.0, -6.1, "$=\\ \\mathbf{v}_I$, the same every time", fontsize=12, color=MAROON)
    a2.set_xlim(-0.3, 4.6); a2.set_ylim(-6.4, 0.8); a2.set_aspect("equal"); a2.axis("off")
    save(fig, "fig-02-velocity-sum")
