wiring [] p = new wiring [20]; void setup(){ for (int i = 0; i < p.length; i++){ p[i] = new wiring(50); } background(255); size(400,400); } void loop(){ for (int i = 0; i < p.length; i++){ p[i].load(); p[i].fire(); p[i].learn(); p[i].update(); } } void mousePressed(){ background(255); } class wiring{ perceptron [] p; wiring(int n){ p = new perceptron[n]; for (int i = 0; i < p.length; i++){ p[i] = new perceptron(0.01); } } void load(){ for (int i1 = 0; i1 < p.length; i1++){ for (int i2 = 0; i2 < p[i1].ins.length; i2++){ p[i1].ins[i2] = p[(i1+(i2+1))%p.length].ot; } } } void fire(){ for (int i = 0; i < p.length; i++){ p[i].fire(); } } void learn(){ for (int i = 0; i < p.length; i++){ float theta = atan2(mouseY-p[i].y,mouseX-p[i].x); //float theta = atan2(y-p[i].y,x-p[i].x) / TWO_PI; //float theta = atan2(200-p[i].y,200-p[i].x) / TWO_PI; float d = (theta + PI)/TWO_PI; for (int i2 = 0; i2 < p[i].w.length; i2++){ p[i].learn(i2,d); } } } void update(){ for (int i = 0; i < p.length; i++){ p[i].x = fwrap(p[i].x + (cos(p[i].ot*TWO_PI) * 1),width-1); p[i].y = fwrap(p[i].y + (sin(p[i].ot*TWO_PI) * 1),height-1); color c = pixels[int(p[i].x)+int(p[i].y)*width]; float r = red(c); float g = green(c); float b = blue(c); if (r > 0 && i > p.length/2){r -= 10;}else if (r < 255){r += 10;} if (g > 0 && i > p.length/2){g -= 10;}else if (g < 255){g += 10;} if (b > 0 && i > p.length/2){b -= 10;}else if (b < 255){b += 10;} stroke(r,g,b); point(p[i].x,p[i].y); } } } class perceptron{ float x,y,ot,ln; float [] ins; float [] w; perceptron(float ln){ this.x = random(400); //this.x = 0; this.y = random(400); //this.y = y; this.ot = 0.75; this.ln = ln; this.ins = new float[10]; this.w = new float[10]; for(int i = 0; i < w.length; i++){ w[i] = 1.0; } } void fire(){ ot = meanOut(ins,w); } void learn(int i, float d){ w[i] += ln * (d - ot); //w[i] += ln * (d - (w[i]*ins[i])); } } float meanOut(float [] inputs, float [] weights){ float sum = 0.0; for (int i = 0; i < inputs.length; i++){ sum += inputs[i] * weights[i]; } return sum / inputs.length; } float fwrap (float fvalu, float flimi){ float fcar=fvalu; if (fcar>flimi){ fcar=(fcar)%flimi; } if (fcar<0){ fcar=flimi-fcar; fcar=(fcar)%flimi; fcar=flimi-fcar; } return fcar; }