-
Notifications
You must be signed in to change notification settings - Fork 1
/
RoutineKaleidoscope.pde
287 lines (234 loc) · 5.89 KB
/
RoutineKaleidoscope.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
public class KaleFactory implements RoutineFactory {
public Routine create(PApplet parent) {
return new RoutineKaleidoscope();
}
}
public class RoutineKaleidoscope extends Routine {
public RoutineKaleidoscope() {
super(450, 450);
}
PImage img;
LightArcs arcs;
Shockwave wave;
Kaleidoscope ks;
public void setup() {
arcs = new LightArcs();
wave = new Shockwave();
ks = new Kaleidoscope();
}
public void draw() {
pg.colorMode(RGB);
pg.background(0);
arcs.step();
wave.step();
if (ks.step()) {
arcs.reset();
wave.reset();
ks.reset();
}
arcs.draw();
wave.draw();
ks.draw();
}
private class Kaleidoscope {
PImage image;
ArrayList<ImageSpin> spinners;
int count;
public Kaleidoscope() {
this.image = image;
this.spinners = new ArrayList<ImageSpin>();
this.reset();
}
public void reset() {
File dir = new File(dataPath("images"));
File[] files = dir.listFiles();
File file = files[int(random(0,files.length))];
this.image = loadImage(file.getAbsolutePath());
this.count = 1;
this.setupSpinners();
}
private void setupSpinners() {
int size = int(400 / (this.count * 0.75));
int offset = this.count > 1 ? 90 : 0;
spinners.clear();
for (int i=0; i<this.count; i++) {
spinners.add(new ImageSpin(image, size, offset, TWO_PI/this.count*i));
}
}
public boolean step() {
boolean result = true;
for (ImageSpin spinner : spinners) {
result = spinner.step() && result;
}
if (result) {
this.count = this.count * 2;
this.setupSpinners();
if (this.count >= 8)
return true;
}
return false;
}
public void draw() {
for (ImageSpin spinner : spinners) {
spinner.draw();
}
}
}
private class ImageSpin {
float angle;
float size;
PImage image;
int count;
float magnitude;
int stickFrames;
float maxSize;
float offset;
public ImageSpin(PImage image, float maxSize, float offset, float angle) {
this.image = image;
this.angle = angle;
this.size = 10;
this.magnitude = 1.03;
this.stickFrames = 300;
this.maxSize = maxSize;
this.offset = offset;
}
public boolean step() {
this.angle += PI/100;
this.size = this.size * this.magnitude;
if (this.size > maxSize) {
this.size = maxSize;
this.stickFrames--;
if (this.stickFrames <= 0)
this.magnitude = 0.9;
}
else if (this.size < 10) {
this.magnitude = 1.03;
this.size = 10;
this.stickFrames = 300;
return true;
}
return false;
}
public void draw() {
pg.pushMatrix();
pg.translate(180,180);
if (this.offset > 0) {
pg.rotate(this.angle);
pg.translate(this.offset, this.offset);
}
pg.rotate(this.angle);
pg.translate(-this.size/2, -this.size/2);
pg.image(this.image, 0, 0, this.size, this.size);
pg.popMatrix();
}
}
private class Shockwave {
float radius;
boolean done;
public Shockwave() {
this.reset();
}
public void reset() {
radius = 1.05;
done = false;
}
public boolean step() {
if (!done) {
radius *= 1.07;
done = radius > 2000;
}
return done;
}
public void draw() {
if (!done && radius>10) {
pg.ellipseMode(RADIUS);
pg.noStroke();
pg.fill(0);
pg.ellipse(180,180,radius,radius);
if (radius>20) {
pg.fill(getColor());
pg.ellipse(180,180,radius*.85,radius*.85);
}
if (radius > 50) {
pg.fill(0);
pg.ellipse(180,180,radius-50,radius-50);
}
}
}
}
private class LightArcs {
boolean done;
LightArc[] arcs;
public LightArcs() {
arcs = new LightArc[16];
for (int i=0; i<arcs.length; i++) {
arcs[i] = new LightArc();
}
done = false;
}
public boolean step() {
boolean result = true;
for (int i=0; i<arcs.length; i++) {
result = arcs[i].step() && result;
}
done = result;
return result;
}
public void reset() {
for (int i=0; i<arcs.length; i++) {
arcs[i].reset();
}
}
public void draw() {
if (!done) {
for (int i=0; i<arcs.length; i++) {
arcs[i].draw();
}
}
}
}
private class LightArc {
float angle;
int size;
int len;
public LightArc() {
this.reset();
}
public LightArc(float angle, int size, int len) {
this.angle = angle;
this.size = size;
this.len = len;
}
public boolean step() {
if (len >= 360) {
size = int(size * 1.05);
}
else {
len = int(len * 1.1);
}
return size > 200;
}
public void reset() {
this.angle = random(0, TWO_PI);
this.size = int(random(20, 60));
this.len = int(random(10, 20));
}
public void draw() {
int half = int(size/2 * (len/360.0));
color col = getColor();
pg.pushMatrix();
pg.pushStyle();
pg.translate(180, 180);
pg.rotate(angle);
pg.noStroke();
pg.fill(red(col),green(col),blue(col),64);
pg.triangle(0, 0, half+7, len, -half-7, len);
pg.fill(red(col),green(col),blue(col),127);
pg.triangle(0, 0, half+5, len, -half-5, len);
pg.fill(col);
pg.triangle(0, 0, half, len, -half, len);
pg.popStyle();
pg.popMatrix();
}
}
}