|
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; |
2 | 18 |
|
3 | 19 | void setup() {
|
4 | 20 | // width, height in pixels
|
5 | 21 | // slightly smaller than a5
|
6 |
| - size(750, 500, SVG, "flow_field.svg"); |
| 22 | + size(750, 500); |
7 | 23 | smooth();
|
8 | 24 | strokeWeight(2);
|
9 |
| - stroke(0,0,0); |
| 25 | + stroke(0, 0, 0); |
10 | 26 | 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); |
23 | 36 | resolution = int(width * 0.01);
|
24 | 37 | num_columns = (right_x - left_x) / resolution;
|
25 | 38 | num_rows = (bottom_y - top_y) / resolution;
|
26 |
| - |
| 39 | + |
27 | 40 | grid = new float[int(num_columns)][int(num_rows)];
|
28 |
| - |
| 41 | + starting_points = new float[num_lines][2]; |
| 42 | + |
29 | 43 | for (int column = 0; column < num_columns -1; column +=1) {
|
30 | 44 | for (int row = 0; row < num_rows - 1; row +=1) {
|
31 | 45 | float scaled_x = column * 0.005;
|
32 | 46 | float scaled_y = row * 0.005;
|
33 |
| - noiseDetail(8,0.55); |
| 47 | + noiseDetail(_octave, falloff); |
34 | 48 | float noise_value = noise(scaled_x, scaled_y);
|
35 | 49 | angle = map(noise_value, 0.0, 1.0, 0.0, PI * 2.0);
|
36 | 50 | grid[column][row] = angle;
|
37 | 51 | }
|
38 | 52 | }
|
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; |
45 | 61 | }
|
46 |
| - |
47 |
| - //exit() required for SVG generation |
48 |
| - println("Done"); |
49 |
| - exit(); |
50 | 62 | };
|
51 | 63 |
|
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) { |
57 | 133 | beginShape();
|
58 | 134 | for (int i = 0; i < num_steps; i = i+1) {
|
59 | 135 | vertex(x, y);
|
60 | 136 | int x_offset = int(x) - left_x;
|
61 | 137 | int y_offset = int(y) - top_y;
|
62 |
| - |
| 138 | + |
63 | 139 | column_index = int(x_offset / resolution);
|
64 | 140 | row_index = int(y_offset / resolution);
|
65 |
| - |
| 141 | + |
66 | 142 | grid_angle = grid[column_index][row_index];
|
67 |
| - |
| 143 | + |
68 | 144 | x_step = step_length * cos(grid_angle);
|
69 | 145 | y_step = step_length * sin(grid_angle);
|
70 |
| - |
| 146 | + |
71 | 147 | x = x + x_step;
|
72 | 148 | y = y + y_step;
|
73 | 149 | }
|
|
0 commit comments