Here you can find the source of interpolate(int[] data, double x)
Parameter | Description |
---|---|
data | the specified <tt>int</tt> matrix |
x | the decimal index |
public static final double interpolate(int[] data, double x)
//package com.java2s; public class Main { /**// w w w. j a va2s . c om * Returns the value of specified decimal index. * <p> * Calculating algorithm is implemented by first-order interpolation. * * <pre> * f(x) = f(x') + (f(x'+1) - f(x')) * (x - x') * x' = rounded_down(x) * </pre> * * </p> * * @param data * the specified <tt>int</tt> matrix * @param x * the decimal index * @return the interpolation value */ public static final double interpolate(int[] data, double x) { if (data.length == 0) return 0; if (x > data.length - 1) return data[data.length]; if (x < 0) return data[0]; int dx = (int) x; return data[dx] + (data[dx + 1] - (double) data[dx]) * (x - dx); } /** * Returns the value of specified decimal index. * <p> * Calculating algorithm is implemented by first-order interpolation. * * <pre> * f(x) = f(x') + (f(x'+1) - f(x')) * (x - x') * x' = rounded_down(x) * </pre> * * </p> * * @param data * the specified <tt>long</tt> array * @param x * the decimal index * @return the interpolation value */ public static final double interpolate(long[] data, double x) { if (data.length == 0) return 0; if (x > data.length - 1) return data[data.length]; if (x < 0) return data[0]; int dx = (int) x; return data[dx] + (data[dx + 1] - (double) data[dx]) * (x - dx); } /** * Returns the value of specified decimal index. * <p> * Calculating algorithm is implemented by first-order interpolation. * * <pre> * f(x) = f(x') + (f(x'+1) - f(x')) * (x - x') * x' = rounded_down(x) * </pre> * * </p> * * @param data * the specified <tt>float</tt> array * @param x * the decimal index * @return the interpolation value */ public static final double interpolate(float[] data, double x) { if (data.length == 0) return 0; if (x > data.length - 1) return data[data.length]; if (x < 0) return data[0]; int dx = (int) x; return data[dx] + (data[dx + 1] - (double) data[dx]) * (x - dx); } /** * Returns the value of specified decimal index. * <p> * Calculating algorithm is implemented by first-order interpolation. * * <pre> * f(x) = f(x') + (f(x'+1) - f(x')) * (x - x') * x' = rounded_down(x) * </pre> * * </p> * * @param data * the specified <tt>double</tt> array * @param x * the decimal index * @return the interpolation value */ public static final double interpolate(double[] data, double x) { if (data.length == 0) return 0; if (x > data.length - 1) return data[data.length]; if (x < 0) return data[0]; int dx = (int) x; return data[dx] + (data[dx + 1] - (double) data[dx]) * (x - dx); } /** * Returns the value of specified decimal index. * <p> * Calculating algorithm is implemented by first-order interpolation. * * <pre> * f(x) = f(x') + (f(x'+1) - f(x')) * (x - x') * x' = rounded_down(x) * </pre> * * </p> * * @param data * the specified <tt>Integer</tt> array * @param x * the decimal index * @return the interpolation value */ public static final double interpolate(Integer[] data, double x) { if (data.length == 0) return 0; if (x > data.length - 1) return data[data.length]; if (x < 0) return data[0]; int dx = (int) x; return data[dx] + (data[dx + 1] - (double) data[dx]) * (x - dx); } /** * Returns the value of specified decimal index. * <p> * Calculating algorithm is implemented by first-order interpolation. * * <pre> * f(x) = f(x') + (f(x'+1) - f(x')) * (x - x') * x' = rounded_down(x) * </pre> * * </p> * * @param data * the specified <tt>Long</tt> array * @param x * the decimal index * @return the interpolation value */ public static final double interpolate(Long[] data, double x) { if (data.length == 0) return 0; if (x > data.length - 1) return data[data.length]; if (x < 0) return data[0]; int dx = (int) x; return data[dx] + (data[dx + 1] - (double) data[dx]) * (x - dx); } /** * Returns the value of specified decimal index. * <p> * Calculating algorithm is implemented by first-order interpolation. * * <pre> * f(x) = f(x') + (f(x'+1) - f(x')) * (x - x') * x' = rounded_down(x) * </pre> * * </p> * * @param data * the specified <tt>Float</tt> array * @param x * the decimal index * @return the interpolation value */ public static final double interpolate(Float[] data, double x) { if (data.length == 0) return 0; if (x > data.length - 1) return data[data.length]; if (x < 0) return data[0]; int dx = (int) x; return data[dx] + (data[dx + 1] - (double) data[dx]) * (x - dx); } /** * Returns the value of specified decimal index. * <p> * Calculating algorithm is implemented by first-order interpolation. * * <pre> * f(x,y) = f(x',y') + (f(x'+1,y') - f(x',y')) * (x - x') + (f(x',y'+1) - f(x',y')) * (y - y') * + (f(x'+1,y'+1)+f(x',y' )-f(x'+1,y' )-f(x',y'+1)) * (x - x') * (y - y') * x' = rounded_down(x) * </pre> * * </p> * * @param data * the specified <tt>Double</tt> array * @param x * the decimal index * @return the interpolation value */ public static final double interpolate(Double[] data, double x) { if (data.length == 0) return 0; if (x > data.length - 1) return data[data.length]; if (x < 0) return data[0]; int dx = (int) x; return data[dx] + (data[dx + 1] - (double) data[dx]) * (x - dx); } /** * Returns the value of specified decimal position(x,y). * <p> * Calculating algorithm is implemented by 2D first-order interpolation. * * <pre> * f(x,y) = f(x',y') + (f(x'+1,y') - f(x',y')) * (x - x') + (f(x',y'+1) - f(x',y')) * (y - y') * + (f(x'+1,y'+1)+f(x',y' )-f(x'+1,y' )-f(x',y'+1)) * (x - x') * (y - y') * x' = rounded_down(x) * </pre> * * </p> * * @param data * the specified <tt>int</tt> matrix * @param x * the decimal x coordinate * @param y * the decimal y coordinate * @return the interpolation value */ public static final double interpolate(int[][] data, double x, double y) { if (data.length == 0) return 0; if (x > data.length - 1 || y > data[0].length - 1) return data[data.length - 1][data[data.length - 1].length - 1]; if (x < 0 || y < 0) return data[0][0]; int dx = (int) x; int dy = (int) y; int p00 = data[dx][dy]; int p01 = data[dx][dy + 1]; int p10 = data[dx + 1][dy]; int p11 = data[dx + 1][dy + 1]; return p00 + (p10 - p00) * (x - dx) + (p01 - p00) * (y - dy) - (p11 + p00 - p10 - p11) * (x - dx) * (y - dy); } /** * Returns the value of specified decimal position(x,y). * <p> * Calculating algorithm is implemented by 2D first-order interpolation. * * <pre> * f(x,y) = f(x',y') + (f(x'+1,y') - f(x',y')) * (x - x') + (f(x',y'+1) - f(x',y')) * (y - y') * + (f(x'+1,y'+1)+f(x',y' )-f(x'+1,y' )-f(x',y'+1)) * (x - x') * (y - y') * x' = rounded_down(x) * </pre> * * </p> * * @param data * the specified <tt>long</tt> matrix * @param x * the decimal x coordinate * @param y * the decimal y coordinate * @return the interpolation value */ public static final double interpolate(long[][] data, double x, double y) { if (data.length == 0) return 0; if (x > data.length - 1 || y > data[0].length - 1) return data[data.length - 1][data[data.length - 1].length - 1]; if (x < 0 || y < 0) return data[0][0]; int dx = (int) x; int dy = (int) y; long p00 = data[dx][dy]; long p01 = data[dx][dy + 1]; long p10 = data[dx + 1][dy]; long p11 = data[dx + 1][dy + 1]; return p00 + (p10 - p00) * (x - dx) + (p01 - p00) * (y - dy) - (p11 + p00 - p10 - p11) * (x - dx) * (y - dy); } /** * Returns the value of specified decimal position(x,y). * <p> * Calculating algorithm is implemented by 2D first-order interpolation. * * <pre> * f(x,y) = f(x',y') + (f(x'+1,y') - f(x',y')) * (x - x') + (f(x',y'+1) - f(x',y')) * (y - y') * + (f(x'+1,y'+1)+f(x',y' )-f(x'+1,y' )-f(x',y'+1)) * (x - x') * (y - y') * x' = rounded_down(x) * </pre> * * </p> * * @param data * the specified <tt>float</tt> matrix * @param x * the decimal x coordinate * @param y * the decimal y coordinate * @return the interpolation value */ public static final double interpolate(float[][] data, double x, double y) { if (data.length == 0) return 0; if (x > data.length - 1 || y > data[0].length - 1) return data[data.length - 1][data[data.length - 1].length - 1]; if (x < 0 || y < 0) return data[0][0]; int dx = (int) x; int dy = (int) y; float p00 = data[dx][dy]; float p01 = data[dx][dy + 1]; float p10 = data[dx + 1][dy]; float p11 = data[dx + 1][dy + 1]; return p00 + (p10 - p00) * (x - dx) + (p01 - p00) * (y - dy) - (p11 + p00 - p10 - p11) * (x - dx) * (y - dy); } /** * Returns the value of specified decimal position(x,y). * <p> * Calculating algorithm is implemented by 2D first-order interpolation. * * <pre> * f(x,y) = f(x',y') + (f(x'+1,y') - f(x',y')) * (x - x') + (f(x',y'+1) - f(x',y')) * (y - y') * + (f(x'+1,y'+1)+f(x',y' )-f(x'+1,y' )-f(x',y'+1)) * (x - x') * (y - y') * x' = rounded_down(x) * </pre> * * </p> * * @param data * the specified <tt>double</tt> matrix * @param x * the decimal x coordinate * @param y * the decimal y coordinate * @return the interpolation value */ public static final double interpolate(double[][] data, double x, double y) { if (data.length == 0) return 0; if (x < 0 || y < 0) return data[0][0]; int dx = (int) x; int dy = (int) y; int maxY = data.length - 1; int maxX = data[maxY].length - 1; if (dy + 1 > maxY) { if (dx + 1 > maxX) return data[maxY][maxX]; else // calculate when only y is at the end return (x - dx) * data[maxY][dx] + (dx + 1 - x) * data[maxY][dx + 1]; } else if (dx + 1 > maxX) // calculate when only x is at the end return (y - dy) * data[dy][maxX] + (dy + 1 - y) * data[dy + 1][maxX]; double p00 = data[dy][dx]; double p01 = data[dy + 1][dx]; double p10 = data[dy][dx + 1]; double p11 = data[dy + 1][dx + 1]; return p00 + (p10 - p00) * (x - dx) + (p01 - p00) * (y - dy) - (p11 + p00 - p10 - p11) * (x - dx) * (y - dy); } /** * Returns the value of specified decimal position(x,y). * <p> * Calculating algorithm is implemented by 2D first-order interpolation. * * <pre> * f(x,y) = f(x',y') + (f(x'+1,y') - f(x',y')) * (x - x') + (f(x',y'+1) - f(x',y')) * (y - y') * + (f(x'+1,y'+1)+f(x',y' )-f(x'+1,y' )-f(x',y'+1)) * (x - x') * (y - y') * x' = rounded_down(x) * </pre> * * </p> * * @param data * the specified <tt>double</tt> matrix * @param x * the decimal x coordinate * @param y * the decimal y coordinate * @return the interpolation value */ public static final <T extends Number> double interpolate(T[][] data, double x, double y) { if (data.length == 0) return 0; if (x > data.length - 1 || y > data[0].length - 1) return data[data.length - 1][data[data.length - 1].length - 1].doubleValue(); if (x < 0 || y < 0) return data[0][0].doubleValue(); int dx = (int) x; int dy = (int) y; double p00 = data[dx][dy].doubleValue(); double p01 = data[dx][dy + 1].doubleValue(); double p10 = data[dx + 1][dy].doubleValue(); double p11 = data[dx + 1][dy + 1].doubleValue(); return p00 + (p10 - p00) * (x - dx) + (p01 - p00) * (y - dy) - (p11 + p00 - p10 - p11) * (x - dx) * (y - dy); } }