import krister.Ess.*; /* Faust 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 sprite; // our pencil network n1; // having a ball (network) int qty = 600; // number of balls in network // -------------------------------------------------------- void setup() { framerate(20); noiseDetail(2, 0.99); img = loadImage("faust.png"); size (768, 576, P3D); n1 = new network(qty, width, height); /* Sprites */ sprite = loadImage("atom.png"); /* Sound */ Ess.start(this); fly = new Channel(); fly.initBuffer(fly.frames(10000)); fly.wave(Ess.TRIANGLE, 75, .1); fly.play(Ess.FOREVER); } public void stop () { Ess.stop(); super.stop(); } void draw() { if(keyPressed && keyCode == SHIFT) image(img, 0, 0); else background(255, 255, 255); n1.run(); } // -------------------------------------------------------- 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); 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; 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] += (.00001 * neighbors[i]); noisey[i] += (.00001 * neighbors[i]); } else { noisex[i] += (.0001 * neighbors[i]); noisey[i] += (.0001 * neighbors[i]); } nodex[i] = noise(noisex[i]) * w; nodey[i] = noise(noisey[i]) * h; } } void show_nodes() // draw all nodes to screen { for (int i = 0; i < nodes; i++) // subtract half a sprites width/heigth image(sprite, nodex[i] - 8, nodey[i] - 8); } }