/* A wave collection keeps track of a set of waves. Because the waves must match in spacing and wave size, the WaveCollection sets these variables for each incoming wave. */ class WaveCollection { // We'll keep track of our different waves in an ArrayList ArrayList waves; // When we're ready to draw our addative wave to the screen // we'll use this array to store values from all of the waves float[] yvalues; // The WaveCollection controls the spacing and width of the wave int waveWidth; int xspacing; // Construct the collection, and calculate the height array WaveCollection(int xspace, int width_) { waveWidth = width_; xspacing = xspace; yvalues = new float[waveWidth / xspacing]; waves = new ArrayList(); } // Add a wave to our collection void add(Wave w) { // set the proper sizing for our wave w.setSizing(xspacing, waveWidth); waves.add(w); } // Walk through the array of waves and calcuate the wave samples for each one void calcWaves() { for (int i = 0; i < waves.size(); i++) { Wave w = (Wave)waves.get(i); w.calcWave(); waves.set(i, w); } } void sumWaves() { // initialize our heights to 0 for (int i = 0; i < yvalues.length; i++) { yvalues[i] = 0; } // for each wave in our collection we // need to add up our values for (int i = 0; i < waves.size(); i++) { // get our wave Wave w = (Wave)waves.get(i); // get the height samples float[] y = w.getValues(); // add the samples to our collective height for (int j = 0; j < yvalues.length; j++) { yvalues[j] += y[j]; } } } // draw the wave to screen void render() { smooth(); for (int x = 0; x < yvalues.length; x++) { noStroke(); fill(32, 64, 128, 50); ellipseMode(CENTER); ellipse(x * xspacing, yvalues[x], 16, 16); } } }