"""Lecture 4, the wind triangle V_a = V - W, seen from above: a 40 m/s headwind and a 40 m/s crosswind for an
aircraft whose ground velocity is 250 m/s due north. Writes img/fig-04-wind-triangle.png."""
import numpy as np
from _visual import plt, save, INK, MAROON, BLUE, GREEN, GREY

V = np.array([0.0, 250.0])                                   # screen: x east, y north
CASES = [("headwind from the north", np.array([0.0, -40.0])), ("crosswind from the west", np.array([40.0, 0.0]))]
kw = dict(arrowstyle="-|>", mutation_scale=18, lw=2.4)

if __name__ == "__main__":
    fig, axs = plt.subplots(1, 2, figsize=(10, 5.4))
    for ax, (title, W) in zip(axs, CASES):
        Va = V - W
        sh = np.array([22.0, 0]) if abs(W[0]) < 1e-9 else np.zeros(2)     # collinear case: draw side by side
        ax.annotate("", xy=V, xytext=(0, 0), arrowprops=dict(color=MAROON, **kw))
        ax.annotate("", xy=V + sh, xytext=W + sh, arrowprops=dict(color=BLUE, **kw))
        ax.annotate("", xy=W - sh, xytext=-sh, arrowprops=dict(color=GREEN, **kw))
        ax.text(*(V / 2 + np.array([-14, 0])), f"$\\mathbf{{V}}$ over the ground\n{np.linalg.norm(V):.0f} m/s", color=MAROON,
                fontsize=12, ha="right", va="center")
        ax.text(*((V + W) / 2 + sh + np.array([14, 0])), f"$\\mathbf{{V}}_a$ through the air\n{np.linalg.norm(Va):.0f} m/s",
                color=BLUE, fontsize=12, ha="left", va="center")
        ax.text(*(W / 2 - sh + np.array([0, -30])), f"wind $\\mathbf{{W}}$, {np.linalg.norm(W):.0f} m/s", color=GREEN, fontsize=12,
                ha="center")
        ax.set_title(title, fontsize=12)
        ax.set_xlim(-190, 230)
        ax.set_ylim(-75, 275)
        ax.set_aspect("equal")
        ax.axis("off")
    save(fig, "fig-04-wind-triangle")
