"""Lecture 36: the three kinds of static stability, a ball in a bowl, on a dome and on a flat table.
Writes img/fig-36-balls.png."""
import numpy as np
from _visual import plt, save, MAROON, INK, GREY

if __name__ == "__main__":
    fig, axes = plt.subplots(1, 3, figsize=(10, 2.8))
    x = np.linspace(-1, 1, 200)
    for ax, y, lab in zip(axes, (0.55 * x**2, 0.6 - 0.55 * x**2, np.full_like(x, 0.25)), ("stable", "unstable", "neutral")):
        ax.plot(x, y, color=INK, lw=2.2)
        yb = np.interp(0.0, x, y) + 0.09
        ax.plot(0, yb, "o", color=MAROON, ms=17)
        xd = 0.5
        ax.plot(xd, np.interp(xd, x, y) + 0.09, "o", mfc="none", mec=MAROON, ms=17, mew=1.8)
        d = {"stable": -1, "unstable": 1, "neutral": 0}[lab]
        if d:
            ax.annotate("", xy=(xd + 0.32 * d, np.interp(xd + 0.32 * d, x, y) + 0.30), xytext=(xd, np.interp(xd, x, y) + 0.30),
                        arrowprops=dict(arrowstyle="-|>", color=MAROON, lw=2))
        ax.set_title(lab, fontsize=12); ax.set_xlim(-1.1, 1.1); ax.set_ylim(-0.1, 1.15); ax.axis("off")
    save(fig, "fig-36-balls")
