from matplotlib import pyplot as plt, pyplot


def interpolate_y(x1, y1, x2, y2, x):
    if x1 == x2:
        raise ValueError("x1 and x2 cannot be the same value, as the line would be undefined.")

    # 计算插值的y值
    y = y1 + (y2 - y1) * (x - x1) / (x2 - x1)
    return y


def cal_num_6(size):
    if size < 2.5:
        return interpolate_y(1.875, 2.363, 2.5, 4.87, size)
    elif 2.5 <= size < 3.75:
        return interpolate_y(3.75, 11, 2.5, 4.87, size)
    elif 3.75 <= size:
        return interpolate_y(3.75, 11, 5, 19.48, size)


def cal_cube_parameter(size, height):

    # size = 5
    # height = 1.25

    num0 = size * height
    num1 = 0.7778
    num2 = size / 2 + 0.03

    num3 = num0
    num4 = num1
    num5 = num2

    num6 = cal_num_6(size)
    num7 = 0.9796
    num8 = interpolate_y(1.875, 0.1449, 5, 0.2207, size)

    num9 = cal_num_6(size)
    num10 = 0.9796
    num11 = interpolate_y(1.875, 0.1449, 5, 0.2207, size)

    num12 = num0
    num13 = 0.7776
    num14 = num2

    num15 = num0
    num16 = 0.7776
    num17 = num2

    num18 = 0
    num19 = -0.5 * height
    num20 = 0

    num21 = size
    num22 = height
    num23 = size

    result = [num0, num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, num11, num12, num13, num14, num15,
              num16, num17, num18, num19, num21, num22, num23]
    return result


def print_cube_content(size, height_list):
    print('    DRAG_CUBE')
    print('    {')
    for i in range(len(height_list)):
        print(f'        cube = {i}, ', end='')
        print(','.join([str(round(item, 5)) for item in cal_cube_parameter(size, height_list[i])]))
    print('    }')



if __name__ == '__main__':
    # sample_data_list = [6.25, 0.7781, 2.529, 6.25, 0.7781, 2.529, 19.48, 0.9796, 0.2207, 19.48, 0.9796, 0.2207, 6.25,
    #                     0.7776, 2.529, 6.25, 0.7776, 2.529, 0, -0.625, 0, 5, 1.25, 5]
    height_list = [1.875, 2.5, 3.75, 5]
    # new_table = cal_cube_parameter(3.35, 5)
    # print(sample_data_list)
    # print(new_table)
    print_cube_content(5.4, height_list)
