import numpy as np
import matplotlib.pyplot as plt


def new_coord(contours, x_scale, y_scale, angle):
    ''''''

    ''' SCALE THE IMAGE HERE '''
    scale_contours = []
    for i in contours:
        x = i[0] * x_scale
        y = i[1] * y_scale
        z = i[2]
        scale_contours.append([x, y, z])

    ''' ROTATE THE IMAGE HERE '''
    angle = angle * np.pi / 180
    rotate_contours = []
    for i in scale_contours:
        x_old = i[0]
        y_old = i[1]
        x = x_old * np.cos(angle) - y_old * np.sin(angle)
        y = x_old * np.sin(angle) + y_old * np.cos(angle)
        z = i[2]
        rotate_contours.append([x, y, z])

    return rotate_contours

def scale_calculator(contours, size):
    x_max = contours[0][0]
    x_min = contours[0][0]
    y_max = contours[0][1]
    y_min = contours[0][1]

    for i in contours:
        x = i[0]
        y = i[1]
        if x_min > x:
            x_min = x
        if x > x_max:
            x_max = x
        if y_min > y:
            y_min = y
        if y > y_max:
            y_max = y
    #print([[x_min, x_max], [y_min, y_max]])            #display max and min in x and y

    x_scale = 1.0
    y_scale = 1.0
    if size[0] != 0:
        x_scale = size[0]/(x_max - x_min)
    if size[1] != 0:
        y_scale = size[1]/(y_max - y_min)
    if size[0] == 0:
        x_scale = y_scale
    if size[1] == 0:
        y_scale = x_scale

    x_min = x_min*x_scale
    y_min = y_min*y_scale

    return [x_min, y_min, x_scale, y_scale]


def get_size(coords):
    x_max = coords[0][0]
    x_min = coords[0][0]
    y_max = coords[0][1]
    y_min = coords[0][1]

    for i in range(len(coords)):
        x = coords[i][0]
        y = coords[i][1]
        if x_min > x:
            x_min = x
        if x > x_max:
            x_max = x
        if y_min > y:
            y_min = y
        if y > y_max:
            y_max = y

    return [[x_min, x_max], [y_min, y_max]]         #display max and min in x and y

def symetry(coords, x_max, y_max, symetry):
    new_coords = []

    for i in coords:
        x = i[0]
        y = i[1]
        z = i[2]
        if symetry == 'x':
            x = x_max - x
        if symetry == 'y':
            y = y_max - y
        new_coords.append([x, y, z])

    return new_coords

def set_origin(coords, origin):
    [[x_min, x_max], [y_min, y_max]] = get_size(coords)
    new_coords = []
    for i in coords:
        x = i[0] - x_min + origin[0]
        y = i[1] - y_min + origin[1]
        z = i[2] + origin[2]
        new_coords.append([x, y, z])

    return new_coords

def plot_function(coords):
    x_coords = []
    y_coords = []
    z_coords = []

    for i in coords:
        x_coords.append(i[0])
        y_coords.append(i[1])
        z_coords.append(i[2])

    plt.scatter(y_coords, x_coords, label='Points', color='blue', s=5)
    plt.gca().set_aspect('equal', adjustable='box')
    plt.xlabel('X-axis')
    plt.ylabel('Y-axis')
    plt.title('Scatter Plot of Points')
    plt.legend()
    plt.show()

def draw_function(coords):
    temp_set_x = []
    temp_set_y = []
    final_set_x = []
    final_set_y = []
    for i in coords:
        temp_set_x.append(i[0])
        temp_set_y.append(i[1])
        if i[2] != 0:
            final_set_x.append(temp_set_x)
            final_set_y.append(temp_set_y)
            temp_set_x = []
            temp_set_y = []
    #print(final_set_x)
    #print(final_set_y)
    for j in range(len(final_set_x)):
        plt.plot(final_set_x[j], final_set_y[j])
    plt.gca().set_aspect('equal', adjustable='box')
    plt.xlabel('X-axis')
    plt.ylabel('Y-axis')
    plt.title('Scatter Plot of Points')
    plt.show()
