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

Conversation

Forchapeatl
Copy link
Contributor

@Forchapeatl Forchapeatl commented Sep 1, 2024

Resolves #6750

Changes:

Screenshots of the change:

PR Checklist

@Forchapeatl Forchapeatl changed the title Added example on ClampToZero method. Added example and Unit tests for ClampToZero method on p5.Vector. Sep 1, 2024
@Forchapeatl Forchapeatl marked this pull request as ready for review September 1, 2024 04:19
@@ -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

* <div">
* <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

* <code>
* function setup() {
* // Create a new vector
* 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

* // 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

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

@Forchapeatl
Copy link
Contributor Author

Than you for the review @ashish1729 . I have made the updated changes, please have a look at my PR.

*
* 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

@ashish1729
Copy link
Contributor

thank you for fixing all these, although I am not a maintainer, I don't think I can/should approve changes.

@Forchapeatl
Copy link
Contributor Author

No worries @ashish1729 , I really needed someone to put me on track. Thank you.

Comment on lines +3911 to +3915
* console.log('Before:', v.x , v.y);
*
* // Clamp negligible value of x-component to zero
* v.clampToZero();
* console.log('After:', v.x , v.y);
Copy link
Contributor

Choose a reason for hiding this comment

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

If you run grunt yui:dev, the user won't be able to see the log values on the reference page. I think they have to open their browser developer tools to see that.

Comment on lines +1896 to +1901
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);
});
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.

Comment on lines +1903 to +1908
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);
});
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 ;)

@perminder-17
Copy link
Contributor

Keep up the great work. Some thoughts, let me know what you feel. :)

Here how it shows when you run grunt yui:dev in your code-editor, you need to open the developer tools in your browser to see the log values.

image

This could be resolved by two solution.

  1. If you write the code just as a function, for ex:
```js
your sketch....```

and by writing some description for eg;
Here's the after result (maybe in your words):

```js
before : values
after : values```
  1. The second option is to use text() function and can write the log values into the canvas by making a canvas of 100X100.

Let me know what you think by these thoughts :)

@Forchapeatl
Copy link
Contributor Author

thanks @perminder-17 , I will work on this over the weekend

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add missing examples.
3 participants