Class Interpolation


  • public final class Interpolation
    extends java.lang.Object
    Interpolate or clamp within a range of values.
    Since:
    3.0
    Author:
    Chris Jennings
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Interpolation.CubicSpline
      A CubicSpline interpolator passes through each point in its data set, connecting the points with cubic spline curves.
      static class  Interpolation.InterpolatedFunction
      This is a base class for interpolated functions.
      static class  Interpolation.LinearRegression
      A LinearRegression function finds the line of best fit for a set of (usually noisy) data points.
      static class  Interpolation.QuadraticRegression
      A QuadraticRegression function finds the quadratic curve of best fit for a set of (usually noisy) data points.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static double clamp​(double value, double low, double high)
      Clamp a double value to fall within a range from low to high.
      static float clamp​(float value, float low, float high)
      Clamp a float value to fall within a range from low to high.
      static int clamp​(int value, int low, int high)
      Clamp an integer value to fall within a range from low to high.
      static Fn createLerpFunction​(double low, double high)
      Returns a function that performs linear interpolation.
      static Fn createMapFunction​(double low1, double high1, double low2, double high2)
      Returns a function that maps the input x value from the range (low1, high1) to (low2, high2).
      static double lerp​(double position, double low, double high)
      Return a linear interpolation between two double values.
      static float lerp​(float position, float low, float high)
      Return a linear interpolation between two float values.
      static int lerp​(float position, int low, int high)
      Return a linear interpolation between two int values.
      static double map​(double position, double low1, double high1, double low2, double high2)
      Maps a position in one range of double values to another range.
      static float map​(float position, float low1, float high1, float low2, float high2)
      Maps a position in one range of float values to another range.
      static int map​(int position, int low1, int high1, int low2, int high2)
      Maps a position in one range of integer values to another range.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • lerp

        public static double lerp​(double position,
                                  double low,
                                  double high)
        Return a linear interpolation between two double values.
        Parameters:
        position - the desired relative point between the low and high
        low - the low end of the range
        high - the high end of the range
        Returns:
        the value that is position*100% of the way between low and high
      • createLerpFunction

        public static Fn createLerpFunction​(double low,
                                            double high)
        Returns a function that performs linear interpolation.
        Parameters:
        low - the low end of the range
        high - the high end of the range
        Returns:
        the function f(x) = lerp( x, low, high )
      • lerp

        public static float lerp​(float position,
                                 float low,
                                 float high)
        Return a linear interpolation between two float values.
        Parameters:
        position - the desired relative point between the low and high
        low - the low end of the range
        high - the high end of the range
        Returns:
        the value that is position*100% of the way between low and high
      • lerp

        public static int lerp​(float position,
                               int low,
                               int high)
        Return a linear interpolation between two int values.
        Parameters:
        position - the desired relative point between the low and high
        low - the low end of the range
        high - the high end of the range
        Returns:
        the nearest integer value that is position*100% of the way between low and high
      • map

        public static double map​(double position,
                                 double low1,
                                 double high1,
                                 double low2,
                                 double high2)
        Maps a position in one range of double values to another range. This is the same as using lerp(double, double, double) to interpolate between low2 and high2, except that the ratio between the two is determined from the ratio of position between low1 and high1. (So if position is half way between low1 and high1, this is the same as calling lerp( 0.5, low2, high2 ).
        Parameters:
        position - the position in (low1, high1) to map to (low2, high2)
        low1 - the low end of the original range
        high1 - the high end of the original range
        low2 - the low end of the new range
        high2 - the high end of the new range
        Returns:
        the relative value of position between low1 and high1 mapped to the range between low2 and high2
        Throws:
        java.lang.IllegalArgumentException - if low1 and high1 are equal, in which case the range is 0 and position does not represent any one ratio
      • createMapFunction

        public static Fn createMapFunction​(double low1,
                                           double high1,
                                           double low2,
                                           double high2)
        Returns a function that maps the input x value from the range (low1, high1) to (low2, high2).
        Parameters:
        low1 - the low end of the original range
        high1 - the high end of the original range
        low2 - the low end of the new range
        high2 - the high end of the new range
        Returns:
        the function f(x) = map( x, low1, high1, low2, high2 )
      • map

        public static float map​(float position,
                                float low1,
                                float high1,
                                float low2,
                                float high2)
        Maps a position in one range of float values to another range. This is the same as using lerp(double, double, double) to interpolate between low2 and high2, except that the ratio between the two is determined from the ratio of position between low1 and high1. (So if position is half way between low1 and high1, this is the same as calling lerp( 0.5, low2, high2 ).
        Parameters:
        position - the position in (low1, high1) to map to (low2, high2)
        low1 - the low end of the original range
        high1 - the high end of the original range
        low2 - the low end of the new range
        high2 - the high end of the new range
        Returns:
        the relative value of position between low1 and high1 mapped to the range between low2 and high2
        Throws:
        java.lang.IllegalArgumentException - if low1 and high1 are equal, in which case the range is 0 and position does not represent any one ratio
      • map

        public static int map​(int position,
                              int low1,
                              int high1,
                              int low2,
                              int high2)
        Maps a position in one range of integer values to another range. This is the same as using lerp(double, double, double) to interpolate between low2 and high2, except that the ratio between the two is determined from the ratio of position between low1 and high1. (So if position is half way between low1 and high1, this is the same as calling lerp( 0.5, low2, high2 ).
        Parameters:
        position - the position in (low1, high1) to map to (low2, high2)
        low1 - the low end of the original range
        high1 - the high end of the original range
        low2 - the low end of the new range
        high2 - the high end of the new range
        Returns:
        the relative value of position between low1 and high1 mapped to the range between low2 and high2
        Throws:
        java.lang.IllegalArgumentException - if low1 and high1 are equal, in which case the range is 0 and position does not represent any one ratio
      • clamp

        public static double clamp​(double value,
                                   double low,
                                   double high)
        Clamp a double value to fall within a range from low to high. Values less than low will be increased to low, values greater than high will be reduced to high, and other values will be returned unchanged.
        Parameters:
        value - the value to clamp
        low - the lowest acceptable value
        high - the highest acceptable value
        Returns:
        the clamped value
      • clamp

        public static float clamp​(float value,
                                  float low,
                                  float high)
        Clamp a float value to fall within a range from low to high. Values less than low will be increased to low, values greater than high will be reduced to high, and other values will be returned unchanged.
        Parameters:
        value - the value to clamp
        low - the lowest acceptable value
        high - the highest acceptable value
        Returns:
        the clamped value
      • clamp

        public static int clamp​(int value,
                                int low,
                                int high)
        Clamp an integer value to fall within a range from low to high. Values less than low will be increased to low, values greater than high will be reduced to high, and other values will be returned unchanged.
        Parameters:
        value - the value to clamp
        low - the lowest acceptable value
        high - the highest acceptable value
        Returns:
        the clamped value