# Hi! I'm the artist and creater of this code, I'm going to explain every function and block

# Import libraries for the fuctions we're going to need
import random
import math
from math import sqrt

def random_paintbrush():
    """main function"""

    # Create a welcoming user interface
    print('Velkommen til Random Paintbrush!')
    print('Beskriv ditt format i millimeter')
    
    # For practical reasons set canvas dimensions from user, idealy this would also be randomized
    x = int(input('Hva er bredden (mm): '))
    y = int(input('Hva er lengden (mm): '))

    # Generate a random number of strokes
    num_strokes = random.randint(1, 20)
    for _ in range(num_strokes):
        values(x, y)
    print(f"\nTegningen er ferdig med {num_strokes} strøk!")


def values(x, y):
    """Generate a random stroke with random attributes: point d'accrochage, direction, colour and length"""

    # Set point d'accrochage at random
    startpunkt_x = random.randint(0, x)
    startpunkt_y = random.randint(0, y)
    startpunkt = f"({startpunkt_x}, {startpunkt_y})"

    # Random stroke direction in degrees
    strøk_retning = random.randint(0, 360)

    # Random stroke colour using RGB scale
    farge = f"rgb({random.randint(0, 255)}, {random.randint(0, 255)}, {random.randint(0, 255)})"

    # Determine max possible stroke length as to not exceed the format
    max_lengde_strøk = max_length_stroke(x, y, startpunkt_x, startpunkt_y, strøk_retning)
    strøk_lengde = random.randint(0, int(max_lengde_strøk))

    # Give the attributes back to main function
    print(f"Start: {startpunkt}, Retning: {strøk_retning}°, Lengde: {strøk_lengde} mm, Farge: {farge}")


def max_length_stroke(x_tot, y_tot, startpunkt_x, startpunkt_y, strøk_retning):
    """Calculate max stroke length based on format and rotation"""

    # Set the distances from point d'accrochage to each "wall" in the format
    x1, y1 = x_tot - startpunkt_x, y_tot - startpunkt_y
    x0, y0 = startpunkt_x, startpunkt_y

    # Use Pythagoras theorem to find every diogonal line from point d'accrochage to the corners
    p1, p2, p3, p4 = sqrt(x1**2 + y1**2), sqrt(x0**2 + y1**2), sqrt(y0**2 + x0**2), sqrt(x1**2 + y0**2)

    # Using the sinus function, identify the degree of the angels a, b, c and d between the horisontal+vertical and diagonal lines
    # This is needed to identify witch length from the point d'accrochage is needed in the calucation of the max stroke lenght
    vinkel_a = math.degrees(math.asin(y1 / p1)), 
    vinkel_b = math.degrees(math.asin(y1 / p2))
    vinkel_c = math.degrees(math.asin(y0 / p3)) 
    vinkel_d = math.degrees(math.asin(y0 / p4))

    # Set the length needed in clalculation of max stroke length
    if 0 <= strøk_retning < vinkel_a or 360 - vinkel_d <= strøk_retning <= 360:
        lengde = x1
    elif vinkel_a <= strøk_retning < 180 - vinkel_b:
        lengde = y1
    elif 180 - vinkel_b <= strøk_retning < 180 + vinkel_c:
        lengde = x0
    else:
        lengde = y0

    # Return the max stroke length using the cosinus function and the correct lenght and degree values
    return abs(lengde / math.cos(math.radians(strøk_retning)))

# Set of the main function
random_paintbrush()