import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import FancyArrowPatch


def fig1_4():
    """Literal Python conversion of fig1_4.m."""

    plt.close('all')
    # get(gcf)
    # MATLAB Position [8 1148 685 164]: preserve the 685 x 164 pixel size.
    fig, ax = plt.subplots(figsize=(6.85, 1.64), dpi=100, layout='constrained')
    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]
    ])
    plt.rcParams['axes.prop_cycle'] = plt.cycler(color=co)

    ax.plot([0, 5], [0, 0], 'k', linewidth=2)
    arrow = FancyArrowPatch((4.1, 0), (4.5, 0), arrowstyle='-|>',
                            mutation_scale=12, linewidth=1, color='k')
    ax.add_patch(arrow)
    yf = -0.22
    ax.text(1.95 - 1.52, yf, r'$\frac{1}{2}$', fontsize=18, clip_on=False)
    ax.text(1.95 - 0.97, yf + 0.02, r'$1$', fontsize=13, clip_on=False)
    ax.text(1.95 + 0.03, yf + 0.02, r'$2$', fontsize=13, clip_on=False)
    ax.text(1.97 + 2, yf + 0.02, r'$4$', fontsize=13, clip_on=False)
    ax.text(1.95 + 2.4, yf + 0.05, r'$x$', fontsize=16, clip_on=False)

    L = 2
    R = 4
    dx = 0.2
    dy = 0.07
    lw = 1.5
    lwr = 2.0
    for i in range(1, 5):
        if i == 1:
            ax.plot([L + dx*(i - 1), L + dx*(i - 1)], [-dy, dy],
                    'r', linewidth=lwr)
            ax.plot([R - dx*(i - 1), R - dx*(i - 1)], [-dy, dy],
                    'r', linewidth=lwr)
        else:
            ax.plot([L + dx*(i - 1), L + dx*(i - 1)], [-dy, dy],
                    'k', linewidth=lw)
            ax.plot([R - dx*(i - 1), R - dx*(i - 1)], [-dy, dy],
                    'k', linewidth=lw)

    L = 1
    R = 2
    dx = dx / 2
    for i in range(1, 5):
        if i == 1:
            ax.plot([L + dx*(i - 1), L + dx*(i - 1)], [-dy, dy],
                    'r', linewidth=lwr)
            ax.plot([R - dx*(i - 1), R - dx*(i - 1)], [-dy, dy],
                    'r', linewidth=lwr)
        else:
            ax.plot([L + dx*(i - 1), L + dx*(i - 1)], [-dy, dy],
                    'k', linewidth=lw)
            ax.plot([R - dx*(i - 1), R - dx*(i - 1)], [-dy, dy],
                    'k', linewidth=lw)

    L = 0.5
    R = 1
    dx = dx / 2
    for i in range(1, 5):
        if i == 1:
            ax.plot([L + dx*(i - 1), L + dx*(i - 1)], [-dy, dy],
                    'r', linewidth=lwr)
            ax.plot([R - dx*(i - 1), R - dx*(i - 1)], [-dy, dy],
                    'r', linewidth=lwr)
        else:
            ax.plot([L + dx*(i - 1), L + dx*(i - 1)], [-dy, dy],
                    'k', linewidth=lw)
            ax.plot([R - dx*(i - 1), R - dx*(i - 1)], [-dy, dy],
                    'k', linewidth=lw)

    ax.axis([0.2, 4.5, -2*dy, 2*dy])
    ax.set_frame_on(True)
    ax.grid(True)
    ax.set_aspect('equal', adjustable='box')
    ax.axis('off')
    ax.tick_params(labelsize=16)
    for label in ax.get_xticklabels() + ax.get_yticklabels():
        label.set_fontweight('bold')

    # exportgraphics(gca,'/Users/mark/Desktop/line.png','Resolution',1200)

    plt.show()


if __name__ == '__main__':
    fig1_4()
