From 053b88314a04adc3145332418f2b35ed232b973f Mon Sep 17 00:00:00 2001 From: ShraddhaBhojane <55998820+ShraddhaBhojane@users.noreply.github.com> Date: Mon, 16 Dec 2019 15:43:13 +0100 Subject: [PATCH 1/5] added snippet VectorAngle --- snippets/vectorAngle.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 snippets/vectorAngle.md diff --git a/snippets/vectorAngle.md b/snippets/vectorAngle.md new file mode 100644 index 000000000..119c77e30 --- /dev/null +++ b/snippets/vectorAngle.md @@ -0,0 +1,36 @@ +--- +title: vectorAngle +tags: math,intermediate +--- + +Returns the angle (theta) value between two vectors. + +Use `Math.trunc()` to pick the integer value of length. Use `slice()` slice the incoming array into individual vectors. +Use `forEach()`, `Math.pow()` and `Math.sqrt()` to iterate over the vectors and calculate the magnitude of each vector and the scalar product. +Use `Math.acos()` to calculate arccos and get the theta. + + +```js +const theta = ((...x) =>{ + let half = Math.trunc(x.length / 2); + let a = x.slice(0 , half); + let b = x.slice(half); + + let magnitudeOfA = 0; + a.forEach((a) => magnitudeOfA += Math.pow(a , 2)); + magnitudeOfA = Math.sqrt(magnitudeOfA); + + let magnitudeOfB = 0; + b.forEach((b) => magnitudeOfB += Math.pow(b , 2)); + magnitudeOfB = Math.sqrt(magnitudeOfB); + + let scalar = 0; + a.forEach((a , i) => scalar += a * b[i]); + + return Math.acos(scalar / (magnitudeOfA*magnitudeOfB)); +}); +``` + +```js +theta(3,4,4,3); // 0.283794109208328 +``` \ No newline at end of file From 841f36044fd647f8cf934bd4fa35134252ac590c Mon Sep 17 00:00:00 2001 From: ShraddhaBhojane <55998820+ShraddhaBhojane@users.noreply.github.com> Date: Sun, 22 Dec 2019 16:36:29 +0100 Subject: [PATCH 2/5] Update snippets/vectorAngle.md Co-Authored-By: Angelos Chalaris --- snippets/vectorAngle.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snippets/vectorAngle.md b/snippets/vectorAngle.md index 119c77e30..64567066b 100644 --- a/snippets/vectorAngle.md +++ b/snippets/vectorAngle.md @@ -1,6 +1,6 @@ --- title: vectorAngle -tags: math,intermediate +tags: math,beginner --- Returns the angle (theta) value between two vectors. @@ -33,4 +33,4 @@ const theta = ((...x) =>{ ```js theta(3,4,4,3); // 0.283794109208328 -``` \ No newline at end of file +``` From 00ad575adbeda0ad190369304e52a0c1b377f4c1 Mon Sep 17 00:00:00 2001 From: ShraddhaBhojane <55998820+ShraddhaBhojane@users.noreply.github.com> Date: Sun, 22 Dec 2019 17:26:01 +0100 Subject: [PATCH 3/5] changes made --- snippets/vectorAngle.md | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/snippets/vectorAngle.md b/snippets/vectorAngle.md index 119c77e30..f686f4a57 100644 --- a/snippets/vectorAngle.md +++ b/snippets/vectorAngle.md @@ -1,36 +1,27 @@ --- title: vectorAngle -tags: math,intermediate +tags: math,beginner --- Returns the angle (theta) value between two vectors. -Use `Math.trunc()` to pick the integer value of length. Use `slice()` slice the incoming array into individual vectors. -Use `forEach()`, `Math.pow()` and `Math.sqrt()` to iterate over the vectors and calculate the magnitude of each vector and the scalar product. +Use `Math.trunc()` to convert the half of `length` to integer. +Use `Array.prototype.slice()` slice the incoming array into two vectors. +Use `Array.prototype.reduce()`, `Math.pow()` and `Math.sqrt()` to calculate the magnitude of each vector and the scalar product of the two vectors. Use `Math.acos()` to calculate arccos and get the theta. ```js -const theta = ((...x) =>{ +const vectorAngle = (...x) =>{ let half = Math.trunc(x.length / 2); - let a = x.slice(0 , half); - let b = x.slice(half); - - let magnitudeOfA = 0; - a.forEach((a) => magnitudeOfA += Math.pow(a , 2)); - magnitudeOfA = Math.sqrt(magnitudeOfA); - - let magnitudeOfB = 0; - b.forEach((b) => magnitudeOfB += Math.pow(b , 2)); - magnitudeOfB = Math.sqrt(magnitudeOfB); - - let scalar = 0; - a.forEach((a , i) => scalar += a * b[i]); - - return Math.acos(scalar / (magnitudeOfA*magnitudeOfB)); -}); + let [a, b] = [x.slice(0, half), x.slice(half)]; + let magnitudeOfA = Math.sqrt(a.reduce((total, num) => {return total + Math.pow(num, 2);}, 0)); + let magnitudeOfB = Math.sqrt(b.reduce((total, num) => {return total + Math.pow(num, 2);}, 0)); + let scalar = a.reduce((total, num, i)=>{return total + (num * b[i])}, 0); + return Math.acos(scalar / (magnitudeOfA * magnitudeOfB)); +}; ``` ```js -theta(3,4,4,3); // 0.283794109208328 +vectorAngle(3,4,4,3); // 0.283794109208328 ``` \ No newline at end of file From eaf471dec9c6daba906ca8f90c25632a68271c6e Mon Sep 17 00:00:00 2001 From: ShraddhaBhojane <55998820+ShraddhaBhojane@users.noreply.github.com> Date: Sun, 22 Dec 2019 17:59:26 +0100 Subject: [PATCH 4/5] changes to the snippet --- snippets/vectorAngle.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/vectorAngle.md b/snippets/vectorAngle.md index 4c9dc2000..ce218fde1 100644 --- a/snippets/vectorAngle.md +++ b/snippets/vectorAngle.md @@ -1,6 +1,6 @@ --- title: vectorAngle -tags: math,beginner +tags: math, beginner --- Returns the angle (theta) value between two vectors. From 275c5e61f5da9491d24bcf353c23799cbf6ca97e Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 16 Apr 2020 23:18:18 +0300 Subject: [PATCH 5/5] Update vectorAngle.md --- snippets/vectorAngle.md | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/snippets/vectorAngle.md b/snippets/vectorAngle.md index ce218fde1..336277285 100644 --- a/snippets/vectorAngle.md +++ b/snippets/vectorAngle.md @@ -1,28 +1,23 @@ --- title: vectorAngle -tags: math, beginner +tags: math,beginner --- -Returns the angle (theta) value between two vectors. +Returns the angle (theta) between two vectors. -Use `Math.trunc()` to convert the half of `length` to integer. -Use `Array.prototype.slice()` slice the incoming array into two vectors. Use `Array.prototype.reduce()`, `Math.pow()` and `Math.sqrt()` to calculate the magnitude of each vector and the scalar product of the two vectors. -Use `Math.acos()` to calculate arccos and get the theta. +Use `Math.acos()` to calculate the arccos and get the theta value. ```js -const vectorAngle = (...x) =>{ - let half = Math.trunc(x.length / 2); - let [a, b] = [x.slice(0, half), x.slice(half)]; - let magnitudeOfA = Math.sqrt(a.reduce((total, num) => {return total + Math.pow(num, 2);}, 0)); - let magnitudeOfB = Math.sqrt(b.reduce((total, num) => {return total + Math.pow(num, 2);}, 0)); - let scalar = a.reduce((total, num, i)=>{return total + (num * b[i])}, 0); - return Math.acos(scalar / (magnitudeOfA * magnitudeOfB)); +const vectorAngle = (x, y) => { + let mX = Math.sqrt(x.reduce((acc, n) => acc + Math.pow(n, 2), 0)); + let mY = Math.sqrt(y.reduce((acc, n) => acc + Math.pow(n, 2), 0)); + return Math.acos(x.reduce((acc, n, i) => acc + n * y[i], 0) / (mX * mY)); }; ``` ```js -vectorAngle(3,4,4,3); // 0.283794109208328 +vectorAngle([3, 4], [4, 3]); // 0.283794109208328 ```