All notes
Interpolati

# Intro

## Common algorithmss

### Linear

• The points are simply joined by straight line segments.
• Each segment (bounded by two data points) can be interpolated independently.

 

double LinearInterpolate(
double y1,double y2,
double mu)
{
return(y1*(1-mu)+y2*mu);
}

 

### Cosine

 

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

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

 

### Cubic

• Cubic interpolation is the simplest method that offers true continuity between the segments.
• It requires more than just the two endpoints of the segment but also the two points on either side of them. So the function requires 4 points.

 

double CubicInterpolate(
double y0,double y1,
double y2,double y3,
double mu)
{
double a0,a1,a2,a3,mu2;

mu2 = mu*mu;
a0 = y3 - y2 - y0 + y1;
a1 = y0 - y1 - a0;
a2 = y2 - y0;
a3 = y1;

return(a0*mu*mu2+a1*mu2+a2*mu+a3);
}

 

### Catmull-Rom splines

• Paul Breeuwsma proposes the following coefficients for a smoother interpolated curve.
• It uses the slope between the previous point and the next as the derivative at the current point.

 

double CubicInterpolate(
double y0,double y1,
double y2,double y3,
double mu)
{
double a0,a1,a2,a3,mu2;

mu2 = mu*mu;
a0 = -0.5*y0 + 1.5*y1 - 1.5*y2 + 0.5*y3;
a1 = y0 - 2.5*y1 + 2*y2 - 0.5*y3;
a2 = -0.5*y0 + 0.5*y2;
a3 = y1;

return(a0*mu*mu2+a1*mu2+a2*mu+a3);
}

 

### Hermite

• Like cubic requires 4 points.
• Has nice tension and biasing controls. Tension can be used to tighten up the curvature at the known points. The bias is used to twist the curve about the known points.

 

/*
Tension: 1 is high, 0 normal, -1 is low
Bias: 0 is even,
positive is towards first segment,
negative towards the other
*/
double HermiteInterpolate(
double y0,double y1,
double y2,double y3,
double mu,
double tension,
double bias)
{
ouble m0,m1,mu2,mu3;
ouble a0,a1,a2,a3;

mu2 = mu * mu;
mu3 = mu2 * mu;
m0  = (y1-y0)*(1+bias)*(1-tension)/2;
m0 += (y2-y1)*(1-bias)*(1-tension)/2;
m1  = (y2-y1)*(1+bias)*(1-tension)/2;
m1 += (y3-y2)*(1-bias)*(1-tension)/2;
a0 =  2*mu3 - 3*mu2 + 1;
a1 =    mu3 - 2*mu2 + mu;
a2 =    mu3 -   mu2;
a3 = -2*mu3 + 3*mu2;

return(a0*y1+a1*m0+a2*m1+a3*y2);
}