Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Harrison Young Recursive Art #7

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
Binary file added Recursive Art reflection.pdf
Binary file not shown.
Binary file added Test3.2.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 Test4.2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
133 changes: 125 additions & 8 deletions recursive_art.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,33 @@

import random
from PIL import Image
import math
pi = 3.14

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a stylistic convention to use all caps for constants. PI=3.14

def prod(a,b): #Returns the product of two values

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice inline comments! 👍

return a * b

def avg(a,b): #returns the average of two values
return 0.5 * (a + b)

def cos_pi(a): #returns the cosine of a value
return math.cos(pi * a)

def sin_pi(a):
return math.sin(pi * a) #returns the sine of a value

def findx(a , b): #returns the x value
return a

def findy(a,b): #returns the y value
return b
def times_neg_1(a): #returns the negitive
return -1 * a
def mean(a,b): #returns the the geometric mean
return (abs(a) * abs(b)) ** 0.5






def build_random_function(min_depth, max_depth):
Expand All @@ -20,7 +47,47 @@ def build_random_function(min_depth, max_depth):
(See the assignment writ-eup for details on the representation of
these functions)
"""
depth = random.randint(min_depth,max_depth);

allfunc = ['prod','avg','cos_pi','sin_pi','x','y','times_neg_1','mean']
xory = ['y','x']
func = random.choice(allfunc)
#print (func, depth)

if depth == 1:
return random.choice(xory)
if func == 'prod':
return ['prod',build_random_function(depth-1, depth-1),build_random_function(depth-1, depth-1)]
elif func == 'avg':
return ['avg',build_random_function(depth-1, depth-1),build_random_function(depth-1, depth-1)]
elif func == 'mean':
return ['mean',build_random_function(depth-1, depth-1),build_random_function(depth-1, depth-1)]
elif func == 'cos_pi':
return ['cos_pi',build_random_function(depth-1, depth-1)]
elif func == 'sin_pi':
return ['sin_pi',build_random_function(depth-1, depth-1)]
elif func == 'times_neg_1':
return ['times_neg_1',build_random_function(depth-1, depth-1)]
elif func == 'x':
return ['x',build_random_function(depth-1, depth-1),build_random_function(depth-1, depth-1)]
elif func == 'y':
return ['y',build_random_function(depth-1, depth-1),build_random_function(depth-1, depth-1)]
else:
print ("error")



# prod = ['prod',build_random_function(depth-1, depth-1),build_random_function(depth-1, depth-1)]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove comments for final code submission.

# avg = ['avg',build_random_function(depth-1, depth-1),build_random_function(depth-1, depth-1)]
# cos = ['cos_pi',build_random_function(depth-1, depth-1)]
# sin = ['sin_pi',build_random_function(depth-1, depth-1)]
# x = ['x',build_random_function(depth-1, depth-1),build_random_function(depth-1, depth-1)]
# y = ['y', build_random_function(depth-1, depth-1),build_random_function(depth-1, depth-1)]



# TODO: implement this

pass


Expand All @@ -43,7 +110,47 @@ def evaluate_random_function(f, x, y):
>>> evaluate_random_function(["y"],0.1,0.02)
0.02

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend that you add more unit tests of your own in future projects.

"""
# TODO: implement this
#if (f[0] == 'x') | (f[0] == 'X'):
# return x
#elif (f[0] == 'y') | (f[0] == 'y'):
# return y
#else:
# return "Error"
#TODO: implement this
#print (f)
e = evaluate_random_function #dummy function to make the code take up less room

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think it's actually more readable to use the full function name in this case.

if len(f) == 1: # only happens on the inner most layer when the list is [x] or [y]
if f[0] == 'x':
return x
elif f[0] == 'y':
return y
else:
print ("Evaluating Error", f[0]) #if somthing other than x or y is found somthing went wrong

elif f[0] == 'prod':
return prod(e(f[1],x,y),e(f[2],x,y))
elif f[0] == 'avg':
return avg(e(f[1],x,y),e(f[2],x,y))
elif f[0] == 'cos_pi':
return cos_pi(e(f[1],x,y))
elif f[0] == 'sin_pi':
return sin_pi(e(f[1],x,y))
elif f[0] == 'x':
return findx(e(f[1],x,y),e(f[2],x,y))
elif f[0] == 'y':
return findy(e(f[1],x,y),e(f[2],x,y))
elif f[0] == 'mean':
return mean(e(f[1],x,y),e(f[2],x,y))
elif f[0] == 'times_neg_1':
return times_neg_1(e(f[1],x,y))
else:
print ("Evaluation Error", f[0]) # #if somthing other than the above options is found somthing went wrong






pass


Expand Down Expand Up @@ -79,7 +186,13 @@ def remap_interval(val,
1.0
>>> remap_interval(5, 4, 6, 1, 2)
1.5
"""
"""

difference = val-input_interval_start
difference = difference/ (input_interval_end - input_interval_start)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

simple syntax : difference += input_interval_end - input_interval_start)

difference = difference * (output_interval_end - output_interval_start)
difference = difference + output_interval_start
return difference
# TODO: implement this
pass

Expand Down Expand Up @@ -137,10 +250,11 @@ def generate_art(filename, x_size=350, y_size=350):
x_size, y_size: optional args to set image dimensions (default: 350)
"""
# Functions for red, green, and blue channels - where the magic happens!
red_function = ["x"]
green_function = ["y"]
blue_function = ["x"]

red_function = build_random_function(9, 15)
green_function = build_random_function(9, 15)
blue_function = build_random_function(9, 15)
x = random.uniform(0, 1)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this line of code doing? Perhaps add an inline comment explaining why you added this line of code.

y = random.uniform(0, 1)
# Create image and loop over all pixels
im = Image.new("RGB", (x_size, y_size))
pixels = im.load()
Expand All @@ -160,12 +274,15 @@ def generate_art(filename, x_size=350, y_size=350):
if __name__ == '__main__':
import doctest
doctest.testmod()
#print(build_random_function(7,9))
# doctest.run_docstring_examples(remap_interval, globals(), verbose=True)

# Create some computational art!
# TODO: Un-comment the generate_art function call after you
# implement remap_interval and evaluate_random_function
# generate_art("myart.png")
for i in range(10):
generate_art("Test6." + str(i) + ".png")

# Test that PIL is installed correctly
# TODO: Comment or remove this function call after testing PIL install
test_image("noise.png")
#test_image("noise1.png")