Path: blob/trunk/third_party/closure/goog/math/interpolator/linear1.js
2868 views
// Copyright 2012 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview A one dimensional linear interpolator.16*17*/1819goog.provide('goog.math.interpolator.Linear1');2021goog.require('goog.array');22goog.require('goog.asserts');23goog.require('goog.math');24goog.require('goog.math.interpolator.Interpolator1');25262728/**29* A one dimensional linear interpolator.30* @implements {goog.math.interpolator.Interpolator1}31* @constructor32* @final33*/34goog.math.interpolator.Linear1 = function() {35/**36* The abscissa of the data points.37* @type {!Array<number>}38* @private39*/40this.x_ = [];4142/**43* The ordinate of the data points.44* @type {!Array<number>}45* @private46*/47this.y_ = [];48};495051/** @override */52goog.math.interpolator.Linear1.prototype.setData = function(x, y) {53goog.asserts.assert(54x.length == y.length,55'input arrays to setData should have the same length');56if (x.length == 1) {57this.x_ = [x[0], x[0] + 1];58this.y_ = [y[0], y[0]];59} else {60this.x_ = x.slice();61this.y_ = y.slice();62}63};646566/** @override */67goog.math.interpolator.Linear1.prototype.interpolate = function(x) {68var pos = goog.array.binarySearch(this.x_, x);69if (pos < 0) {70pos = -pos - 2;71}72pos = goog.math.clamp(pos, 0, this.x_.length - 2);7374var progress = (x - this.x_[pos]) / (this.x_[pos + 1] - this.x_[pos]);75return goog.math.lerp(this.y_[pos], this.y_[pos + 1], progress);76};777879/** @override */80goog.math.interpolator.Linear1.prototype.getInverse = function() {81var interpolator = new goog.math.interpolator.Linear1();82interpolator.setData(this.y_, this.x_);83return interpolator;84};858687