xxxxxxxxxx
124
//SST Lecture 1 Sine Wave
let xspacing;// = 50; // Distance between each horizontal location
let xspacing2;
let w; // Width of entire wave
let theta = 0.0; // Start angle at 0
let period;// = 500.0; // How many pixels before the wave repeats
let period2;
let dx; // Value for incrementing x
let dx2;
let yvalues; // Using an array to store height values for the wave
let yvalues2;
let amplitude;// = 75.0; // Height of wave
let amplitude2;
let circleSize;
let r,g,b;
function setup() {
createCanvas(600, 400);
w = width + 16;
amplitude=createSlider(0,100,75);
amplitude.position(10,10);
amplitude.style('width', '80px');
// amplitude.style('opacity', '1');
amplitude2=createSlider(0,100,75);
amplitude2.position(10,30);
amplitude2.style('width', '80px');
xspacing=15;
xspacing2=15;
// xspacing=createSlider(0,400,15);
// xspacing.position(180,10);
// xspacing.style('width', '80px');
// xspacing2=createSlider(0,400,15);
// xspacing2.position(180,30);
// xspacing2.style('width', '80px');
// circleSize=createSlider(1,20,5);
// circleSize.position(200,10);
// circleSize.style('width', '80px');
period=createSlider(0,1000,500);
period.position(350,10);
period.style('width', '160px');
period2=createSlider(0,1000,500);
period2.position(350,30);
period2.style('width', '160px');
r=createSlider(0,255,100);
r.position(10,370);
r.style('width', '160px');
g=createSlider(0,255,100);
g.position(180,370);
g.style('width', '160px');
b=createSlider(0,255,100);
b.position(360,370);
b.style('width', '160px');
// dx = (TWO_PI / period.value()) * xspacing.value();
yvalues = new Array(floor(w / xspacing));
yvalues2 = new Array(floor(w / xspacing2));
}
function draw() {
// dx = (TWO_PI / period.value()) * xspacing.value();
dx = (TWO_PI/ period.value()) * xspacing;
dx2 = (TWO_PI/ period2.value()) * xspacing2;
background(0);
text('amplitude', 100, 22);
// text('wavelength', 270, 22);
// text('particle size', 380, 22);
text('wavelength', 520, 22);//replaced the perido
calcWave();
renderWave();
}
function calcWave() {
// Increment theta (try different values for
// 'angular velocity' here)
theta += 0.02;
// For every x value, calculate a y value with sine function
let x = theta;
for (let i = 0; i < yvalues.length; i++) {
yvalues[i] = sin(x) * amplitude.value();
x += dx;
}
for (let i = 0; i < yvalues2.length; i++) {
yvalues2[i] = cos(x) * amplitude2.value();
x += dx2;
}
}
function renderWave() {
noStroke();
fill(r.value(),g.value(),b.value());
// A simple way to draw the wave with an ellipse at each location
for (let x = 0; x < yvalues.length; x++) {
ellipse(x * xspacing, height / 2 + yvalues[x], 10,10);
}
for (let x = 0; x < yvalues2.length; x++) {
ellipse(x * xspacing2, height / 2 + yvalues2[x], 10,10);
}
}