import krister.Ess.*; import processing.opengl.*; /* Drosophila Created by: Peter Chiocchetti (http://arton.cunst.net/) Thomas Feuerstein (http://feuerstein.myzel.net/) February-April 2006 based on a sketch by: Charles Hinshaw (http://www.everydayrevolution.com) and John Beech (http://www.mkv25.net/?item_id=74) April 2004 ----------------------------------------------------------- */ Channel fly; // very annoying PImage img; // image we want to track PImage sprites[]; // our pencils network n1; // having a ball (network) int qty = 600; // number of balls in network int cf = 0; // count saved frames // -------------------------------------------------------- void setup() { frameRate(25); noiseDetail(2, 0.99); img = loadImage("galton.png"); size (768, 576, OPENGL); n1 = new network(qty, width, height); /* Sprites + directions: 7 0 1 - 7 0 6 6 2 - 2 1 5 4 3 - 5 3 4 */ sprites = new PImage[8]; sprites[0] = loadImage("fly_000.png"); sprites[6] = loadImage("fly_045.png"); sprites[1] = loadImage("fly_090.png"); sprites[4] = loadImage("fly_135.png"); sprites[3] = loadImage("fly_180.png"); sprites[5] = loadImage("fly_225.png"); sprites[2] = loadImage("fly_270.png"); sprites[7] = loadImage("fly_315.png"); sprites[0].format = ARGB; sprites[1].format = ARGB; sprites[2].format = ARGB; sprites[3].format = ARGB; sprites[4].format = ARGB; sprites[5].format = ARGB; sprites[6].format = ARGB; sprites[7].format = ARGB; /* Sound */ Ess.start(this); fly = new Channel(); fly.initBuffer(fly.frames(1000)); fly.noise(Ess.PINK, .1); fly.play(Ess.FOREVER); //println(javax.imageio.ImageIO.getReaderFormatNames()); } public void stop () { Ess.stop(); super.stop(); } void draw() { if(keyPressed && keyCode == SHIFT) image(img, 0, 0); else background(255, 255, 255); n1.run(); /* saveFrame("galton-####.jpeg"); cf ++; if (cf >15250) stop(); println(cf); */ } // -------------------------------------------------------- class network { int nodes; int[] noded; float[] nodex; float[] nodey; float[] noisex; float[] noisey; int[] neighbors; int x, y, w, h; int maxlength = 40; network(int nodes, int w, int h) { this.nodes = max(1, nodes); noded = new int[nodes]; // direction nodex = new float[nodes]; nodey = new float[nodes]; noisex = new float[nodes]; noisey = new float[nodes]; neighbors = new int[nodes]; this.w = w; this.h = h; for (int i = 0; i < nodes; i++) { noisex[i] = random(nodes); noisey[i] = random(nodes); nodex[i] = noise(noisex[i]) * w; nodey[i] = noise(noisey[i]) * h; } } void run() { network_noise(); create_connections(); show_nodes(); } void create_connections() { // find neighbors int[] newneighbors = new int[nodes]; float dist; for (int i = 0; i < nodes; i++) newneighbors[i] = 0; for (int i = 0; i < nodes; i++) { for(int j = 0; j < nodes; j++) { dist = dist(nodex[i], nodey[i], nodex[j], nodey[j]); if(dist < maxlength) { newneighbors[i]++; newneighbors[j]++; } } } neighbors = newneighbors; } void network_noise() // perlin noise for all nodes { color b, c; float nx, ny; float dx, dy; b = color(0, 0, 0); for (int i = 0; i < nodes; i++) { // different speed depending on bg color & no. of neighbors c = img.pixels[int(nodey[i])*img.width+int(nodex[i])]; if (c == b) { noisex[i] += (.000015 * neighbors[i]); noisey[i] += (.000015 * neighbors[i]); } else { noisex[i] += (.0001 * neighbors[i]); noisey[i] += (.0001 * neighbors[i]); } // different sprite depending on direction nx = noise(noisex[i]) * w; ny = noise(noisey[i]) * h; dx = nx - nodex[i]; dy = ny - nodey[i]; nodex[i] = nx; nodey[i] = ny; // no move if (abs(dx) + abs(dy) < 1) continue; // horizontal if (dx > 0.75) noded[i] = 1; // west else if (dx < -0.75) noded[i] = 2; // east else noded[i] = 0; // same // vertical if (dy > 0.75) noded[i] += 3; // north else if (dy < -0.75 && noded[i] != 0) noded[i] += 5; // south } } void show_nodes() // draw all nodes to screen { for (int i = 0; i < nodes; i++) // subtract half a sprites width/heigth image(sprites[noded[i]], nodex[i] - 8, nodey[i] - 8); } }