-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
base: main
Are you sure you want to change the base?
Changes from 5 commits
0eba307
7bfb747
70bd6ac
aa57c05
9a57378
52b0c63
b64675f
ad63298
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 new vector | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
}); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
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