Skip to content

Commit c115eff

Browse files
committed
refactor flow fields, randomize starting points, parameterize most values, add key controls
1 parent 93e5c9d commit c115eff

File tree

2 files changed

+113
-476
lines changed

2 files changed

+113
-476
lines changed

flow_field/flow_field.pde

+113-37
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,149 @@
1-
import processing.svg.*;
1+
import processing.svg.*; //<>//
2+
3+
String _saveName = "flow_field_";
4+
float[][] grid;
5+
float[][] starting_points;
6+
float resolution;
7+
float num_columns, num_rows, angle, x, y;
8+
int left_x, right_x, top_y, bottom_y;
9+
int column_index, row_index;
10+
float grid_angle, x_step, y_step;
11+
float position_noise;
12+
13+
int _octave = round(random(8, 24));
14+
float step_length = random(1, 4);
15+
int num_steps = round(random(100, 200));
16+
float falloff = random(0.5, 0.9);
17+
int num_lines = 100;
218

319
void setup() {
420
// width, height in pixels
521
// slightly smaller than a5
6-
size(750, 500, SVG, "flow_field.svg");
22+
size(750, 500);
723
smooth();
824
strokeWeight(2);
9-
stroke(0,0,0);
25+
stroke(0, 0, 0);
1026
noFill();
11-
12-
// declare var
13-
float[][] grid;
14-
float resolution;
15-
float num_columns, num_rows, angle, x, y;
16-
int left_x, right_x, top_y, bottom_y;
17-
18-
// assign var
19-
left_x = int(width * -0.5);
20-
right_x = int(width * 1.5);
21-
top_y = int(height * -0.5);
22-
bottom_y = int(height * 1.5);
27+
28+
println(num_steps);
29+
println(falloff);
30+
31+
// make grid larger than paper so lines can flow off
32+
left_x = int(width * -2);
33+
right_x = int(width * 2);
34+
top_y = int(height * -2);
35+
bottom_y = int(height * 2);
2336
resolution = int(width * 0.01);
2437
num_columns = (right_x - left_x) / resolution;
2538
num_rows = (bottom_y - top_y) / resolution;
26-
39+
2740
grid = new float[int(num_columns)][int(num_rows)];
28-
41+
starting_points = new float[num_lines][2];
42+
2943
for (int column = 0; column < num_columns -1; column +=1) {
3044
for (int row = 0; row < num_rows - 1; row +=1) {
3145
float scaled_x = column * 0.005;
3246
float scaled_y = row * 0.005;
33-
noiseDetail(8,0.55);
47+
noiseDetail(_octave, falloff);
3448
float noise_value = noise(scaled_x, scaled_y);
3549
angle = map(noise_value, 0.0, 1.0, 0.0, PI * 2.0);
3650
grid[column][row] = angle;
3751
}
3852
}
39-
40-
for (y = 1; y < height; y = y+30) {
41-
for (x = 0; x < width; x = x+30) { //<>//
42-
float position_noise = noise(x, y) * 30;
43-
drawCurve(x + position_noise, y + position_noise, left_x, top_y, resolution, grid);
44-
}
53+
54+
for (int j = 1; j < num_lines; j = j+1) {
55+
float x = random(0, width);
56+
float y = random(0, height);
57+
float[] point = new float[2];
58+
point[0] = x;
59+
point[1] = y;
60+
starting_points[j] = point;
4561
}
46-
47-
//exit() required for SVG generation
48-
println("Done");
49-
exit();
5062
};
5163

52-
void drawCurve(float x, float y, int left_x, int top_y, float resolution, float[][] grid) {
53-
int num_steps, column_index, row_index;
54-
float step_length, grid_angle, x_step, y_step;
55-
num_steps = 60;
56-
step_length = 4;
64+
void draw() {
65+
for (int k = 1; k < num_lines; k = k+1) {
66+
float[] starting_point = new float[2];
67+
starting_point = starting_points[k];
68+
69+
drawCurves(starting_point[0], starting_point[1], left_x, top_y, resolution, grid);
70+
}
71+
}
72+
73+
void keyPressed() {
74+
if (key == 's' || key == 'S') {
75+
exportSVG();
76+
}
77+
78+
if (key == CODED) {
79+
if (keyCode == UP) {
80+
num_steps = num_steps + 10;
81+
} else if (keyCode == DOWN) {
82+
num_steps = num_steps -10;
83+
}
84+
}
85+
}
86+
87+
void exportSVG() {
88+
int name_falloff = round(falloff * 10);
89+
String exportName = _saveName + num_steps + "_" + name_falloff + ".svg";
90+
PGraphics pg = createGraphics(width, height, SVG, exportName);
91+
pg.beginDraw();
92+
pg.noFill();
93+
pg.strokeWeight(1);
94+
pg.stroke(0);
95+
pg.smooth();
96+
97+
for (int k = 1; k < num_lines; k = k+1) {
98+
float[] starting_point = new float[2];
99+
starting_point = starting_points[k];
100+
x = starting_point[0];
101+
y = starting_point[1];
102+
103+
pg.beginShape();
104+
105+
// drawCurves
106+
for (int i = 0; i < num_steps; i = i+1) {
107+
pg.vertex(x, y);
108+
109+
int x_offset = int(x) - left_x;
110+
int y_offset = int(y) - top_y;
111+
112+
column_index = int(x_offset / resolution);
113+
row_index = int(y_offset / resolution);
114+
115+
grid_angle = grid[column_index][row_index];
116+
117+
x_step = step_length * cos(grid_angle);
118+
y_step = step_length * sin(grid_angle);
119+
120+
x = x + x_step;
121+
y = y + y_step;
122+
}
123+
124+
pg.endShape();
125+
}
126+
127+
pg.endDraw();
128+
pg.dispose();
129+
println("saved " + exportName);
130+
}
131+
132+
void drawCurves(float x, float y, int left_x, int top_y, float resolution, float[][] grid) {
57133
beginShape();
58134
for (int i = 0; i < num_steps; i = i+1) {
59135
vertex(x, y);
60136
int x_offset = int(x) - left_x;
61137
int y_offset = int(y) - top_y;
62-
138+
63139
column_index = int(x_offset / resolution);
64140
row_index = int(y_offset / resolution);
65-
141+
66142
grid_angle = grid[column_index][row_index];
67-
143+
68144
x_step = step_length * cos(grid_angle);
69145
y_step = step_length * sin(grid_angle);
70-
146+
71147
x = x + x_step;
72148
y = y + y_step;
73149
}

0 commit comments

Comments
 (0)