summaryrefslogtreecommitdiff
path: root/src/utils/unitizeVector.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/unitizeVector.js')
-rw-r--r--src/utils/unitizeVector.js11
1 files changed, 11 insertions, 0 deletions
diff --git a/src/utils/unitizeVector.js b/src/utils/unitizeVector.js
new file mode 100644
index 0000000..6297ca8
--- /dev/null
+++ b/src/utils/unitizeVector.js
@@ -0,0 +1,11 @@
+const unitize = (vector) => {
+ // Not *technically* a unit vector, but has x,y components of |magnitude| = 1
+ Object.keys(vector).forEach((key) => {
+ if (typeof vector[key] === 'object') {
+ vector[key] = vector[key].unitize();
+ } else if (typeof vector[key] === 'number') {
+ vector[key] = (vector[key] === 0 ? 0 : vector[key] / Math.abs(vector[key]));
+ }
+ });
+ return vector;
+} \ No newline at end of file