-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprocessing_circular_bubbles.pde
More file actions
322 lines (253 loc) · 6.45 KB
/
processing_circular_bubbles.pde
File metadata and controls
322 lines (253 loc) · 6.45 KB
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
312
313
314
315
316
317
318
319
320
321
322
import com.hamoid.*;
/**
* Sketch
*
* Circular bubbles Art
* Description of the graph made in readme file
*
*/
/**
* Inputs
*/
String INPUT_FILE = "1901717622.csv"; // File in /data folder
float SCALE=1.5; // Scale factor to adapt to other resolutions quickly
int SPEED_MASS_BUBBLE=50; // speed of bubbles
float EASING = 0.05; // controls the smoothness of bubbles deceleration
/**
* Variables initialisation
*/
Table table; // csv table
int nb_rows; // size data matrix
int nb_cols; // size data matrix
int[][] array; // data matrix
int[] minmax;
int counter = 0; // temporality counter
VideoExport videoExport;
float pi=3.14;
ArrayList<Bubble> bubbleStack = new ArrayList<Bubble>(); // bubbles waiting to appear in the animation
ArrayList<Bubble> bubbleArray = new ArrayList<Bubble>(); // bubbles on the sketch
void setup() {
// Size of the canvas
size(1920, 1080);
// Launch recording
videoExport = new VideoExport(this);
videoExport.startMovie();
// Load csv data
table = loadTable("../data/" + INPUT_FILE, "csv");
println(table.getRowCount() + " total rows in table");
nb_rows = table.getRowCount();
nb_cols = table.getRow(0).getColumnCount();
array = new int[nb_rows][nb_cols];
// Ingest data in a matrix
for (int i=0; i<nb_rows;i+=1) {
for (int j=0; j < nb_cols; j+=1 ) {
array[i][j] = table.getRow(i).getInt(j);
print(array[i][j]);
print(", ");
}
println(" ");
}
// Ingest min max of each row in an array
minmax = findMinMaxArray();
print("Data successfully loaded");
// Create bubble
for (int i=0; i < nb_rows; i += 1) {
for (int j=0; j < nb_cols; j += 1 ) {
bubbleStack.add(
new Bubble(
int(array[i][0] * 3 * SCALE),
array[i][1] - 90,
int(SCALE * (array[i][2] + 5) / 2),
array[i][3])
);
}
}
print("Bubbles created !");
}
/**
* class Bubble
*/
public class Bubble {
int target_radius;
int target_angle;
int target_size;
int genre; // genre id corresponding to the color of the bubble
float opacity;
int current_radius;
int current_angle;
int current_size;
float current_opacity;
// Create bubble params
public Bubble(int a, int b, int c, int d){
target_radius = a;
target_angle = b;
target_size = c * 100 / minmax[1];
genre = d;
float rnd = random(40);
current_radius = target_radius + int(SCALE*(rnd+500));
current_angle = target_angle + int(SCALE*rnd);
current_size = target_size + int(SCALE*100);
opacity = random(80) + 20;
current_opacity = opacity;
}
// UpdateParams change location and size of the bubble frame after frame
void updateParams(){
float dr = target_radius - current_radius;
float da = target_angle - current_angle;
float ds = target_size - current_size;
float ddo = opacity - current_opacity;
current_radius += dr * EASING;
current_angle += da * EASING;
current_size += ds * EASING;
current_opacity += ddo * EASING;
}
void drawBubble(){
printBubble(current_radius, current_angle, current_size, genre, current_opacity);
}
void bounce(){
current_size += random(20);
}
void flicker(){
current_opacity += random(20);
}
}
/**
*
* Bubble drawing methods
* A bubble is just a circle with a color and size
*
**/
void printBubble(int radius, int angle, int size, int genre, float opacity){
// set color
color C = getColorFromGenre(genre, opacity);
fill(C);
noStroke();
strokeWeight(0);
drawCircle(radius, angle, size);
}
void drawCircle(int radius, int angle, int size){
float[] cart = getCartFrompolar(radius, angle);
ellipse(cart[0], cart[1], size, size);
}
/**
* Draw method called at each frame
*
* - reinitialise the canvas to white
* - Put new bubbles in the canvas
* - update all bubbles in the canvas parameters
* - actually draw them with their new parameters
*
*/
void draw() {
// reinit background
background(255);
// Add new bubbles to the canvas
counter +=1 ;
if (counter > 1){
counter = 0;
int mini_count = 0;
while(mini_count < SPEED_MASS_BUBBLE){
mini_count += 1;
if(bubbleStack.size() > 0){
Bubble b = bubbleStack.remove(0);
bubbleArray.add(b);
}
}
}
// update params and draw bubles
for (Bubble bub : bubbleArray) {
bub.updateParams();
bub.drawBubble();
}
videoExport.saveFrame();
}
/**
* User interactions
* Make the graph react to ome key pressed.
*/
void keyPressed() {
if (key == 'q') {
videoExport.endMovie();
exit();
}
if (key == 'b') {
makeItBounce();
}
if (key == 'f') {
makeItFlicker();
}
}
void makeItBounce(){
for (Bubble bub : bubbleArray) {
bub.bounce();
}
}
void makeItFlicker(){
for (Bubble bub : bubbleArray) {
bub.flicker();
}
}
/**
* Utils functions
*/
/**
* findMinMaxArray
* find Min and Max entry from the data for each row
*/
int[] findMinMaxArray(){
int min = 10000;
int max = 0;
for (int i=0; i < nb_rows; i += 1) {
if (array[i][2] > max){
max = array[i][2];
}
if (array[i][2] < min){
min = array[i][2];
}
}
int[] res = {min, max};
return res;
}
/**
* Color matching, ID to RGB
*/
color getColorFromGenre(int genre, float opacity){
color C=color(0,0,0);
switch (genre) {
case 0: C = color(244, 219, 255, opacity);
break;
case 1: C = color(249, 244, 79, opacity);
break;
case 2: C = color(255, 89, 84, opacity);
break;
case 3: C = color(65, 125, 193, opacity);
break;
case 4: C = color(31, 102, 30, opacity);
break;
case 5: C = color(140, 104, 53, opacity);
break;
case 6: C = color(177, 71, 198, opacity);
break;
case 7: C = color(88, 82, 99, opacity);
break;
case 8: C = color(59, 91, 147, opacity);
break;
case 9: C = color(68, 68, 68, opacity);
break;
}
return C;
}
/**
* Get cartesian coordinates from polar ones
*/
float[] getCartFrompolar(int radius, int angle){
int center_x = width / 2;
int center_y = height / 2;
float theta = angle * pi / 180;
float new_x = center_x + radius * cos(theta);
float new_y = center_y + radius * sin(theta);
float[] cart = new float[2];
cart[0] = new_x;
cart[1] = new_y;
return cart;
}