· 8 years ago · Mar 10, 2018, 10:34 AM
1// Based on Example Written by Casey Reas and Ben Fry
2// Edited by Tom Bar-Gal
3
4
5
6//particlesystem()
7//addparticle()
8//particle()
9
10
11
12// ========== Table Data Stuff
13
14Table table;
15int k = 0;
16String[] destCountryArray = new String[0];
17String[] sourceCountryArray = new String[0];
18
19String destCountry = "";
20String prevDestCountry = "";
21
22String sourceCountry;
23
24// ========
25
26
27int maxParticles = 12000;
28
29ParticleSystem ps;
30ParticleSystem ps2;
31
32int n = 0, n2=0;
33int emmitMultiplyer = 1;
34int emmitFreq = 1;
35float particleSpeed = 0.002;
36
37float locationX = 250;
38float locationY = 450;
39
40int[] sourceX = {10, 40, 200, 400, 700};
41int[] destX = {300, 600, 300, 600, 600};
42int[] amount = {10, 100, 500, 800, 1000};
43int highestAmount = max(amount);
44
45// a,b,c... max*a/{a+b+c...}
46
47ParticleSystem[] PSystems;
48
49
50void setup() {
51 size(1200, 800);
52
53 //=============== load table and create an array of countries
54
55 table = loadTable("asylum_seekers.csv", "header");
56
57 destCountryArray = (String[]) append(destCountryArray, "Israel");
58
59 for (TableRow row : table.rows()) {
60 //println("going over row" + row.getString("Country / territory of asylum/residence"));
61 String tempCountryHolder = row.getString("Country / territory of asylum/residence");
62 //println("Got a temp country holder" + tempCountryHolder);
63 boolean exists = countryExists(tempCountryHolder);
64 if (exists==true) {
65 //println("exists, skipping");
66 continue;
67 }
68 println("Appending "+tempCountryHolder+" to list of length " +destCountryArray.length);
69 destCountryArray = (String[]) append(destCountryArray, tempCountryHolder);
70 println("destCountryArray length = "+ destCountryArray.length);
71 }
72
73 //============================
74
75 PSystems = new ParticleSystem[destCountryArray.length];
76 //frameRate(30);
77 //colorMode(RGB,255,255,255,255);
78 for (int i = 0; i<destCountryArray.length; i++) {
79
80 // Particle Systems syntax = multiplyer, source, destination, amount);
81 PSystems[i] = new ParticleSystem(1, new Vector3D(i*40+40, 100, 0), new Vector3D(i*40+40, 500, 0), 1/(i+1));
82 //println("PSystems " + i + " is " +PSystems[i]);
83 }
84
85 //ps = new ParticleSystem(1, new Vector3D(width/2, height/2, 0));
86 //ps2 = new ParticleSystem(1, new Vector3D(100, 200, 0));
87
88 smooth();
89}
90
91void draw() {
92
93 background(250);
94 //ellipse(locationX, locationY, 5, 5);
95 //ellipse(width/2, height/2, 5, 5);
96 //ellipse(100, 200, 5, 5);
97 //println(PSystems.length);
98
99 for (int i = 0; i<destCountryArray.length; i++) {
100 //println(PSystems[i]);
101 PSystems[i].run();
102 }
103
104
105 for (int i = 0; i<emmitMultiplyer; i++) {
106 for (int k = 0; k<destCountryArray.length; k++) {
107 if (frameCount % (k+1) == 0) {
108 PSystems[k].addParticle();
109 n++;
110 }
111 }
112 }
113
114 n2+=emmitMultiplyer;
115
116 fill(0);
117 text("Frame rate: "
118 + int(frameRate), 10, 20);
119 println(n);
120 println(n);
121}
122
123
124
125
126// ==============================// A simple Particle class // ===============================================//
127
128
129
130
131class Particle {
132
133 Vector3D loc;
134 Vector3D des;
135 Vector3D vel;
136 Vector3D acc;
137 Vector3D locHome, b, c;
138 float relativeSpeed;
139
140 float r;
141 float timer;
142
143 float t=0.0;
144
145 // Another constructor (the one we are using here)
146 Particle(Vector3D l, Vector3D m) {
147
148 //acc = new Vector3D(0,0.0005,0); // particle acceleration
149
150 acc = new Vector3D(0, 0, 0); // new Vector3D(random(-0.1, 0.1), random(-0.02, 0), 0);
151 loc = l.copy();
152 des = m.copy();
153 locHome = l.copy();
154 locHome.x = locHome.x+random(-2, 2);
155 locHome.y = locHome.y+random(-2, 2);
156 des.x = des.x+random(-2, 2);
157 des.y=des.y+random(-2, 2);
158 relativeSpeed = random(0.5, 1.2);
159
160 r = random(0.9, 2.3); // particle radius
161 timer = 10000.0; // particles lifespan
162 // * emmitMultiplyer = number of living
163
164 b=new Vector3D(locHome.x+random(-20, 20), locHome.y+random(120, 180), 0);
165 c=new Vector3D(des.x+random(-20, 30), des.y-random(120, 180), 0);
166 }
167
168 void run() {
169 update();
170 render();
171 }
172
173 // Method to update location
174 void update() {
175
176 if (t>=1)
177 return;
178
179 // https : // www.processing.org/reference/bezierPoint_.html
180
181 loc.x = bezierPoint(locHome.x, b.x, c.x, des.x, t);
182 loc.y = bezierPoint(locHome.y, b.y, c.y, des.y, t);
183
184 t = lerp(t, 1, particleSpeed*relativeSpeed);
185 //t+=particleSpeed*relativeSpeed;
186
187 // curvePoint(a, b, c, d, t)
188 // vel.add(acc);
189 // loc.add(vel);
190 //timer -= 1.0;
191 }
192
193 // Method to display
194 void render() {
195 ellipseMode(CENTER);
196 noStroke();
197 fill(70, 255);
198
199 ellipse(loc.x, loc.y, r, r);
200 }
201
202 // Is the particle still useful?
203 boolean dead() {
204// if (timer <= 0.0||t>=1.0) {
205 if (t>=0.95) {
206 return true;
207 } else {
208 return false;
209 }
210 }
211}
212
213
214// ==============================// A ParticleSystem // ===============================================//
215
216
217
218// A class to describe a group of Particles
219// An ArrayList is used to manage the list of Particles
220class ParticleSystem {
221
222 ArrayList particles; // An arraylist for all the particles
223 Vector3D origin; // An origin point for where particles are birthed
224 Vector3D dest;
225 int freq;
226
227 //ParticleSystem( number of particles / frame, source, destination, frequency);
228 ParticleSystem(int num, Vector3D v, Vector3D d, float f) {
229 particles = new ArrayList(); // Initialize the arraylist
230 origin = v.copy(); // Store the origin point
231 dest = d.copy();
232
233 //if (frameCount % (1/f) == 0){
234 for (int i = 0; i < num; i++) {
235 particles.add(new Particle(origin, dest)); // Add "num" amount of particles to the arraylist
236 }
237 //}
238 }
239 void run() {
240 // Cycle through the ArrayList backwards b/c we are deleting
241 for (int i = particles.size()-1; i >= 0; i--) {
242 Particle p = (Particle) particles.get(i);
243 p.run();
244 if (p.dead()) {
245 particles.remove(i);
246 n--;
247 }
248 }
249 }
250
251 void addParticle() {
252 particles.add(new Particle(origin, dest));
253 }
254
255 //void addParticle(Particle p) {
256 // particles.add(p);
257 //}
258
259 // A method to test if the particle system still has particles
260 boolean dead() {
261 if (particles.isEmpty()) {
262 return true;
263 } else {
264 return false;
265 }
266 }
267}
268
269
270//=================================================== Class Country
271public class Country {
272 public float countryIndex;
273 public float countryLocationX;
274 public float countryLocationY;
275 public float countryName;
276}
277
278// ================================================ Simple Vector3D Class
279public class Vector3D {
280
281 public float x;
282 public float y;
283 public float z;
284
285 Vector3D(float x_, float y_, float z_) {
286 x = x_;
287 y = y_;
288 z = z_;
289 }
290
291 Vector3D(float x_, float y_) {
292 x = x_;
293 y = y_;
294 z = 0f;
295 }
296
297 Vector3D() {
298 x = 0f;
299 y = 0f;
300 z = 0f;
301 }
302
303 void setX(float x_) {
304 x = x_;
305 }
306
307 void setY(float y_) {
308 y = y_;
309 }
310
311 void setZ(float z_) {
312 z = z_;
313 }
314
315 void setXY(float x_, float y_) {
316 x = x_;
317 y = y_;
318 }
319
320 void setXYZ(float x_, float y_, float z_) {
321 x = x_;
322 y = y_;
323 z = z_;
324 }
325
326 void setXYZ(Vector3D v) {
327 x = v.x;
328 y = v.y;
329 z = v.z;
330 }
331
332 public float magnitude() {
333 return (float) Math.sqrt(x*x + y*y + z*z);
334 }
335
336 public Vector3D copy() {
337 return new Vector3D(x, y, z);
338 }
339
340 public Vector3D copy(Vector3D v) {
341 return new Vector3D(v.x, v.y, v.z);
342 }
343
344 public void add(Vector3D v) {
345 x += v.x;
346 y += v.y;
347 z += v.z;
348 }
349
350 public void sub(Vector3D v) {
351 x -= v.x;
352 y -= v.y;
353 z -= v.z;
354 }
355
356 public void mult(float n) {
357 x *= n;
358 y *= n;
359 z *= n;
360 }
361
362 public void div(float n) {
363 x /= n;
364 y /= n;
365 z /= n;
366 }
367
368 public void normalize() {
369 float m = magnitude();
370 if (m > 0) {
371 div(m);
372 }
373 }
374
375 public void limit(float max) {
376 if (magnitude() > max) {
377 normalize();
378 mult(max);
379 }
380 }
381
382 public float heading2D() {
383 float angle = (float) Math.atan2(-y, x);
384 return -1*angle;
385 }
386
387 public Vector3D add(Vector3D v1, Vector3D v2) {
388 Vector3D v = new Vector3D(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
389 return v;
390 }
391
392 public Vector3D sub(Vector3D v1, Vector3D v2) {
393 Vector3D v = new Vector3D(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
394 return v;
395 }
396
397 public Vector3D div(Vector3D v1, float n) {
398 Vector3D v = new Vector3D(v1.x/n, v1.y/n, v1.z/n);
399 return v;
400 }
401
402 public Vector3D mult(Vector3D v1, float n) {
403 Vector3D v = new Vector3D(v1.x*n, v1.y*n, v1.z*n);
404 return v;
405 }
406
407 public float distance (Vector3D v1, Vector3D v2) {
408 float dx = v1.x - v2.x;
409 float dy = v1.y - v2.y;
410 float dz = v1.z - v2.z;
411 return (float) Math.sqrt(dx*dx + dy*dy + dz*dz);
412 }
413}
414
415boolean countryExists(String tempCountryHolder) {
416
417 for (int i = 0; i < destCountryArray.length; i++) {
418 //println("comparing '" +tempCountryHolder +"' with '"+destCountryArray[i] + "'");
419 if (tempCountryHolder.equals(destCountryArray[i]) == true) {
420 //println("found : "+ tempCountryHolder);
421 return true; // it exists
422 } //if
423 } //for
424 return false ; // not found
425}//func