Skip to content

Commit ee8491e

Browse files
committed
Completed Mini Project sd17fall#2
1 parent 9d40abd commit ee8491e

File tree

1 file changed

+52
-18
lines changed

1 file changed

+52
-18
lines changed

recursive_art.py

+52-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
"""TODO: Put your header comment here."""
1+
"""
2+
Victor Bianchi - Software Design - Fall 2017
3+
MiniProject 2 : Computational Art
4+
"""
25

36
import random
7+
from random import randint
8+
import math
9+
from math import cos, sin, tan, pi
410
from PIL import Image
511

612

@@ -20,9 +26,25 @@ def build_random_function(min_depth, max_depth):
2026
(See the assignment writ-eup for details on the representation of
2127
these functions)
2228
"""
23-
# TODO: implement this
24-
pass
25-
29+
building_blocks = ['x','y','cos_pi','sin_pi','prod','avg','tan_pi','substract',]
30+
pick = random.choice(building_blocks)
31+
i = random.randint(min_depth, max_depth)
32+
33+
if max_depth <= 1:
34+
return [random.choice(['x','y','t'])]
35+
36+
elif pick == 'cos_pi':
37+
return ['cos_pi', build_random_function(min_depth-1, max_depth-1)]
38+
elif pick == 'sin_pi':
39+
return ['sin_pi', build_random_function(min_depth-1, max_depth-1)]
40+
elif pick == 'tan_pi':
41+
return ['tan_pi', build_random_function(min_depth-1, max_depth-1)]
42+
elif pick == 'prod':
43+
return ['prod', build_random_function(min_depth-1, max_depth-1), build_random_function(min_depth-1, max_depth-1)]
44+
elif pick == 'avg':
45+
return ['avg', build_random_function(min_depth-1, max_depth-1), build_random_function(min_depth-1, max_depth-1)]
46+
elif pick == 'substract':
47+
return ['substract', build_random_function(min_depth-1, max_depth-1), build_random_function(min_depth-1, max_depth-1)]
2648

2749
def evaluate_random_function(f, x, y):
2850
"""Evaluate the random function f with inputs x,y.
@@ -43,9 +65,23 @@ def evaluate_random_function(f, x, y):
4365
>>> evaluate_random_function(["y"],0.1,0.02)
4466
0.02
4567
"""
46-
# TODO: implement this
47-
pass
4868

69+
if f[0] == 'x':
70+
return x
71+
elif f[0] == 'y':
72+
return y
73+
elif f[0] == 'avg':
74+
return 0.5 (evaluate_random_function(f[1],x,y) + evaluate_random_function(f[2],x,y))
75+
elif f[0] == 'prod':
76+
return (evaluate_random_function(f[1],x,y) * evaluate_random_function(f[2],x,y))
77+
elif f[0] == 'substract':
78+
return (evaluate_random_function(f[1],x,y) - evaluate_random_function(f[2],x,y))
79+
elif f[0] == 'sin_pi':
80+
return math.sin(math.pi*evaluate_random_function(f[1],x,y))
81+
elif f[0] == 'tan_pi':
82+
return math.tan(math.pi*evaluate_random_function(f[1],x,y))
83+
elif f[0] == 'cos_pi':
84+
return math.cos(math.pi*evaluate_random_function(f[1],x,y))
4985

5086
def remap_interval(val,
5187
input_interval_start,
@@ -80,9 +116,9 @@ def remap_interval(val,
80116
>>> remap_interval(5, 4, 6, 1, 2)
81117
1.5
82118
"""
83-
# TODO: implement this
84-
pass
85-
119+
output_val = input_interval_end - input_interval_start
120+
input_val = output_interval_end - output_interval_start
121+
return ((float(val - input_interval_start) * (input_val) / output_val)) + output_interval_start
86122

87123
def color_map(val):
88124
"""Maps input value between -1 and 1 to an integer 0-255, suitable for use as an RGB color code.
@@ -137,9 +173,9 @@ def generate_art(filename, x_size=350, y_size=350):
137173
x_size, y_size: optional args to set image dimensions (default: 350)
138174
"""
139175
# Functions for red, green, and blue channels - where the magic happens!
140-
red_function = ["x"]
141-
green_function = ["y"]
142-
blue_function = ["x"]
176+
red_function = build_random_function(7, 9)
177+
green_function = build_random_function(7, 9)
178+
blue_function = build_random_function(7, 9)
143179

144180
# Create image and loop over all pixels
145181
im = Image.new("RGB", (x_size, y_size))
@@ -159,13 +195,11 @@ def generate_art(filename, x_size=350, y_size=350):
159195

160196
if __name__ == '__main__':
161197
import doctest
162-
doctest.testmod()
198+
#doctest.testmod(verbose = True)
163199

164-
# Create some computational art!
165-
# TODO: Un-comment the generate_art function call after you
166-
# implement remap_interval and evaluate_random_function
167-
# generate_art("myart.png")
200+
#generate_art("example1.png")
201+
#generate_art("example2.png")
168202

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

0 commit comments

Comments
 (0)