I am working on a simple linear interpolation program. And I am having some troubles when implementing the algorithm. Assuming there are 12 numbers as a whole, we'll let user input 3 of them(position 0, position 6 and position 12). Then the program will calculate other numbers. Here is a piece of my code to achieve this:
static double[] interpolate(double a, double b){
double[] array = new double[6];
for(int i=0;i<6;i++){
array[i] = a + (i-0) * (b-a)/6;
}
return array;
}
static double[] interpolate2(double a, double b){
double[] array = new double[13];
for(int i=6;i<=12;i++){
array[i] = a + (i-6) * (b-a)/6;
}
return array;
}
As you can see, I used two functions. But I want to find a universal function to do this job. However, I don't know how to find a common way to represent i-0
and i-6
. How to fix it? According to Floating point linear interpolation, I know maybe I should add a formal parameter float f
. But I am not quite understand what does float f
mean and how to modify my code based on it. Could anyone help me? Thank you.