summaryrefslogtreecommitdiff
path: root/src/Util
diff options
context:
space:
mode:
Diffstat (limited to 'src/Util')
-rw-r--r--src/Util/Util.js20
-rw-r--r--src/Util/Util.test.js41
2 files changed, 61 insertions, 0 deletions
diff --git a/src/Util/Util.js b/src/Util/Util.js
new file mode 100644
index 0000000..165d1d0
--- /dev/null
+++ b/src/Util/Util.js
@@ -0,0 +1,20 @@
+function useAverage() {
+ var avg = 0;
+ let weight = 0;
+ const append = value => {
+ avg = (weight * avg) + value;
+ weight += 1;
+ avg = avg/weight;
+ }
+
+ return [() => avg, append];
+}
+
+function clamp(value, min, max) {
+ return Math.min(Math.max(value, min), max);
+}
+
+function lerp(a, b, alpha) {
+ return ((1-alpha)*a) + (alpha*b);
+}
+export { useAverage, clamp, lerp };
diff --git a/src/Util/Util.test.js b/src/Util/Util.test.js
new file mode 100644
index 0000000..9c6adbf
--- /dev/null
+++ b/src/Util/Util.test.js
@@ -0,0 +1,41 @@
+import { test, assert} from '../test-assert.js';
+import { useAverage, clamp, lerp } from './Util.js';
+
+
+test('Average correctly accumulates an average', () => {
+ let [avg, avg_append] = useAverage();
+ let data = [];
+ for (let i=0; i<5000; i++) {
+ let d = Math.random();
+ data.push(d);
+ avg_append(d);
+ }
+
+ let manual_average = 0;
+ for (let d of data) manual_average += d;
+ manual_average /= data.length;
+
+ const precision = (decimalPlaces, num) => {
+ const theta = 10**decimalPlaces;
+ return Math.floor(num * theta) / theta;
+ };
+ assert.equal(precision(5, avg()), precision(5, manual_average));
+});
+
+
+test('Clamp correctly constrains values', () => {
+ assert.equal(clamp(5, 0, 10), 5);
+ assert.equal(clamp(-1, 0, 10), 0);
+ assert.equal(clamp(15, 0, 10), 10);
+});
+
+
+test ('Lerp correctly interpolates values', () => {
+ const a = 15;
+ const b = 10;
+ assert.equal(lerp(a, b, 0), a);
+ assert.equal(lerp(a, b, 1), b);
+ assert.equal(lerp(a, b, 0.5), (a+b)/2);
+ assert.equal(lerp(a, b, 0.25), (3*a + b)/4);
+ assert.equal(lerp(a, b, 0.75), (a + (3*b))/4);
+});