from pathlib import Path

import numpy as np
import matplotlib.pyplot as plt
from cycler import cycler

from splineA import splineA


def fig5_2L():
    # the requires the files: hand_nodes.txt and splineA.m

    # the hand data is due to John Burkardt and from:
    # https://people.sc.fsu.edu/~jburkardt/m_src/hand_data_test/hand_data_test.html

    hn = np.loadtxt(Path(__file__).with_name("hand_nodes.txt"))

    hand = np.vstack((hn, hn[0, :]))
    m, n = hand.shape
    print((m, n))
    p = np.arange(1, m + 1)
    t = np.linspace(1, m, 500)

    # use not-a-knot
    # u = CubicSpline(p, hand[:, 0])(t)
    # v = CubicSpline(p, hand[:, 1])(t)

    # use a natural spline fit
    u = splineA(p, hand[:, 0], t)
    v = splineA(p, hand[:, 1], t)

    plt.close("all")
    # get(gcf)
    fig, ax = plt.subplots(figsize=(7.0, 5.25))

    co = np.array([
        [0, 0, 1],
        [0, 0.5, 0],
        [1, 0, 0],
        [0, 0.75, 0.75],
        [0.75, 0, 0.75],
        [0.75, 0.75, 0],
        [0.25, 0.25, 0.25],
    ])
    ax.set_prop_cycle(cycler(color=co))

    ax.plot(u, v, color="r", linewidth=2)
    ax.plot(hand[:, 0], hand[:, 1], ".b", markersize=26)

    ax.set_xticks([0.2, 0.3, 0.4, 0.5, 0.6, 0.7])
    ax.set_yticks([0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
    ax.set_xlim(0.2, 0.7)
    ax.set_ylim(0.2, 0.9)
    # ax.set_aspect("equal", adjustable="box")
    ax.grid(True)
    ax.set_xlabel("x-axis")
    ax.set_ylabel("y-axis")
    ax.tick_params(labelsize=24)
    for label in ax.get_xticklabels() + ax.get_yticklabels():
        label.set_fontweight("bold")
    fig.tight_layout()
    plt.show()


if __name__ == "__main__":
    fig5_2L()
