We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › How to draw a moving sine wave.
Page Index Toggle Pages: 1
How to draw a moving sine wave. (Read 565 times)
How to draw a moving sine wave.
Dec 19th, 2007, 3:36pm
 
I have tried to draw it but the wave moves in a very unstable manner.  Are there any god ways to generate a slow stable moving sine wave?

float angle = 0.0;
float offset = 100.0;
float scaleValue = 50.0;
float angleIncre = PI/360;

void setup() {
 size(700, 200);
 frameRate(60);
}

void draw() {
 background(200);
 for (float x = 0; x <=width; x+=1) {
   float y = offset + (sin(angle) * scaleValue);
   rect(x, y, 1, 1);
   angle += angleIncre;
 }
}



Thanks in in advance.
Re: How to draw a moving sine wave.
Reply #1 - Dec 20th, 2007, 7:19pm
 
here's a simple little code my teacher gave us

Code:

//Code by JFR

float posX, posY, amplitude = 40, variationY, variationY2, temps = 0, WaveSpeed, completeCycle, subCycle;
int startX = 100, endX = 500, step=10, distanceX;
color color_1, color_2;

void setup() {
size(600,500);
background(0);
color_1 = color(200,150,0);
color_2 = color(150,240,50);

noStroke();
smooth();
rectMode(CENTER);
distanceX = endX-startX;
completeCycle = TWO_PI/float(distanceX);
subCycle = (TWO_PI*5)/float(distanceX);
}


void draw() {
background(0);

fill(color_1);
posY = 75;
WaveSpeed = 0.0005;

for (int i = startX; i<endX; i+=step) {
temps += (millis()%i)*WaveSpeed;
variationY = sin(((i-startX)+temps)*completeCycle);
variationY*=amplitude;
ellipse(i, (posY+variationY), 5, 5);
}

fill(color_2);
posY = 165;
WaveSpeed = 0.0005;
for (int i = startX; i<endX; i+=step) {
temps += (millis()%i)*WaveSpeed;
variationY = sin(((i-startX)+temps)*completeCycle);
variationY*=(amplitude*0.2)+((i*i)/(4000));
rect(i, (posY+variationY), 5, 5);
}

fill(color_1);
posY = 275;
WaveSpeed = 0.0001;
for (int i = startX; i<endX; i+=step) {
temps += (millis()%i)*WaveSpeed;
variationY = sin(((i-startX))*completeCycle);
variationY*=amplitude;
variationY2 = sin((i-startX+temps)*subCycle);
variationY2 *= amplitude*0.3;
ellipse(i, (posY+variationY+variationY2), 5, 5);
}

fill(color_2);
posY = 395;
WaveSpeed = 0.0001;
for (int i = startX; i<endX; i+=step) {
temps += (millis()%i)*WaveSpeed;
variationY = sin(((i-startX+temps))*completeCycle);
variationY*=(amplitude*0.2)+((i*i)/(4000));
variationY2 = sin((i-startX+temps)*subCycle);
variationY2 *= (amplitude*0.005)+((i*i)/(16000));
rect(i, (posY+variationY+variationY2), 5, 5);
}
}

Re: How to draw a moving sine wave.
Reply #2 - Dec 22nd, 2007, 7:43pm
 
This is perfect, cheers steve
Page Index Toggle Pages: 1