Snippets
Created by
Benjamin J. DeLong
last modified
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | 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;
}
/*
Interpolate constructor
With properties for the expected min/max outputs
Usage:
> translate = new Interpolate();
> val = translate.from(0, 5).to(0, 100).val(2.5);
> console.assert(val === 50);
*/
function Interpolate () {
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) {
/*
x values (input range)
*/
this.input_from = Number( min );
this.input_to = Number( max );
return this;
};
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_from) /
(this.input_to - this.input_from) *
(this.output_to - this.output_from) +
this.output_from;
return this.checkBounds( output );
};
Interpolate.prototype.exponential = function (val) {
/*
exponential rise
y = ab^x
*/
var offset = 0,
a = this.output_from,
y2 = this.output_to,
x2 = this.input_to - this.input_from,
b,
output;
if (val === this.input_to) {
return this.output_to;
}
if (val === this.input_from) {
return this.output_from;
}
// x is an index (starts at 0)
val -= this.input_from;
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 / a, 1 / x2);
// formula here (reapply offset to deal with 0's)
output = a * Math.pow(b, val) - offset;
return this.checkBounds( output );
};
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.