Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions backup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@

def random_sin(n):
# if random.random() < 0.5:
# return sin(pi * n) * random.random()
# else:
return sin(pi * n)

def random_cos(n):
# if random.random() < 0.5:
# return cos(pi * n) * random.random()
# else:
return cos(pi * n)

def random_mod_pi(n):
# if random.random() < 0.5:
# return (n % pi) * random.random()
# else:
return (n % pi)

def random_log(n):
return log(abs(n)) if n != 0 else (random.random()+1)

def random_axis(x,y):
axis_choice = random.randint(1,4)
if axis_choice == 1:
return x
elif axis_choice == 2:
return y
elif axis_choice == 3:
return x + y
else:
return x*y

def random_function(axis):
function_choice = random.randint(1,6)
if function_choice == 1:
return random_sin(axis)
elif function_choice == 2:
return random_cos(axis)
elif function_choice == 3:
return random_log(axis)
else:
return random_mod_pi(axis)

def random_formula(x,y):
formula_choice = random.randint(1,4)
if formula_choice == 1:
return random_function(x) + random_function(y)
elif formula_choice == 2:
return random_function(x) - random_function(y)
elif formula_choice == 3:
return random_function(x) * random_function(y)
else:
return random_function(x * y)

def add_to_expression(expr):
random_function = random.randint(4,7)
if random_function == 0:
new_expr = lambda x, y: (expr(x,y) * random_sin(random_axis(x,y)))
elif random_function == 1:
new_expr = lambda x, y: (expr(x,y) * random_cos(random_axis(x,y)))
elif random_function == 2:
new_expr = lambda x, y: (expr(x,y) * random_mod_pi(random_axis(x,y)))
elif random_function == 3:
new_expr = lambda x, y: (expr(x,y) * random_log(random_axis(x,y)))
elif random_function == 4:
new_expr = lambda x,y: random_sin(expr(x,y))
elif random_function == 5:
new_expr = lambda x,y: random_cos(expr(x,y))
elif random_function == 6:
new_expr = lambda x,y: random_mod_pi(expr(x,y))
elif random_function == 7:
new_expr = lambda x,y: random_log(expr(x,y))
else:
new_expr = lambda x, y: (expr(x,y) + expr(y,x))
return new_expr
Binary file added color0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added favorite.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added favorite2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
138 changes: 135 additions & 3 deletions random_art.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,146 @@
import random
from math import sin, cos, tan, pi
from math import log, log10, acos
from statistics import mean

# Your job is to create better version of create_expression and
# run_expression to create random art.
# Your expression should have a __str__() function defined for it.
formula = ""


def __str__():
global formula
print(formula)


def random_sin(n):
global formula
formula = "sin({})".format(formula)
return sin(pi * n)


def random_cos(n):
global formula
formula = "cos({})".format(formula)
return cos(pi * n)


def random_mod_pi(n):
global formula
formula = "({} % pi)".format(formula)
return (n % pi)/10


def average(x, y):
global formula
formula = "avg({})".format(formula)
return mean([x, y])


def random_log(n):
""" returns log of n or a random number, if n == 0 """
global formula
formula = "log({})".format(formula)
return log(abs(n)) if n != 0 else (random.random()+1)


def random_axis(x, y):
""" Returns an axis or a combination of axes. """
axis_choice = random.randint(0, 100)
if axis_choice <= 40:
return x
elif axis_choice <= 80:
return y
elif axis_choice <= 90:
return x + y
else:
return x*y


def random_operator(axis):
""" Random Operators takes the axis and applies a randomly generated
operator to it. """
global formula
function_choice = random.randint(1, 20)
if function_choice <= 15:
formula = "sin({})".format(formula)
return random_sin(axis)
elif function_choice <= 30:
formula = "cos({})".format(formula)
return random_cos(axis)
elif function_choice <= 45:
formula = "log({})".format(formula)
return random_log(axis)
else:
formula = "({} % pi)".format(formula)
return random_mod_pi(axis)


def random_function(x, y):
""" Creates a random base function from a variety of operators and how
they are combined. """
global formula
formula_choice = random.randint(0, 100)
if formula_choice <= 35:
formula = "x"
return random_operator(x)
elif formula_choice <= 70:
formula = "y"
return random_operator(y)
elif formula_choice <= 85:
formula = "avg(x, y)"
return average(random_operator(x), random_operator(y))
else:
formula = "(x * y)"
return random_operator(x)*random_operator(y)


def add_to_expression(expr):
""" Randomly generates larger expressions from the given expression. """
global formula
random_operator = random.randint(0, 100)
if random_operator <= 5:
formula = "{} * sin(x, y, x+y or x*y)".format(formula)
new_expr = lambda x, y: (expr(x, y) * random_sin(random_axis(x, y)))
elif random_operator <= 10:
formula = "{} * cos(x, y, x+y or x*y)".format(formula)
new_expr = lambda x, y: (expr(x, y) * random_cos(random_axis(x, y)))
elif random_operator <= 15:
formula = "{} * (x, y, x+y or x*y)%pi".format(formula)
new_expr = lambda x, y: (expr(x, y) * random_mod_pi(random_axis(x, y)))
elif random_operator <= 20:
formula = "{} * log(x, y, x+y or x*y)".format(formula)
new_expr = lambda x, y: (expr(x, y) * random_log(random_axis(x, y)))
elif random_operator <= 55:
formula = "sin( {} )".format(formula)
new_expr = lambda x, y: random_sin(expr(x, y))
elif random_operator <= 80:
formula = "cos( {} )".format(formula)
new_expr = lambda x, y: random_cos(expr(x, y))
elif random_operator <= 85:
formula = "( {} % pi )".format(formula)
new_expr = lambda x, y: random_mod_pi(expr(x, y))
elif random_operator <= 90:
formula = "log( {} )".format(formula)
new_expr = lambda x, y: random_log(expr(x, y))
else:
formula = "( {} + inverse({}) / 2 )".format(formula, formula)
new_expr = lambda x, y: (expr(x, y) + expr(y, x)/2)
return new_expr


def create_expression():
"""This function takes no arguments and returns an expression that
generates a number between -1.0 and 1.0, given x and y coordinates."""
expr = lambda x, y: (random.random() * 2) - 1
"""Creates a simple expression and then builds upon it a random amount
of times using add_to_expression. """
global formula
formula = ""
nests = random.randint(2, 10)
expr = lambda x, y: random_function(x, y)

for _ in range(nests):
expr = add_to_expression(expr)
print(formula)
return expr


Expand Down