how can i control and program a wave
in
Programming Questions
•
1 year ago
Hello, i am very new to processing. i have a current sketch that is a basic motion sin wave, set a constant rate. instead of the sin function, i need a custom one so i can control certain lengths of the wave at certain points, i have attached an image to explain what i am trying to do, and here is the sin wave sketch i am currently using.
ultimately i want to able to change x1, x2, x3 using the controlP5 library during the sketch, but i have no idea how to program the wave function.
int xspacing = 8;int w;float theta = 0.0; // Start angle at 0 float amplitude = 75.0; // Height of wave float period = 500.0; // How many pixels before the wave repeats float dx; // Value for incrementing X, a function of period and xspacing float[] yvalues; // Using an array to store height values for the wavevoid setup() { size(200,200); frameRate(30); colorMode(RGB,255,255,255,100); smooth(); w = width+16; dx = (TWO_PI / period) * xspacing; yvalues = new float[w/xspacing]; }void draw() { background(0); calcWave(); renderWave(); }
void calcWave() { // Increment theta theta += 0.02; // For every x value, calculate a y value with sine function float x = theta; for (int i = 0; i < yvalues.length; i++) { yvalues[i] = sin(x)*amplitude; x+=dx; } }void renderWave() { // A simple way to draw the wave with an ellipse at each location for (int x = 0; x < yvalues.length; x++) { noStroke(); fill(255,50); ellipseMode(CENTER); ellipse(x*xspacing,width/2+yvalues[x],16,16); } }
Thank you in advance if you can help me!
1