




Processing, 2017
A program written with Processing which draws an interactive sine wave.
Free to use.
Code:
sinewave wave;
int t, cycle;
float A, f;
boolean save;
void setup() {
size(900, 600);
background(0);
fill(255);
stroke(255);
t = 1;
A = height/2;
f = 0.1;
cycle = 1;
save = false;
}
void draw() {
background(0);
wave = new sinewave(A, f, t, cycle);
wave.setup();
wave.draw();
println("Save: "+save+", Amplitude: "+A+", Frequency: "+f+", Time: "+t+", Cycle: "+cycle);
}
void mousePressed() {
if(mouseButton == LEFT){
cycle++; //if left mouse button is pressed add wave
}
if(mouseButton == RIGHT && cycle > 1){
cycle--; //if right mouse button is pressed remove wave
}
}
void keyPressed() {
if(key == 'a'){
A++; //press 'a' key to increase wave height
if(save == true){
saveFrame("sine-###.png");
}
}
if(key == 's' && A > 1){
if(save == true){
saveFrame("sine-###.png");
}
A--; //press 's' key to decrease wave height
}
if(key == 'f'){
f+=0.1; //press 'f' key to increase number of dots
if(save == true){
saveFrame("sine-###.png");
}
}
if(key == 'd' && f > 0.1){
if(save == true){
saveFrame("sine-###.png");
}
f-=0.1;//press 'd' key to increase number of dots
}
if(key == 't'){
if(save == true){
saveFrame("sine-###.png");
}
t++; //press 't' key to increase wave width
}
if(key == 'r' && t > 1){
if(save == true){
saveFrame("sine-###.png");
}
t--; //press 'r' key to decrease wave width
}
if(key == ' '){
if(save == false){
save = true; //if space bar is pressed save image
}
else{
save = false;
}
}
}
class sinewave {
float x[];
float y[];
float A, f, t;
int cycle, c;
int max;
sinewave(float _A, float _f, float _t, int _cycle){
A = _A;
f = _f;
t = _t;
cycle = _cycle;
c = 1;
}
void setup() {
max = (width/int(t))*cycle;
x = new float[max];
y = new float[max];
for(int i = 0; i < max; i++){
x[i] = i * t;
if(x[i] > width){
if(x[i] % width == 0){
c++;
}
x[i] -= width*c;
}
}
for(int j = 0; j < max; j++){
float ytemp = A * sin(j * f);
y[j] = map(ytemp, -height, height, 0, height);
}
}
void draw() {
for(int i = 0; i < x.length; i++){
fill(255);
stroke(255);
ellipse(x[i], y[i], 1, 1);
}
}
}