//Scans image downwards for signs of whiteness and generates a graph class WhiteMountain{ int [] peak; //height of white detected on capture Particle [] graph; //fixed particles that move relative to peak values Particle [] blanket; //drawing of graph attached to graph particles by springs boolean flipGraph; //mirror capture results? int columns, columnWidth, screenWidth, speed, threshold; //tracking variables WhiteMountain(int columns, int columnWidth, int speed, int threshold, boolean flipGraph){ peak = new int[columns]; graph = new Particle[columns]; blanket = new Particle[columns]; for(int i = 0; i < blanket.length; i++){ graph[i] = physics.makeParticle(1.0, i * screenColumnWidth + (screenColumnWidth>>1), height, 0); blanket[i] = physics.makeParticle(1.0, i * screenColumnWidth + (screenColumnWidth>>1), height, 0); graph[i].makeFixed(); physics.makeSpring(graph[i], blanket[i], 0.5, 0.8, 0.01 ); } this.columns = columns; this.columnWidth = columnWidth; this.speed = speed; this.threshold = threshold; this.flipGraph = flipGraph; } void draw(){ stroke(200); strokeWeight(3); beginShape(LINE_STRIP); vertex(0, blanket[0].position().y()+2); for(int i = 0; i < graph.length; i++){ curveVertex(blanket[i].position().x(), blanket[i].position().y()+2); } vertex(width, blanket[graph.length-1].position().y()+2); endShape(); stroke(255); strokeWeight(3); beginShape(LINE_STRIP); vertex(0, blanket[0].position().y()+2); for(int i = 0; i < graph.length; i++){ curveVertex(blanket[i].position().x(), blanket[i].position().y()); } vertex(width, blanket[graph.length-1].position().y()+2); endShape(); } //Update the graph based on source image given void update(PImage source){ for(int i = 0; i < columns; i++){ peak[i] = source.height; if(!flipGraph){ graph[i].moveTo(graph[i].position().x(), source.height, 0); } else{ graph[graph.length - (i + 1)].moveTo(graph[graph.length - (i + 1)].position().x(), source.height, 0); } for(int j = 0; j < source.height; j++){ int brightnessTotal = -1; for(int k = 0; k < columnWidth; k += speed){ int pixel = (i * columnWidth) + k + j * source.width; brightnessTotal += grey(source.pixels[pixel]); } if(brightnessTotal > threshold){ peak[i] = j; if(!flipGraph){ graph[i].moveTo(graph[i].position().x(), j, 0); } else{ graph[graph.length - (i + 1)].moveTo(graph[graph.length - (i + 1)].position().x(), j, 0); } break; } } } } } //integer brightness function for extra speed static int grey(color p){ return max((p >> 16) & 0xFF, (p >> 8) & 0xFF, p & 0xFF); }