-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathday_7.pde
301 lines (240 loc) · 4.91 KB
/
day_7.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
float margin = 50;
// Made x and y global values to simulate a lexical env
float x, y;
Grid grid;
ExpressionGenerator gen;
/*
* Main interface for the AST nodes
*/
interface ASTNode {
float eval();
}
/*
* Enum for operators
*/
enum Operation {
ADD("+"), SUBTRACT("-"), MULTIPLY("*");
String op;
private Operation(String op) {
this.op = op;
}
String toString() {
return op;
}
}
/*
* Return a random operation
*/
Operation randomOp() {
return Operation.values()[int(random((Operation.values().length)))];
}
/*
* Binary operator
*/
class BinOp implements ASTNode {
Operation op;
ASTNode left, right;
BinOp(Operation op, ASTNode left, ASTNode right) {
this.op = op;
this.left = left;
this.right = right;
}
float eval() {
float l = left.eval();
float r = right.eval();
switch (op) {
case ADD:
return l + r;
case SUBTRACT:
return l - r;
case MULTIPLY:
return l * r;
default:
return 1;
}
}
String toString() {
return left.toString() + op + right.toString();
}
}
/*
* Enum storing available function names
*/
enum FuncName {
// Give them a ratio so we can pick more cos over noise for example
COS(0.4), SIN(0.4), TAN(0.03), EXP(0.02), SQRT(0.05), NOISE(0.1);
float ratio;
private FuncName(float ratio) {
this.ratio = ratio;
}
String toString() {
return name().toLowerCase();
}
}
/*
* Return a random function name based on their ratio
*/
FuncName randomFuncName() {
float r = random(1);
int choosen = 0;
for (int i = 0; i < FuncName.values().length; i++) {
float ratio = FuncName.values()[i].ratio;
if (ratio <= r) {
choosen = i;
break;
}
}
return FuncName.values()[choosen];
}
/*
* Responsible to call processing math functions
*/
class FunCall implements ASTNode {
FuncName function;
ASTNode arg;
FunCall(FuncName function, ASTNode arg) {
this.function = function;
this.arg = arg;
}
float eval() {
float a = arg.eval();
switch(function) {
case COS:
return cos(a);
case SIN:
return sin(a);
case TAN:
return tan(a);
case EXP:
return exp(a);
case SQRT:
return sqrt(a);
case NOISE:
return noise(a);
default:
return 0;
}
}
String toString() {
return function + "(" + arg + ")";
}
}
/*
* Holds a simple float
*/
class Value implements ASTNode {
float value;
Value(float value) {
this.value = value;
}
float eval() {
return value;
}
String toString() {
return Float.toString(value);
}
}
/*
* return x global value (very silly)
*/
class XValue implements ASTNode {
float eval() {
return x;
}
String toString() {
return "x";
}
}
/*
* return y global value
*/
class YValue implements ASTNode {
float eval() {
return y;
}
String toString() {
return "y";
}
}
/*
* Time AST node return millis
*/
class Time implements ASTNode {
float eval() {
return millis() / 1000.0;
}
String toString() {
return "t";
}
}
/*
* This class can generate random functions
*/
class ExpressionGenerator {
// Recursive function to create a random AST
ASTNode generate(int depth) {
ASTNode left, right;
// If we are at the end, put x/y and time with an operation
if (depth == 1) {
left = new Time();
right = (random(1) < 0.5) ? new XValue() : new YValue();
return new BinOp(randomOp(), left, right);
}
// Randomly add a binop or a function call
if (random(1) < 0.4) {
left = generate(depth - 1);
right = generate(depth - 1);
return new BinOp(randomOp(), left, right);
} else {
ASTNode arg = generate(depth - 1);
return new FunCall(randomFuncName(), arg);
}
}
}
/*
* The actual grid
*/
class Grid {
int size;
// Stores a function as AST node
ASTNode function;
Grid(int size, ASTNode function) {
this.size = size;
this.function = function;
}
void setFunction(ASTNode function) {
this.function = function;
}
void display() {
float gap = (width - 2 * margin) / size;
float maxCircleDiameter = 0.9 * gap;
fill(0);
noStroke();
for (int i = 0; i < size; i++) {
float xPos = margin + gap / 2 + i * gap;
for (int j = 0; j < size; j++) {
float yPos = margin + gap / 2 + j * gap;
x = (float) i / size;
y = (float) j / size;
float radius = constrain(function.eval(), -1, 1) * maxCircleDiameter;
circle(xPos, yPos, radius);
}
}
textAlign(CENTER, CENTER);
text(function.toString(), width / 2, margin / 2);
}
}
void setup() {
size(500, 500);
gen = new ExpressionGenerator();
grid = new Grid(32, gen.generate(4));
}
void draw() {
background(255);
// Display the grid
grid.display();
// Every 150 frames regenerate a new function (random tree with depth 5)
if (frameCount % 150 == 0) {
grid.setFunction(gen.generate(5));
}
}