Snippets

Benjamin J. DeLong Interpolations

Updated by Benjamin J. DeLong

File interpolations.js Modified

  • Ignore whitespace
  • Hide word diff
     }
     return interpolated;
 }
-
 /*
 
 Interpolate constructor
 Usage:
 
 > translate = new Interpolate();
-> val = translate.from(0, 5).to(0, 100).linear(2.5);
+> val = translate.from(0, 5).to(0, 100).val(2.5);
 > console.assert(val === 50);
 
 */
 function Interpolate () {
-	this.input_min = null;
-	this.input_max = null;
+	this.input_from = null;
+	this.input_to = null;
+	this.output_from = null;
+	this.output_to = null;
 	this.output_min = null;
 	this.output_max = null;
 }
 
 Interpolate.prototype.from = function (min, max) {
-	this.input_min = Number( min );
-	this.input_max = Number( max );
+	/*
+	
+	x values (input range)
+
+	*/
+	this.input_from = Number( min );
+	this.input_to = Number( max );
 	return this;
 };
 
-Interpolate.prototype.to = function (min, max) {
-	this.output_min = Number( min );
-	this.output_max = Number( max );
+Interpolate.prototype.to = function (_min, _max) {
+	/*
+
+	y values (desired output range)
+
+	*/
+	var min = Number( _min ),
+		max = Number( _max );
+	this.output_from = min;
+	this.output_to = max;
+	this.output_min = Math.min(min, max);
+	this.output_max = Math.max(min, max);
 	return this;
 };
 
+Interpolate.prototype.checkBounds = function (output) {
+	/*
+
+	check if value is between max/min
+
+	*/
+	if (output > this.output_max) {
+		return this.output_max;
+	} 
+
+	if (output < this.output_min) {
+	    return this.output_min;
+	}
+
+	return output;
+};
+
 Interpolate.prototype.linear = function (val) {
-	var output = (val - this.input_min) / 
-		(this.input_max - this.input_min) * 
-		(this.output_max - this.output_min) + 
-		this.output_min;
-
-    if (output > this.output_max ||
-    	output < this.output_min) {
-        return false;
-    }
+	var output = (val - this.input_from) / 
+		(this.input_to - this.input_from) * 
+		(this.output_to - this.output_from) + 
+		this.output_from;
 
-    return output;
+    return this.checkBounds( output );
 };
 
 Interpolate.prototype.exponential = function (val) {
 	y = ab^x
 	*/
 	var offset = 0,
-		a = this.output_min,
-		y2 = this.output_max,
-		x2 = this.input_max - this.input_min,
+		a = this.output_from,
+		y2 = this.output_to,
+		x2 = this.input_to - this.input_from,
 		b,
 		output;
 
-
-	if (val === this.input_max) {
-		return this.output_max;
+	if (val === this.input_to) {
+		return this.output_to;
 	}
 
-	if (val === this.input_min) {
-		return this.output_min;
+	if (val === this.input_from) {
+		return this.output_from;
 	}
 
 	// x is an index (starts at 0)
-	val -= this.input_min;
+	val -= this.input_from;
 
 	if (a <= 0 || y2 <= 0) {
 		// can't have y = 0 or less for pow functions
 	// however, b is supposed to equal y
 	// when x == 0; so apparently this 
 	// is a formula to find that
-	b = Math.pow(y2 / y1, 1 / x2);
+	b = Math.pow(y2 / a, 1 / x2);
 
 	// formula here (reapply offset to deal with 0's)
 	output = a * Math.pow(b, val) - offset;
 
-    if (output > this.output_max) {
-    	return this.output_max;
-    } 
-
-    if (output < this.output_min) {
-        return this.output_min;
-    }
-
-    return output;
+    return this.checkBounds( output );
 };
Updated by Benjamin J. DeLong

File interpolations.js Modified

  • Ignore whitespace
  • Hide word diff
     return interpolated;
 }
 
-function linearInterpolate (val, from_range, to_range) {
-    if (from_range.indexOf(val) !== -1) {
-        // just return the value
-        return to_range[from_range.indexOf(val)];
-    }
-    if (val < from_range[0]) {
-        return to_range[0];
-    }
-    if (val > from_range[from_range.length - 1]) {
-        return to_range[to_range.length - 1];
+/*
+
+Interpolate constructor
+
+With properties for the expected min/max outputs
+
+Usage:
+
+> translate = new Interpolate();
+> val = translate.from(0, 5).to(0, 100).linear(2.5);
+> console.assert(val === 50);
+
+*/
+function Interpolate () {
+	this.input_min = null;
+	this.input_max = null;
+	this.output_min = null;
+	this.output_max = null;
+}
+
+Interpolate.prototype.from = function (min, max) {
+	this.input_min = Number( min );
+	this.input_max = Number( max );
+	return this;
+};
+
+Interpolate.prototype.to = function (min, max) {
+	this.output_min = Number( min );
+	this.output_max = Number( max );
+	return this;
+};
+
+Interpolate.prototype.linear = function (val) {
+	var output = (val - this.input_min) / 
+		(this.input_max - this.input_min) * 
+		(this.output_max - this.output_min) + 
+		this.output_min;
+
+    if (output > this.output_max ||
+    	output < this.output_min) {
+        return false;
     }
-    if (from_range.length > 2) {
-        // get subsections
-        var ranges = (function () {
-                from_range.sort();
-                for (var i = 0, len = from_range.length; i < len; i++) {
-                    var item = from_range[i];
-                    if (item > val) {
-                        return [
-                            from_range.slice(i - 1, i + 1),
-                            to_range.slice(i - 1, i + 1),
-                        ];
-                    }
-                }
-            })(),
-            from_range = ranges[0],
-            to_range = ranges[1];
+
+    return output;
+};
+
+Interpolate.prototype.exponential = function (val) {
+	/*
+	exponential rise
+	y = ab^x
+	*/
+	var offset = 0,
+		a = this.output_min,
+		y2 = this.output_max,
+		x2 = this.input_max - this.input_min,
+		b,
+		output;
+
+
+	if (val === this.input_max) {
+		return this.output_max;
+	}
+
+	if (val === this.input_min) {
+		return this.output_min;
+	}
+
+	// x is an index (starts at 0)
+	val -= this.input_min;
+
+	if (a <= 0 || y2 <= 0) {
+		// can't have y = 0 or less for pow functions
+		offset = Math.abs(Math.min(a, y2)) + 1;
+		a += offset;
+		y2 += offset;
+	}
+
+	// I don't remember why I did this - BJD
+	// however, b is supposed to equal y
+	// when x == 0; so apparently this 
+	// is a formula to find that
+	b = Math.pow(y2 / y1, 1 / x2);
+
+	// formula here (reapply offset to deal with 0's)
+	output = a * Math.pow(b, val) - offset;
+
+    if (output > this.output_max) {
+    	return this.output_max;
+    } 
+
+    if (output < this.output_min) {
+        return this.output_min;
     }
-    // ready
-    var minX = from_range[0],
-        minY = to_range[0],
-        rangeX = from_range[1] - from_range[0],
-        rangeY = to_range[1] - to_range[0];
-
-    return (val - minX) * rangeY / rangeX + minY;
-}
+
+    return output;
+};
Created by Benjamin J. DeLong

File interpolations.js Added

  • Ignore whitespace
  • Hide word diff
+function linearList (min, max, len) {
+    var interpolated = [];
+    var range = max - min;
+    var diff = range / (len - 1);
+    var value = +min;
+    for (var i = 0; i < len; i++) {
+        interpolated.push(value);
+        value += diff;
+    }
+    return interpolated;
+}
+
+function linearInterpolate (val, from_range, to_range) {
+    if (from_range.indexOf(val) !== -1) {
+        // just return the value
+        return to_range[from_range.indexOf(val)];
+    }
+    if (val < from_range[0]) {
+        return to_range[0];
+    }
+    if (val > from_range[from_range.length - 1]) {
+        return to_range[to_range.length - 1];
+    }
+    if (from_range.length > 2) {
+        // get subsections
+        var ranges = (function () {
+                from_range.sort();
+                for (var i = 0, len = from_range.length; i < len; i++) {
+                    var item = from_range[i];
+                    if (item > val) {
+                        return [
+                            from_range.slice(i - 1, i + 1),
+                            to_range.slice(i - 1, i + 1),
+                        ];
+                    }
+                }
+            })(),
+            from_range = ranges[0],
+            to_range = ranges[1];
+    }
+    // ready
+    var minX = from_range[0],
+        minY = to_range[0],
+        rangeX = from_range[1] - from_range[0],
+        rangeY = to_range[1] - to_range[0];
+
+    return (val - minX) * rangeY / rangeX + minY;
+}
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.