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

from splineA import splineA


def fig2_15():
    a = -3
    b = 3
    nx = 200
    x = np.linspace(a, b, nx)
    y = np.zeros(nx)
    Y = np.zeros(nx)
    for ix in range(nx):
        y[ix] = f(x[ix])
        Y[ix] = F(x[ix])

    plt.close("all")
    # get(gcf)
    fig, ax = plt.subplots(
        figsize=(658 / 100, 230 / 100), dpi=100, constrained_layout=True
    )
    co = [
        (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),
    ]
    plt.rcParams["axes.prop_cycle"] = cycler(color=co)

    ax.plot(x, y, "b", linewidth=2)
    ax.plot(x, Y, "b", linewidth=2)
    ax.plot([a, b], [0, 0], "-k", linewidth=0.5)
    for spine in ax.spines.values():
        spine.set_visible(True)
    ax.grid(True)
    ax.axis([-3, 3, -2.3, 2.2])
    ax.set_yticks([0])
    ax.set_xlabel("x-axis")
    ax.set_ylabel("y-axis")
    ax.tick_params(labelsize=12)
    for label in ax.get_xticklabels() + ax.get_yticklabels():
        label.set_fontweight("bold")
    ax.xaxis.label.set_fontsize(12)
    ax.yaxis.label.set_fontsize(12)
    ax.xaxis.label.set_fontweight("bold")
    ax.yaxis.label.set_fontweight("bold")

    ax.text(1.1, 1.3, r"$F(x)$", fontsize=16)
    ax.text(-1.1, 1.1, r"$G(x)$", fontsize=16)

    # fig.savefig('/Users/mark/Desktop/exercise2.eps')
    plt.show()


def f(x):
    # g=(x+1)*(x-0.1)*(x-2);
    g = splineA([-3, -1.5, 0, 1.5, 2.5], [1, -2, 2, -1.5, 2], x)
    return g


def F(x):
    # g=exp(x/2)-1.2;
    g = -1 + (1.5 + 1) * (x + 3) / 6
    return g


if __name__ == "__main__":
    fig2_15()
