Java Array Interpolate interpolateLinear(int[] vec, int start, int end)

Here you can find the source of interpolateLinear(int[] vec, int start, int end)

Description

Given an array of integers, compute a series of integers as a linear interpolation between two values.

License

Open Source License

Parameter

Parameter Description
vec The array that gives number of values.
start The start value for the linear interpolation.
end The end value for the linear interpolation.

Return

A new array containing the interpolated values.

Declaration

public static int[] interpolateLinear(int[] vec, int start, int end) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*  ww w.  j a  va 2s.  c om*/
     * Given an array of integers, compute a series of integers as a linear
     * interpolation between two values.
     * 
     * @param vec
     *            The array that gives number of values.
     * @param start
     *            The start value for the linear interpolation.
     * @param end
     *            The end value for the linear interpolation.
     * @return A new array containing the interpolated values.
     */
    public static int[] interpolateLinear(int[] vec, int start, int end) {
        double step;
        int length = vec.length;
        int ret[] = new int[vec.length];
        if (start < end) {
            step = (double) (end - start) / (double) length;
            for (int i = 0; i < length; i++) {
                if (vec[i] != 0) {
                    ret[i] = start + (int) (i * step);
                }
            }
        } else if (start > end) {
            step = (double) (start - end) / (double) length;
            for (int i = 0; i < length; i++)
                if (vec[i] != 0)
                    ret[i] = start - (int) (i * step);
        } else
            for (int i = 0; i < length; i++)
                if (vec[i] != 0)
                    ret[i] = start;
        return ret;
    }
}

Related

  1. interpolate(int[] data, double x)
  2. interpolate_linear(int[] x, double[] y, int[] xi)
  3. interpolateArray(int[] array, int interval)
  4. interpolateColor(float[] result, float[] color0, float[] color1, double s)
  5. interpolateLinear(int[] v1, int[] v2, double t)
  6. interpolateNonZeroValues(double[] contour)