Elan Hickler
Elan Hickler

Reputation: 1137

C++ interpolate array with cosine

I got this function from [this website]http://paulbourke.net/miscellaneous/interpolation/):

double CosineInterpolate(
   double y1,double y2,
   double mu)
{
   double mu2;

   mu2 = (1-cos(mu*PI))/2;
   return(y1*(1-mu2)+y2*mu2);
}

How do I use this to interpolate an array? Here's how I'd be calling the function.

Interpolate(point_a, point_b, number_of_positions_between_the_points, position)

e.g.

for (int i = 0; i < ArrayOfPoints.size()-1; ++i) {
    double point_a = ArrayOfPoints[i];
    double point_b = ArrayOfPoints[i+1];
    for (int j = 0; j < 2048; ++j){
        array[j] = Interpolate(point_a, point_b, 2048, j)
    }
}

Upvotes: 0

Views: 2194

Answers (1)

Mike Dinescu
Mike Dinescu

Reputation: 55750

You have the number of positions between the points, and then you have the current position. Think of mu as a percentage of the linear distance between the first point and the second that is determined by the current position, and the total number of positions. That is:

mu = (double)current_position / number_of_positions_between_the_points;

That will give you values between 0 and 1, in fixed increments, determined by how many positions you want to have between the points.

Hint: In your loop, j is the current position.

The other thing that you have to deal with is that you are calling a function named Interpolate(point_a, point_b, 2048, j) but you haven't shown the implementation for that function. Instead, you have the CosineInterpolate function. Presumably you wanted to abstract the interpolation method by invoking CosineInterpolate from Interpolate. The first part of the answer tells you how to do that. I hope this helps!

Upvotes: 2

Related Questions