1
- """TODO: Put your header comment here."""
1
+ """
2
+ Victor Bianchi - Software Design - Fall 2017
3
+ MiniProject 2 : Computational Art
4
+ """
2
5
3
6
import random
7
+ from random import randint
8
+ import math
9
+ from math import cos , sin , tan , pi
4
10
from PIL import Image
5
11
6
12
@@ -20,9 +26,25 @@ def build_random_function(min_depth, max_depth):
20
26
(See the assignment writ-eup for details on the representation of
21
27
these functions)
22
28
"""
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 )]
26
48
27
49
def evaluate_random_function (f , x , y ):
28
50
"""Evaluate the random function f with inputs x,y.
@@ -43,9 +65,23 @@ def evaluate_random_function(f, x, y):
43
65
>>> evaluate_random_function(["y"],0.1,0.02)
44
66
0.02
45
67
"""
46
- # TODO: implement this
47
- pass
48
68
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 ))
49
85
50
86
def remap_interval (val ,
51
87
input_interval_start ,
@@ -80,9 +116,9 @@ def remap_interval(val,
80
116
>>> remap_interval(5, 4, 6, 1, 2)
81
117
1.5
82
118
"""
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
86
122
87
123
def color_map (val ):
88
124
"""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):
137
173
x_size, y_size: optional args to set image dimensions (default: 350)
138
174
"""
139
175
# 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 )
143
179
144
180
# Create image and loop over all pixels
145
181
im = Image .new ("RGB" , (x_size , y_size ))
@@ -159,13 +195,11 @@ def generate_art(filename, x_size=350, y_size=350):
159
195
160
196
if __name__ == '__main__' :
161
197
import doctest
162
- doctest .testmod ()
198
+ # doctest.testmod(verbose = True )
163
199
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")
168
202
169
203
# Test that PIL is installed correctly
170
204
# TODO: Comment or remove this function call after testing PIL install
171
- test_image ("noise.png" )
205
+ # test_image("noise.png")
0 commit comments