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 5 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">
Copy link
Contributor

Choose a reason for hiding this comment

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

do we want div> instead of div"> .... in the second one we have extra quotation marks

* <code>
* function setup() {
* // Create a new vector
Copy link
Contributor

Choose a reason for hiding this comment

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

this comment can be made more useful, by saying that we are creating a vector with components having small numerical value

* let v = createVector(0.0000000000000002220446049250313, 5 );
Copy link
Contributor

Choose a reason for hiding this comment

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

why are we using such a detailed number can't we use 0.000....02

do we need all those digits ?

Copy link
Contributor

Choose a reason for hiding this comment

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

same value is used in tests as well,

it is ok if this value is kept, you can make that decision

*
* 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');
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this can be improved by saying that we round down the value of components of a vector,

you can use your own words, basically this function doesn't round down small number, this is for a vector, we can make that clearer

* }
* </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 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() {
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.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);
});
});
});
});
Loading