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
|
int PARTICLE_NUM = 60; int MIN_DIST = 70; float MIN_SPEED = 0.25; float MAX_SPEED = 0.75; float RING_SIZE = 150; boolean line_connect = true;
boolean use_color_map = false; boolean use_color_gradient = false; float clr; float NOISE_SCALE = 0.02; float frame_cnt = 0; PImage clrmap;
Particle [] ps;
class Particle { public float x, y, rot_angle; private float vx, vy; private int step;
Particle(float x, float y, float v, float v_angle) { this.x = x; this.y = y; this.vx = v*cos(v_angle); this.vy = v*sin(v_angle); generate_new_curve(); }
public void next() { turn(rot_angle); x += vx; y += vy; step--; if (step <= 0) { generate_new_curve(); } }
public void turn(float rot) { float pvx = vx; float pvy = vy; vx = pvx * cos(rot) - pvy * sin(rot); vy = pvx * sin(rot) + pvy * cos(rot); }
private void generate_new_curve() { step = (int)random(80, 300); rot_angle = random(-0.005, 0.005); } }
void setup() { reset(); } void reset() { size( 600, 600); background(0); smooth();
ps = new Particle[PARTICLE_NUM];
for (int i = 0; i < PARTICLE_NUM; i++) { float v = random(MIN_SPEED, MAX_SPEED); float angle = random(0, 2*PI); float v_angle = random(0, 2*PI);
ps[i] = new Particle(RING_SIZE * cos(angle), RING_SIZE * sin(angle), v, v_angle); } clr = random(1.0);
if (use_color_map) load_color_map(); }
void draw() { if (use_color_map) { colorMode(RGB, 255); generateGraph( "colormap"); } else { filter(INVERT); colorMode(HSB, 1); if (use_color_gradient) { clr += random(0.001); clr = clr % 1; } generateGraph("rand"); filter(INVERT); } }
void changeColorGradient() { use_color_gradient = !use_color_gradient; }
void generateGraph( String coloring_type) {
translate(width/2, height/2); for (int i = 0; i < PARTICLE_NUM; i++) { for (int j = i+1; j < PARTICLE_NUM; j++) { float dis = dist(ps[i].x, ps[i].y, ps[j].x, ps[j].y); if ( dis < MIN_DIST ) { if (coloring_type == "rand") { float k = (0.5 + float(i)/2.0) / PARTICLE_NUM; fill(clr, pow(k, 0.1), 0.9 * sqrt(1-k), 0.1 * (1 - dis / MIN_DIST)); stroke(clr, pow(k, 0.1), 0.9 * sqrt(1-k), 0.1 * (1 - dis / MIN_DIST)); } else if (coloring_type == "colormap") { color c = set_color_by_map(ps[i]); fill((c >> 16) & 0xFF, (c >> 8) & 0xFF, c & 0xFF, 32 * (1 - dis / MIN_DIST)); stroke((c >> 16) & 0xFF, (c >> 8) & 0xFF, c & 0xFF, 32 * (1 - dis / MIN_DIST)); } buildConnect(ps[i], ps[j], dis); } } ps[i].next(); } }
void keyPressed() { switch(key) { case 'g': changeColorGradient(); break; case 't': changeConnectionBuild(); break; case 'r': reset(); break; case 's': saveFrame(); break; case 'c': changeColor(); break; } }
void buildConnect(Particle p1, Particle p2, float dis) {
if (line_connect) { line(p1.x, p1.y, p2.x, p2.y); } else { float cx = (p1.x + p2.x)/2; float cy = (p1.y + p2.y)/2; noFill(); ellipse(cx, cy, dis/2, dis/2); } }
void changeConnectionBuild() { line_connect = ! line_connect; }
void changeColor() { use_color_map = ! use_color_map; }
void load_color_map() { clrmap = loadImage("chocolate.png"); blurImage(clrmap); }
color set_color_by_map(Particle p) { if (clrmap.width > 0) { if (abs(p.x) >= width/2 - 1 || abs(p.y) >= height/2 - 1) return 0; float x = map(p.x, -width/2, width/2, 0, clrmap.width); float y = map(p.y, -height/2, height/2, 0, clrmap.height); if ((int)((y) * clrmap.width) + (int) (x-1) >= clrmap.width * clrmap.height) return 0; color c = clrmap.pixels[(int)((y) * clrmap.width) + (int) (x-1)]; return c; } return 0; }
void blurImage(PImage img) { float v = 1.0 / 25.0; float[][] kernel = { { v, v, v, v, v } , { v, v, v, v, v } , { v, v, v, v, v } , { v, v, v, v, v } , { v, v, v, v, v } };
for (int y = 2; y < img.height-2; y++) { for (int x = 2; x < img.width-2; x++) { float sum_r = 0, sum_g = 0, sum_b = 0; for (int ky = -2; ky <= 2; ky++) { for (int kx = -2; kx <= 2; kx++) { int pos = (y + ky)*img.width + (x + kx); float val_r = red(img.pixels[pos]); float val_g = green(img.pixels[pos]); float val_b = blue(img.pixels[pos]);
sum_r += kernel[ky+2][kx+2] * val_r; sum_g += kernel[ky+2][kx+2] * val_g; sum_b += kernel[ky+2][kx+2] * val_b; } } img.pixels[y*img.width + x] = color(sum_r, sum_g, sum_b); } } }
|