Guest User

Untitled

a guest
Jul 22nd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1.  
  2. /*
  3. * A cubic extrapolator using the polynomial
  4. * P(T) = aT^2 + bT + c
  5. */
  6. function Extrapolator() {
  7. this.s = [];
  8. this.t = [];
  9. this.sol = [];
  10. };
  11.  
  12. /*
  13. * Add a sample to the extrapolator
  14. * @param {Number} s Sample
  15. * @param {Number} t Time
  16. */
  17. Extrapolator.prototype.addSample = function(s, t) {
  18. this.s.push(s);
  19. this.t.push(t);
  20.  
  21. //We only handle 3 samples, so shift if we get more
  22. if(this.s.length > 3) {
  23. this.s.shift();
  24. this.t.shift();
  25. }
  26.  
  27. //Only compute solution if we have 3 samples
  28. if(this.s.length > 2) {
  29. this.sol = Extrapolator._solve(this.s,this.t);
  30. }
  31. };
  32.  
  33. /*
  34. * Return a predicted value from the extrapolator.
  35. * @param {Number} t Time to predict value for.
  36. * @Return {Number} P(T) of polynomial.
  37. */
  38. Extrapolator.prototype.getValue = function(t) {
  39. var s = this.sol;
  40. return s[0] * t * t + s[1] * t + s[2];
  41. };
  42.  
  43. /*
  44. * Solves a 3x3 simultaneous equation system
  45. * using cramers rule.
  46. */
  47. Extrapolator._solve = function(p, t) {
  48.  
  49. //cramers rule.
  50. var t0 = t[0], t1 = t[1], t2 = t[2],
  51. p0 = p[0], p1 = p[1], p2 = p[2],
  52.  
  53. //Set up system
  54. a = t0 * t0, b = t0, c = 1, d = p0,
  55. e = t1 * t1, f = t1, g = 1, h = p1,
  56. i = t2 * t2, j = t2, k = 1, l = p2,
  57.  
  58. //Delta determinant
  59. delta = (a * f * k) + (b * g * i) +
  60. (c * e * j) - (c * f * i) -
  61. (a * g * j) - (b * e * k),
  62.  
  63. //x answer(a);
  64. xnum = (d * f * k) + (b * g * l) +
  65. (c * h * j) - (c * f * l) -
  66. (d * g * j) - (b * h * k),
  67. xans = xnum/delta,
  68.  
  69. //y answer(b)
  70. ynum = (a * h * k) + (d * g * i) +
  71. (c * e * l) - (c * h * i) -
  72. (a * g * l) - (d * e * k),
  73. yans = ynum/delta,
  74.  
  75. //z answer(c)
  76. znum = (a * f * l) + (b * h * i) +
  77. (d * e * j) - (d * f * i) -
  78. (a * h * j) - (b * e * l),
  79. zans = znum/delta;
  80.  
  81. return [xans, yans, zans];
  82. };
  83.  
  84. /*
  85. * A 2D Vector extrapolator using
  86. * one extrapolator for each component
  87. */
  88. function Vector2Extrapolator() {
  89. this.ex = new Extrapolator();
  90. this.ey = new Extrapolator();
  91. };
  92.  
  93. /*
  94. * Add a sample to the extrapolator
  95. * @param {Number} s Sample
  96. * @param {Number} t Time
  97. */
  98. Vector2Extrapolator.prototype.addSample = function(v,t) {
  99. this.ex.addSample(v.x, t);
  100. this.ey.addSample(v.y, t);
  101. };
  102.  
  103. /*
  104. * Return a predicted value from the extrapolator.
  105. * @param {Number} t Time to predict value for.
  106. * @Return {Number} P(T) of polynomial.
  107. */
  108. Vector2Extrapolator.prototype.getValue = function(t) {
  109. var x = this.ex.getValue(t);
  110. var y = this.ey.getValue(t);
  111. return new Vector(x,y);
  112. };
Add Comment
Please, Sign In to add comment