Source: Core/LinearApproximation.js

  1. /*global define*/
  2. define([
  3. './defined',
  4. './DeveloperError'
  5. ], function(
  6. defined,
  7. DeveloperError) {
  8. 'use strict';
  9. /**
  10. * An {@link InterpolationAlgorithm} for performing linear interpolation.
  11. *
  12. * @exports LinearApproximation
  13. */
  14. var LinearApproximation = {
  15. type : 'Linear'
  16. };
  17. /**
  18. * Given the desired degree, returns the number of data points required for interpolation.
  19. * Since linear interpolation can only generate a first degree polynomial, this function
  20. * always returns 2.
  21. * @param {Number} degree The desired degree of interpolation.
  22. * @returns {Number} This function always returns 2.
  23. *
  24. */
  25. LinearApproximation.getRequiredDataPoints = function(degree) {
  26. return 2;
  27. };
  28. /**
  29. * Interpolates values using linear approximation.
  30. *
  31. * @param {Number} x The independent variable for which the dependent variables will be interpolated.
  32. * @param {Number[]} xTable The array of independent variables to use to interpolate. The values
  33. * in this array must be in increasing order and the same value must not occur twice in the array.
  34. * @param {Number[]} yTable The array of dependent variables to use to interpolate. For a set of three
  35. * dependent values (p,q,w) at time 1 and time 2 this should be as follows: {p1, q1, w1, p2, q2, w2}.
  36. * @param {Number} yStride The number of dependent variable values in yTable corresponding to
  37. * each independent variable value in xTable.
  38. * @param {Number[]} [result] An existing array into which to store the result.
  39. * @returns {Number[]} The array of interpolated values, or the result parameter if one was provided.
  40. */
  41. LinearApproximation.interpolateOrderZero = function(x, xTable, yTable, yStride, result) {
  42. //>>includeStart('debug', pragmas.debug);
  43. if (xTable.length !== 2) {
  44. throw new DeveloperError('The xTable provided to the linear interpolator must have exactly two elements.');
  45. } else if (yStride <= 0) {
  46. throw new DeveloperError('There must be at least 1 dependent variable for each independent variable.');
  47. }
  48. //>>includeEnd('debug');
  49. if (!defined(result)) {
  50. result = new Array(yStride);
  51. }
  52. var i;
  53. var y0;
  54. var y1;
  55. var x0 = xTable[0];
  56. var x1 = xTable[1];
  57. //>>includeStart('debug', pragmas.debug);
  58. if (x0 === x1) {
  59. throw new DeveloperError('Divide by zero error: xTable[0] and xTable[1] are equal');
  60. }
  61. //>>includeEnd('debug');
  62. for (i = 0; i < yStride; i++) {
  63. y0 = yTable[i];
  64. y1 = yTable[i + yStride];
  65. result[i] = (((y1 - y0) * x) + (x1 * y0) - (x0 * y1)) / (x1 - x0);
  66. }
  67. return result;
  68. };
  69. return LinearApproximation;
  70. });