From 0eba3074ad9e1ea7536df6f6ab13265a68912f86 Mon Sep 17 00:00:00 2001 From: FORCHA PEARL Date: Sun, 1 Sep 2024 04:51:52 +0100 Subject: [PATCH 1/8] Added example on ClampToZero method. --- src/math/p5.Vector.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/math/p5.Vector.js b/src/math/p5.Vector.js index 0b48490258..dd430c8fb0 100644 --- a/src/math/p5.Vector.js +++ b/src/math/p5.Vector.js @@ -3901,6 +3901,30 @@ p5.Vector = class { * @method clampToZero * @return {p5.Vector} with components very close to zero replaced with zero. * @chainable + * @example + * + * + * function setup() { + * // Create a p5.Vector object. + * let v = p5.Vector.random2D(); + * + * // Prints "p5.Vector Object : [x, y, 0]" to the console + * // where x and y are small random numbers. + * print(v.toString()); + * } + * function setup() { + * // Create a new vector + * let v = createVector(0.0000000000000002220446049250313, 5 ); + * + * console.log('Before:', v.x , v.y); + * + * // Clamp components close to zero to zero + * v.clampToZero(); + * console.log('After:', v.x , v.y); + * describe('Round down very small numbers to zero'); + * } + * + * */ clampToZero() { this.x = this._clampToZero(this.x); From 7bfb747c658e5a28ced4432f9ce14baff45cb0cd Mon Sep 17 00:00:00 2001 From: FORCHA PEARL Date: Sun, 1 Sep 2024 05:00:56 +0100 Subject: [PATCH 2/8] removed duplicate function --- src/math/p5.Vector.js | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/math/p5.Vector.js b/src/math/p5.Vector.js index dd430c8fb0..5b0fe8d5f3 100644 --- a/src/math/p5.Vector.js +++ b/src/math/p5.Vector.js @@ -3905,14 +3905,6 @@ p5.Vector = class { * * * function setup() { - * // Create a p5.Vector object. - * let v = p5.Vector.random2D(); - * - * // Prints "p5.Vector Object : [x, y, 0]" to the console - * // where x and y are small random numbers. - * print(v.toString()); - * } - * function setup() { * // Create a new vector * let v = createVector(0.0000000000000002220446049250313, 5 ); * From 70bd6aca90036d1390d4f44e042e4a16bda6bf29 Mon Sep 17 00:00:00 2001 From: FORCHA PEARL Date: Sun, 1 Sep 2024 05:01:52 +0100 Subject: [PATCH 3/8] Added unit test on P5.vector ClampToZero --- test/unit/math/p5.Vector.js | 40 +++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/test/unit/math/p5.Vector.js b/test/unit/math/p5.Vector.js index ef3570ee0b..73a516dbfb 100644 --- a/test/unit/math/p5.Vector.js +++ b/test/unit/math/p5.Vector.js @@ -1882,5 +1882,45 @@ suite('p5.Vector', function() { expect(p5.Vector.equals(a1, a2)).to.be.true; }); }); + + suite('p5.Vector.clampToZero()', function() { + let v; + + test('should clamp very small positive number to zero', function() { + v = new p5.Vector(0.0000000000000002220446049250313, 5); + v.clampToZero(); + expect(v.x).to.equal(0); + expect(v.y).to.equal(5); + }); + + test('should clamp very small negative number to zero', function() { + v = new p5.Vector(-0.0000000000000002220446049250313, 5); + v.clampToZero(); + expect(v.x).to.equal(0); + expect(v.y).to.equal(5); + }); + + test('should not clamp regular numbers', function() { + v = new p5.Vector(0.01, 5); + v.clampToZero(); + expect(v.x).to.equal(0.01); + expect(v.y).to.equal(5); + }); + + test('should leave zero components unchanged', function() { + v = new p5.Vector(0, 0); + v.clampToZero(); + expect(v.x).to.equal(0); + expect(v.y).to.equal(0); + }); + + test('should clamp very small numbers in all components of a 3D vector to zero', function() { + v = new p5.Vector(0.0000000000000002220446049250313, -0.0000000000000002220446049250313, 0.0000000000000002220446049250313); + v.clampToZero(); + expect(v.x).to.equal(0); + expect(v.y).to.equal(0); + expect(v.z).to.equal(0); + }); + }); }); }); From aa57c05f544c41f780a276e82d3eec609d2fb7bb Mon Sep 17 00:00:00 2001 From: FORCHA PEARL Date: Sun, 1 Sep 2024 05:05:59 +0100 Subject: [PATCH 4/8] Update p5.Vector.js --- test/unit/math/p5.Vector.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/unit/math/p5.Vector.js b/test/unit/math/p5.Vector.js index 73a516dbfb..2a8e9e5c11 100644 --- a/test/unit/math/p5.Vector.js +++ b/test/unit/math/p5.Vector.js @@ -1915,7 +1915,8 @@ suite('p5.Vector', function() { }); test('should clamp very small numbers in all components of a 3D vector to zero', function() { - v = new p5.Vector(0.0000000000000002220446049250313, -0.0000000000000002220446049250313, 0.0000000000000002220446049250313); + v = new p5.Vector(0.0000000000000002220446049250313, + -0.0000000000000002220446049250313, 0.0000000000000002220446049250313); v.clampToZero(); expect(v.x).to.equal(0); expect(v.y).to.equal(0); From 9a5737813e98c518652c68940b6ba9a5385c63df Mon Sep 17 00:00:00 2001 From: FORCHA PEARL Date: Sun, 1 Sep 2024 05:14:42 +0100 Subject: [PATCH 5/8] fixed lint --- test/unit/math/p5.Vector.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/unit/math/p5.Vector.js b/test/unit/math/p5.Vector.js index 2a8e9e5c11..703d984b18 100644 --- a/test/unit/math/p5.Vector.js +++ b/test/unit/math/p5.Vector.js @@ -1915,8 +1915,10 @@ suite('p5.Vector', function() { }); test('should clamp very small numbers in all components of a 3D vector to zero', function() { - v = new p5.Vector(0.0000000000000002220446049250313, - -0.0000000000000002220446049250313, 0.0000000000000002220446049250313); + v = new p5.Vector( + 0.0000000000000002220446049250313, + -0.0000000000000002220446049250313, + 0.0000000000000002220446049250313); v.clampToZero(); expect(v.x).to.equal(0); expect(v.y).to.equal(0); From 52b0c633770afe682b6e753d62e7856c07aa9dee Mon Sep 17 00:00:00 2001 From: FORCHA PEARL Date: Thu, 5 Sep 2024 07:12:48 +0100 Subject: [PATCH 6/8] Adjusted wordings --- test/unit/math/p5.Vector.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/unit/math/p5.Vector.js b/test/unit/math/p5.Vector.js index 703d984b18..85dc8d8034 100644 --- a/test/unit/math/p5.Vector.js +++ b/test/unit/math/p5.Vector.js @@ -1886,28 +1886,28 @@ suite('p5.Vector', function() { suite('p5.Vector.clampToZero()', function() { let v; - test('should clamp very small positive number to zero', function() { - v = new p5.Vector(0.0000000000000002220446049250313, 5); + test('should clamp very small positive number of vector components to zero', function() { + v = new p5.Vector(0.0000000000000002, 5); v.clampToZero(); expect(v.x).to.equal(0); expect(v.y).to.equal(5); }); - test('should clamp very small negative number to zero', function() { - v = new p5.Vector(-0.0000000000000002220446049250313, 5); + test('should clamp very small negative number of vector components to zero', function() { + v = new p5.Vector(-0.0000000000000002, 5); v.clampToZero(); expect(v.x).to.equal(0); expect(v.y).to.equal(5); }); - test('should not clamp regular numbers', function() { + test('should not clamp regular numbers of vector components', function() { v = new p5.Vector(0.01, 5); v.clampToZero(); expect(v.x).to.equal(0.01); expect(v.y).to.equal(5); }); - test('should leave zero components unchanged', function() { + test('should leave zero components of a 2D vector unchanged', function() { v = new p5.Vector(0, 0); v.clampToZero(); expect(v.x).to.equal(0); @@ -1916,7 +1916,7 @@ suite('p5.Vector', function() { test('should clamp very small numbers in all components of a 3D vector to zero', function() { v = new p5.Vector( - 0.0000000000000002220446049250313, + 0.00000000000000005, -0.0000000000000002220446049250313, 0.0000000000000002220446049250313); v.clampToZero(); From b64675fa95168f1e41854642d7e315df4d8a7cc2 Mon Sep 17 00:00:00 2001 From: FORCHA PEARL Date: Thu, 5 Sep 2024 09:24:46 +0100 Subject: [PATCH 7/8] updated comments and fixed example error --- src/math/p5.Vector.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/math/p5.Vector.js b/src/math/p5.Vector.js index 5b0fe8d5f3..c8ef3a890e 100644 --- a/src/math/p5.Vector.js +++ b/src/math/p5.Vector.js @@ -3902,18 +3902,18 @@ p5.Vector = class { * @return {p5.Vector} with components very close to zero replaced with zero. * @chainable * @example - * + *
* * function setup() { - * // Create a new vector - * let v = createVector(0.0000000000000002220446049250313, 5 ); + * // Create a 2D vector where the x-component is near-zero + * let v = createVector(0.00000000000000005, 5 ); * * console.log('Before:', v.x , v.y); * - * // Clamp components close to zero to zero + * // Clamp negigabe value of x-component to zero * v.clampToZero(); * console.log('After:', v.x , v.y); - * describe('Round down very small numbers to zero'); + * describe('Round down very small numbers of vector components to zero'); * } * *
From ad63298cf7ea27b80f55be8bb13740f0e60316dd Mon Sep 17 00:00:00 2001 From: FORCHA PEARL Date: Thu, 5 Sep 2024 09:40:22 +0100 Subject: [PATCH 8/8] fixed volgabulary --- src/math/p5.Vector.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/math/p5.Vector.js b/src/math/p5.Vector.js index c8ef3a890e..f97bea902f 100644 --- a/src/math/p5.Vector.js +++ b/src/math/p5.Vector.js @@ -3910,7 +3910,7 @@ p5.Vector = class { * * console.log('Before:', v.x , v.y); * - * // Clamp negigabe value of x-component to zero + * // Clamp negligible value of x-component to zero * v.clampToZero(); * console.log('After:', v.x , v.y); * describe('Round down very small numbers of vector components to zero');