class WhiteMountain{ int [] peak; Particle [] graph; Particle [] blanket; boolean flipGraph; int columns, columnWidth, screenWidth, speed, threshold; 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 * columnWidth + (columnWidth>>1), height, 0); blanket[i] = physics.makeParticle(1.0, i * columnWidth + (columnWidth>>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(){ noFill(); stroke(0, 255, 0); beginShape(LINE_STRIP); for(int i = 0; i < graph.length; i++){ curveVertex(blanket[i].position().x(), blanket[i].position().y()); } endShape(); } 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); }