//Kohonen Self-Organising-Map //adapted from // AIExport ai; Tracer make; SOM net; int state = 0; int netSize = 10; Buttons [] button; Draggable corn; PFont font; boolean rand; int exportState = 0; PImage buffer; float screenRatio = 0.0; void setup(){ size(800,800);//,P3D); ai = new AIExport( this, 1 ); font = loadFont("Kartika-48.vlw"); buffer = loadImage("dots.gif"); //change for desired image textFont(font,26); button = new Buttons[4]; button[0] = new Buttons(width-100, 100, 100, 20, color(240,240,240), "train", null); button[1] = new Buttons(width-100, 120, 100, 20, color(240,240,240), "clear image", null); button[2] = new Buttons(width-100, 140, 100, 20, color(240,240,240), "export data", null); button[3] = new Buttons(width-100, 160, 100, 20, color(240,240,240), "cyclic vectors", null); corn = new Draggable (width-100, height-100, 20, color(255,20,20), color(200,10,10)); initRatio(corn.x,corn.y); make = new Tracer(); ellipseMode(CENTER); rand = false; } void draw(){ ai.run(); background(240,240,240); image(buffer,0,0,buffer.width*screenRatio,buffer.height*screenRatio); stroke(0); line(0,corn.y,width,corn.y); line(corn.x,0,corn.x,height); corn.draw(); corn.update(); make.draw(); switch(state){ case 0: break; case 1: net = new SOM(netSize,make.xVectors(),make.yVectors(),rand); state++; break; case 2: net.run(); net.draw(); if(net.stable){ state++; } break; case 3: net.draw(); break; } for(int i = 0; i < button.length; i++) button[i].draw(); switch(exportState){ case 1: ai.toggleContinuousRecording(); exportState++; break; case 2: aiExport(); exportState++; break; case 3: ai.toggleContinuousRecording(); exportState = 0; break; } } void mousePressed(){ int choice = -1; for(int i = 0; i < button.length; i++) if(button[i].over()) choice = i; switch(choice){ case 0: state = 1; break; case 1: make = new Tracer(); state = 0; break; case 2: exportState = 1; break; case 3: rand = !rand; if(rand) button[choice].label = "random vectors"; else button[choice].label = "cyclic vectors"; break; default: if(!corn.over()){ Vec2 temp = new Vec2((1.0/width)*mouseX,(1.0/height)*mouseY); make.trace(temp); } } } void mouseDragged(){ initRatio(corn.x,corn.y); } void initRatio(float w, float h){ float ratioX = (1.0 / buffer.width) * w; float ratioY = (1.0 / buffer.height) * h; screenRatio = ratioX; if(ratioY < ratioX) screenRatio = ratioY; } boolean overVector(float x, float y){ for(int i = 0; i < make.vector.length; i++) if (make.vector[i].equals(x,y)) return true; return false; } Vec2 [] concatVector(Vec2 [] vectors, Vec2 [] addVectors) { Vec2 [] temp = new Vec2[vectors.length + addVectors.length]; System.arraycopy(vectors, 0, temp, 0, vectors.length); System.arraycopy(addVectors, 0, temp, vectors.length, addVectors.length); return temp; } Vec2 [] addVector(Vec2 [] vectors, Vec2 newVector) { Vec2 [] temp = new Vec2[vectors.length + 1]; System.arraycopy(vectors, 0, temp, 0, vectors.length); temp[vectors.length] = newVector; return temp; }