Skip to content

Instantly share code, notes, and snippets.

@mroth23
Created May 5, 2010 16:14
Show Gist options
  • Save mroth23/391019 to your computer and use it in GitHub Desktop.
Save mroth23/391019 to your computer and use it in GitHub Desktop.
//Linear interpolation of a 2D double array
private double[,] Interpolate(double[,] matrix, int dim)
{
double[,] result = new double[dim, dim];
double sf = (double)(matrix.GetLength(0) - 1) / (double)(dim - 1d);
for (int x = 0; x < dim; x++)
{
for (int y = 0; y < dim; y++)
{
double oldX = sf * x;
double oldY = sf * y;
double q12 = matrix[(int)Math.Floor(oldX), (int)Math.Floor(oldY)]; //Oben links
double q22 = matrix[(int)Math.Ceiling(oldX), (int)Math.Floor(oldY)]; //Oben rechts
double q11 = matrix[(int)Math.Floor(oldX), (int)Math.Ceiling(oldY)]; //Unten links
double q21 = matrix[(int)Math.Ceiling(oldX), (int)Math.Ceiling(oldY)];//Unten rechts
double x1 = Math.Floor(oldX); //Links
double x2 = Math.Ceiling(oldX); //Rechts
double y2 = Math.Floor(oldY); //Oben
double y1 = Math.Ceiling(oldY); //Unten
double x0 = (x2 - oldX);
double y0 = (y1 - oldY);
double newValue = q11 * (1d - x0) * (1d - y0) +
q21 * x0 * (1d - y0) +
q12 * y0 * (1d - x0) +
q22 * x0 * y0;
result[x, y] = newValue;
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment