-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime_lines.pde
311 lines (236 loc) · 6.49 KB
/
time_lines.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
302
303
304
305
306
307
308
309
310
311
float min_vel = 20.0;
float max_vel = 50.0;
float min_length = 100;
float max_length = 500;
float min_chronoton_radius = 2.5;
float max_chronoton_radius = 7.5;
float delta_chronoton_radius = max_chronoton_radius - min_chronoton_radius;
float time_line_spawn_probability = 0.9;
float chronoton_spawn_probability = 0.4;
class Chronoton {
Vector direction;
Vector pos;
float v;
TimeLine src_tl;
color c;
float t;
boolean off_screen;
public Chronoton(TimeLine src_tl) {
this.src_tl = src_tl;
Vector src_dir = src_tl.direction();
this.direction = new Vector(src_dir.x-src_dir.y, src_dir.y+src_dir.x);
Vector src_pos = src_tl.position();
this.pos = new Vector(src_pos.x, src_pos.y);
this.v = src_tl.velocity();
this.c = src_tl.get_color();
this.t = 0;
this.off_screen = false;
}
public void update(float dt) {
t += dt;
float d = v*dt;
pos.x += d*direction.x;
pos.y += d*direction.y;
if ((pos.x < 0) || (width < pos.x) || (pos.y < 0) || (height < pos.y)) {
this.off_screen = true;
}
}
public void draw() {
float radius = min_chronoton_radius + delta_chronoton_radius*(1+ sin(t))/2;
pushStyle();
noStroke();
fill(this.source().get_color());
ellipse(pos.x, pos.y, 2*radius, 2*radius);
popStyle();
}
public boolean is_off_screen() {
return this.off_screen;
}
public Vector position() {
return pos;
}
public TimeLine source() {
return src_tl;
}
}
class TimeLine {
Vector direction;
Vector pos;
Vector dim;
float v;
float l;
color c;
boolean off_screen;
Chronoton chrono;
Chronoton entangled_chrono;
public TimeLine(Vector dir, float v, Vector pos, color c, float l, Vector dim) {
this.direction = dir;
this.pos = pos;
this.dim = dim;
this.c = c;
this.v = v;
this.l = l;
off_screen = false;
chrono = null;
entangled_chrono = null;
}
public void update(float dt) {
float d = this.velocity()*dt;
pos.x += d*direction.x;
pos.y += d*direction.y;
}
public void draw() {
Vector end_pos = new Vector(pos.x - l*direction.x, pos.y - l*direction.y);
//end_pos.x = max(0, end_pos.x);
//end_pos.y = max(0, end_pos.y);
//Vector start_pos = new Vector(min(pos.x, dim.x), min(pos.y, dim.y));
Vector start_pos = new Vector(pos.x, pos.y);
//print("" + start_pos.x + " " + start_pos.y + " " + end_pos.x + " " + end_pos.y + "\n");
pushStyle();
stroke(this.get_color());
strokeWeight(2);
line(end_pos.x, end_pos.y, start_pos.x, start_pos.y);
popStyle();
if ((width < end_pos.x) || (height < end_pos.y)) {
off_screen = true;
}
}
public boolean is_off_screen() {
return this.off_screen;
}
private color get_color(TimeLine src) {
if ((null != this.entangled_chrono) && (src != this.entangled_chrono.source())){
return this.entangled_chrono.source().get_color(src);
} else {
return this.c;
}
}
public color get_color() {
return this.get_color(this);
}
public Vector direction() {
return this.direction;
}
public Vector position() {
return this.pos;
}
private float velocity(TimeLine src) {
if ((null != this.entangled_chrono) && (src != this.entangled_chrono.source())) {
return this.entangled_chrono.source().velocity(src);
} else {
return this.v;
}
}
public float velocity() {
return this.velocity(this);
}
public Chronoton spawn_chronoton() {
this.chrono = new Chronoton(this);
return this.chrono;
}
public boolean has_spawned_chronoton() {
return (this.chrono != null);
}
private void entangle(Chronoton c, TimeLine src) {
if ((null != this.entangled_chrono) && (this.entangled_chrono.source() != src)) {
this.entangled_chrono.source().entangle(c, src);
}
this.entangled_chrono = c;
}
public void entangle(Chronoton c) {
this.entangle(c, this);
}
}
TimeLine random_time_line(int w, int h) {
float d = sqrt(w*w + h*h);
Vector direction = new Vector((float)w/d,(float)h/d);
Vector position;
Vector dimension = new Vector(w, h);
int r = (int)random(0, w+h);
r = r - (r%40);
if (r < w) {
position = new Vector(r, 0);
} else {
position = new Vector(0, r-w);
}
float l = random(min_length, max_length);
float v = random(min_vel, max_vel);
color c = color(random(255), random(255), random(255));
/*
println("d = " + d);
println("direction = " + direction.x + " " + direction.y);
println("position = " + position.x + " " + position.y);
println("dimension = " + dimension.x + " " + dimension.y);
println("l = " + l);
println("v = " + v);
println("c = " + hex(c));
*/
return new TimeLine(direction, v, position, c, l, dimension);
}
ArrayList<TimeLine> tls;
ArrayList<Chronoton> chronos;
int last_millis;
void setup() {
size(800, 600, P2D);
tls = new ArrayList<TimeLine>();
chronos = new ArrayList<Chronoton>();
last_millis = millis();
}
int cnt = 0;
void draw() {
cnt++;
pushStyle();
fill(255);
noStroke();
rect(0,0,width,height);
popStyle();
int cur_millis = millis();
float dt = (cur_millis - last_millis)/1000.0;
last_millis = cur_millis;
for (int i = 0; i < tls.size(); i++)
{
TimeLine tl = tls.get(i);
tl.update(dt);
tl.draw();
if (tl.is_off_screen()) {
tls.remove(i);
} else {
if (!tl.has_spawned_chronoton())
{
if (random(1) < dt*chronoton_spawn_probability) {
//println("adding new chronoton.");
Chronoton c = tl.spawn_chronoton();
chronos.add(c);
}
}
}
}
if (random(1) < (dt*time_line_spawn_probability)) {
TimeLine tl = random_time_line(width, height);
tls.add(tl);
//println("count since last spawn: " + cnt);
cnt = 0;
}
for (int i = 0; i < chronos.size(); i++)
{
Chronoton c = chronos.get(i);
boolean remove = false;
c.update(dt);
c.draw();
for (TimeLine tl : tls) {
if (tl != c.source()) {
if (vector_distance(tl.position(), c.position()) <= max_chronoton_radius) {
//println("entangle!");
tl.entangle(c);
remove = true;
}
}
}
if (c.is_off_screen()) {
remove = true;
}
if (remove) {
chronos.remove(i);
}
}
}