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


def fig5_16():
    ### pick the number of points
    # n = 6
    n = 12
    an = 7

    # semi-circle
    r = 1
    nt = 100
    th = np.linspace(0, np.pi, nt)
    x = np.zeros(nt)
    y = np.zeros(nt)
    for i in range(nt):
        x[i] = r * np.cos(th[i])
        y[i] = r * np.sin(th[i])

    # points on circle
    xx = np.zeros(2 * n)
    yy = np.zeros(2 * n)
    for ii in range(0, 2 * n, 2):
        matlab_ii = ii + 1
        xx[ii] = r * np.cos(matlab_ii * np.pi / (2 * n))
        yy[ii] = r * np.sin(matlab_ii * np.pi / (2 * n))
    print(xx[0])

    plt.close("all")
    # get(gcf)
    fig, ax = plt.subplots(figsize=(7.0, 4.0))
    fig.subplots_adjust(left=0.08, right=0.98, bottom=0.18, top=0.98)
    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))

    # arc shader
    xc = np.zeros(8)
    yc = np.zeros(8)
    xc[0] = 0.02 * r * np.cos((an + 1) * np.pi / (2 * n))
    yc[0] = 0.02 * r * np.sin((an + 1) * np.pi / (2 * n))
    na = 6
    ang = np.linspace(
        an * np.pi / (2 * n) + 0.008,
        (an + 2) * np.pi / (2 * n) - 0.008,
        na,
    )
    for i in range(na):
        xc[i + 1] = 0.995 * r * np.cos(ang[i])
        yc[i + 1] = 0.995 * r * np.sin(ang[i])
    xc[na + 1] = xc[0]
    yc[na + 1] = yc[0]
    ax.fill(xc, yc, "k", alpha=0.3)
    ax.text(0.5, 0.94, r"$\pi/n$", fontsize=18)

    ax.plot(x, y, linewidth=1.2)
    for i in range(0, 2 * n, 2):
        ax.plot([0, xx[i]], [0, yy[i]], "b", linewidth=1)
        ax.plot([xx[i]], [yy[i]], ".b", markersize=30)
        ax.plot([xx[i], xx[i]], [0, yy[i]], "--r", linewidth=1.2)
        ax.plot([xx[i]], [0], ".r", markersize=30)

    ax.set_xticks([-1.02, 0, 1.02])
    ax.set_yticks([2])
    ax.set_xticklabels(["a", "(a+b)/2", "b"])

    ax.set_aspect("equal", adjustable="box")
    ax.grid(True)
    ax.set_xlim(-1.02, 1.02)
    ax.set_ylim(-0.008, 1.02)
    ax.set_xlabel("x-axis")
    ax.set_ylabel("y-axis")
    ax.tick_params(labelsize=16)
    for label in ax.get_xticklabels() + ax.get_yticklabels():
        label.set_fontweight("bold")
    plt.show()

    # plt.savefig('/Users/mark/Desktop/t12.eps')


if __name__ == "__main__":
    fig5_16()
