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 › interpolating arrays
Page Index Toggle Pages: 1
interpolating arrays (Read 617 times)
interpolating arrays
Aug 6th, 2009, 11:11am
 
hello i have 2 array with values, for example a = [2 , 5 , 7 , 4]   and

b = [ 4 , 6 , 7 , 2]

i need to change the values of my array a to b, but i need make with  interpolation, so the values of my array a converts to the values of my array b not suddenly but in a smoothly way.

is there an easy way of doing this?

thanksss
Re: interpolating arrays
Reply #1 - Aug 6th, 2009, 1:53pm
 
you need to know how many steps between the two positions.

then you'll need an array of increments where i[n] = (b[n] - a[n]) / num_steps. (actually, you can re-use b[] for the increments). (be careful when dividing integers, the answers need to be floats or doubles.)

then, each time through the loop, add i[n] to a[n].

there's also lerp() but i don't see any advantage over the above in this case.

http://processing.org/reference/lerp_.html
Re: interpolating arrays
Reply #2 - Aug 8th, 2009, 1:54pm
 
hi i made this code , but its not working, any idea of what am i doing wrong?

thanks
Code:



float[] a;
float[] b;
float[] incremento;

float num_steps;


void setup(){
size(500, 500 );
smooth();

a = new float[20];
b = new float[20];
incremento = new float[20];


num_steps = 5;
for (int i = 0 ; i < 20 ; i ++) {
a[i] = random(200);
incremento[i] = a[i];
b[i] = random(200);

}
}


void draw(){
background(250, 0, 0);
if (keyPressed == true) {
interpolate_array();
}
for (int i = 0 ; i < 20 ; i ++) {
ellipse( incremento[i] , 20 , 30, 30);
//print(" " + i + " " + incremento[i]);
}
}


void interpolate_array(){
for (int i = 0 ; i < 20 ; i ++) {
incremento[i] = (b[i] - a[i] ) / num_steps;
}

}







Re: interpolating arrays
Reply #3 - Aug 8th, 2009, 2:39pm
 
incremento, as the name implies, is supposed to hold the increment value, not the drawing values.
Possible change:
Code:
// [...]
 num_steps = 5;
 for (int i = 0 ; i < a.length ; i ++) {
   a[i] = random(200);
   b[i] = random(200);
   incremento[i] = (b[i] - a[i]) / num_steps;
 }
}


void draw(){
 background(250, 0, 0);
 if (keyPressed == true) {
   for (int i = 0 ; i < a.length ; i ++) {
a[i] += incremento[i];
   }
 }
 for (int i = 0 ; i < a.length ; i ++) {
   ellipse(a[i] , 20 , 30, 30);
 }
}
(untested)
Page Index Toggle Pages: 1