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

Completed Project 2 #15

Open
wants to merge 1 commit 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 Reflection.pdf
Binary file not shown.
Binary file added example1.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 example2.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 modified noise.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
87 changes: 70 additions & 17 deletions recursive_art.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
"""TODO: Put your header comment here."""
"""
Recursive Art
-Generates image of pixels whose colors are determined by random equations
By Siena
"""

import random
import math
from PIL import Image


Expand All @@ -17,12 +22,50 @@ def build_random_function(min_depth, max_depth):

Returns:
The randomly generated function represented as a nested list.
(See the assignment writ-eup for details on the representation of
(See the assignment write-up for details on the representation of
these functions)
"""
# TODO: implement this
pass

#TODO: make depth random
if max_depth == 0:
xOry = random.randint(1,2)
if xOry == 1:
return ['x']
else:
return ['y']
if (min_depth ==0) and (random.randint(1,2)==1):
xOry = random.randint(1,2)
if xOry == 1:
return ['x']
else:
return ['y']
else:
myInt = random.randint(1,4)
if myInt == 1:
l1 = ['prod']
l2 = (build_random_function(min_depth-1, max_depth-1))
l3 = (build_random_function(min_depth-1, max_depth-1))
l1.append(l2)
l1.append(l3)
return l1
elif myInt == 2:
l1 = ['avg']
l2 = (build_random_function(min_depth-1, max_depth-1))
l3 = (build_random_function(min_depth-1, max_depth-1))
l1.append(l2)
l1.append(l3)
return l1
elif myInt == 3:
l1 = ['cos_pi']
l2 = (build_random_function(min_depth-1, max_depth-1))
l1.append(l2)
return l1
elif myInt == 4:
l1 = ['sin_pi']
l2 = (build_random_function(min_depth-1, max_depth-1))
l1.append(l2)
return l1

print("ran func: " + (str)(build_random_function(3,4)))
Copy link

@mightydeveloper mightydeveloper Oct 11, 2017

Choose a reason for hiding this comment

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

  • You can remove to TODO comment at the top
  • If you use l = lambda : build_random_function(min_depth-1, max_depth-1), and use l() whenever you need them, you don't need to repeat l2, l3 codes and write out similar code for each if statement.
    for example, the code would look like
elif myInt == 2:
    return ['avg', l(), l()]
  • I'm not sure what the print code is doing... shouldn't it be str(build_random_function(3,4)) ??
  • Overall, great job for the concise code!


def evaluate_random_function(f, x, y):
"""Evaluate the random function f with inputs x,y.
Expand All @@ -43,9 +86,18 @@ def evaluate_random_function(f, x, y):
>>> evaluate_random_function(["y"],0.1,0.02)
0.02
"""
# TODO: implement this
pass

if f[0] == 'x':
return x
elif f[0] == 'y':
return y
elif f[0] == 'prod':
return (evaluate_random_function(f[1], x, y))*(evaluate_random_function(f[2], x, y))
elif f[0] == 'avg':
return ((evaluate_random_function(f[1], x, y))+(evaluate_random_function(f[2], x, y))/2)
elif f[0] == 'cos_pi':
return math.cos(math.pi*evaluate_random_function(f[1], x, y))
elif f[0] == 'sin_pi':
return math.sin(math.pi*evaluate_random_function(f[1], x, y))

def remap_interval(val,
input_interval_start,
Expand All @@ -66,7 +118,7 @@ def remap_interval(val,
values for val
output_interval_start: the start of the interval that contains all
possible output values
output_inteval_end: the end of the interval that contains all possible
output_interval_end: the end of the interval that contains all possible
output values

Returns:
Expand All @@ -80,8 +132,10 @@ def remap_interval(val,
>>> remap_interval(5, 4, 6, 1, 2)
1.5
"""
# TODO: implement this
pass
range1 = input_interval_end - input_interval_start
range2 = output_interval_end - output_interval_start
scaledVal = val-input_interval_start
return output_interval_start + (range2/range1)*scaledVal


def color_map(val):
Expand Down Expand Up @@ -137,9 +191,9 @@ 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(7,9)
green_function = build_random_function(7,9)
blue_function = build_random_function(7,9)

# Create image and loop over all pixels
im = Image.new("RGB", (x_size, y_size))
Expand All @@ -153,7 +207,6 @@ def generate_art(filename, x_size=350, y_size=350):
color_map(evaluate_random_function(green_function, x, y)),
color_map(evaluate_random_function(blue_function, x, y))
)

im.save(filename)


Expand All @@ -164,8 +217,8 @@ def generate_art(filename, x_size=350, y_size=350):
# 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")
generate_art("myart.png")

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