Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added example and Unit tests for ClampToZero method on p5.Vector. #7199

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/math/p5.Vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -3901,6 +3901,22 @@ p5.Vector = class {
* @method clampToZero
* @return {p5.Vector} with components very close to zero replaced with zero.
* @chainable
* @example
* <div>
* <code>
* function setup() {
* // 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 negigabe value of x-component to zero
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am sorry I don't know this word 'negigabe' , can you please check if this is something intentional

* v.clampToZero();
* console.log('After:', v.x , v.y);
* describe('Round down very small numbers of vector components to zero');
* }
* </code>
* </div>
*/
clampToZero() {
this.x = this._clampToZero(this.x);
Expand Down
43 changes: 43 additions & 0 deletions test/unit/math/p5.Vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -1882,5 +1882,48 @@ 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 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 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);
});
Comment on lines +1896 to +1901
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we use Math.abs((val || 0) - 0) <= Number.EPSILON ? 0 : val in our clampToZero function, so there's no need to check for negative values. It seems like we're testing whether Math.abs() works correctly rather than testing our p5.js 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);
});
Comment on lines +1903 to +1908
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here as well, it looks like we are checking for the same test and we are specifically testing for Number.EPSILON . What I think we can remove all the tests and can just keep a single big test as you have a test at last.

something with :

        v = new p5.Vector(
          0.00000000000000005,
          -0.0000000000000002220446049250313,
          0.0000000000000002220446049250313);

With these three values, we can create different combinations to verify if clampToZero() works correctly. Since unit tests can be time-consuming to run, this would be a kind of optimization.

Let me know what you think ;)


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);
expect(v.y).to.equal(0);
});

test('should clamp very small numbers in all components of a 3D vector to zero', function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like how you mentioned that we are clamping "components of a vector", I think it would be awesome if other tests followed the same wording

ie instead of saying that we are rounding down the number we can say rounding down the component of a vector

v = new p5.Vector(
0.00000000000000005,
-0.0000000000000002220446049250313,
0.0000000000000002220446049250313);
v.clampToZero();
expect(v.x).to.equal(0);
expect(v.y).to.equal(0);
expect(v.z).to.equal(0);
});
});
});
});
Loading