/* The Wave class contains all of the properties and methods necessary to simulate a sine or cosine wave form. The simulated wave is calculated using a defined number of samples across the wave's width */ class Wave { // We define two 'constants' to make it easier to specify the type // of wave we want to use. Outside of the class we can refer to: // Wave.SIN or Wave.COS, while inside the class we can use SIN or COS public static final int SIN = 1; public static final int COS = 2; // keep track of how far apart our samples are, and how wide // the wave is on screen int xspacing; int waveWidth; // which type of wave is this int type; // The wave is calcuated using theta, amplitude and period float theta; float amplitude; float period; // the change in theta from sample to sample float dx; // The height samples across the width of the wave float[] yvalues; // Constructor for a new wave Wave(int type_, float amp_, int xspace, int width_, float period_) { // Store all of our values, and use them to calculate other // values we need type = type_; amplitude = amp_; xspacing = xspace; waveWidth = width_; period = period_; dx = (TWO_PI / period) * xspacing; yvalues = new float[waveWidth / xspacing]; } void calcWave() { // shift our wave a little bit each time theta += 0.01; // Step through the sample points of the wave and recalculate the // height at that sample point float x = theta; for (int i = 0; i < yvalues.length; i++) { if (type == SIN) yvalues[i] = sin(x) * amplitude; if (type == COS) yvalues[i] = cos(x) * amplitude; x += dx; } } // Draw a circle at each sample point across the 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); } } }