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 › Beginner! SINE WAVE
Page Index Toggle Pages: 1
Beginner! SINE WAVE (Read 221 times)
Beginner! SINE WAVE
Mar 10th, 2009, 1:53pm
 
I really need some help over here!

I am trying to understand what makes the sine wave move, and how to change direction and shape of it. This is the code that I found on here and had played around with it. I would like the wave to move in one direction only and be in the shape of a loop/ribbon (best way to describe it would be the shape of the breast cancer ribbon). Can someone please show me how to alter the existing code and maybe explain the calculations to me in layman's terms??

Any help is much appreciated. THANKS Smiley


int yspacing = 10;  
int w;            
float theta = 0.0;  
float amplitude = 75.0;  
float period = 500.0;  
float dy;  
float[] xvalues;  

void setup() {
 size(300,300);
 frameRate(35);
 smooth();
 w = width+15;
 dy = (TWO_PI / period) * yspacing;
 xvalues = new float[w/yspacing];
}
void draw() {
 background(125);
 calcWave();
 renderWave();
}
void calcWave() {
 theta += 0.02;
 float y = theta;
 for (int i = 0; i < xvalues.length; i++) {
   xvalues[i] = sin(y)*amplitude;
   y+=dy;
 }
}

void renderWave() {
 for (int y = 0; y < xvalues.length; y++) {
   stroke(214,7,142);
   noFill();
   ellipseMode(CENTER);
   ellipse(width/2+xvalues[y],y*yspacing,21,21);
 }
}

Re: Beginner! SINE WAVE
Reply #1 - Mar 10th, 2009, 3:14pm
 
Changing direction is simple: invert x and y (and related stuff):
Code:
int xspacing = 10;    
int h;
float theta = 0.0;
float amplitude = 75.0;
float period = 500.0;
float dx;
float[] yvalues;

void setup() {
size(300,300);
frameRate(35);
smooth();
h = height+15;
dx = (TWO_PI / period) * xspacing;
yvalues = new float[h/xspacing];
}
void draw() {
background(125);
calcWave();
renderWave();
}
void calcWave() {
theta += 0.02;
float x = theta;
for (int i = 0; i < yvalues.length; i++) {
yvalues[i] = sin(x)*amplitude;
x+=dx;
}
}

void renderWave() {
for (int x = 0; x < yvalues.length; x++) {
stroke(214,7,142);
noFill();
ellipseMode(CENTER);
ellipse(x*xspacing,height/2+yvalues[x],21,21);
}
}

"Move in one direction only": I don't understand.
Making the wave looping on itself is a quite different problem. That would be no longer an y = f(x) curve.
Re: Beginner! SINE WAVE
Reply #2 - Mar 10th, 2009, 3:24pm
 
So if I want to loop the wave onto itself, how do I start something like that?
Any links or suggestions you can provide?

Thanks
Re: Beginner! SINE WAVE
Reply #3 - Mar 10th, 2009, 4:30pm
 
A possible way, re-using a previous sketch I made:
Code:
int queueSize = 50;

ArrayDeque lines;

void setup()
{
 size(700, 700);
 frameRate(40);
 lines = new ArrayDeque();
 noFill();
 ellipseMode(CENTER);
}

void draw()
{
 background(255);

 if (mouseX != pmouseX || mouseY != pmouseY)
 {
   PVector p = new PVector(mouseX, mouseY);
   lines.addFirst(p);
 }
 if (lines.size() > queueSize)
 {
   lines.removeLast();
 }

 Iterator it = lines.iterator();
 PVector pp = null;
 while (it.hasNext())
 {
   PVector cp = (PVector) it.next();
   if (pp != null)
   {
     stroke(14, 7, 42);
     strokeWeight(1);
     line(pp.x, pp.y, cp.x, cp.y);
     stroke(214, 7, 142);
     strokeWeight(5);
     ellipse(cp.x, cp.y, 21, 21);
   }
   pp = cp;
 }
}

The ArrayDeque is just a list with good handling of adding and removing at ends.
Page Index Toggle Pages: 1