Skip to content

Add static versions of existing mutable vector functions #5044

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

Merged
merged 20 commits into from
Dec 16, 2022

Conversation

weslord
Copy link
Contributor

@weslord weslord commented Feb 14, 2021

Resolves #4852

Changes:

Updated functions, docs and unit tests for:

PR Checklist

@welcome
Copy link

welcome bot commented Feb 14, 2021

🎉 Thanks for opening this pull request! Please check out our contributing guidelines if you haven't already. And be sure to add yourself to the list of contributors on the readme page!

@weslord
Copy link
Contributor Author

weslord commented Feb 14, 2021

An ongoing branch for development of the static functions and supporting materials.

The existing test suites for p5.Vector take dramatically different approaches from one test to another (eg: assert vs expect, calling createVector vs overwriting v.x, v.y & v.z), so it is unclear what convention I should be following. Since I will be editing a lot of these tests anyways in the course of this PR, I'll do my best to get them to a more uniform standard. I've used my best judgment (for example, trying to remove dependencies on values initialized in previous suites), but I have very little experience writing tests for JS, so I would appreciate any guidance or feedback in that particular area.

@limzykenneth
Copy link
Member

For unit tests you can start by looking at here and if there's anything missing or unclear (whether to use assert or expect for example) I think an issue can be filed to either edit the contributor docs or refactor the test code.

@codecov-commenter
Copy link

Codecov Report

Merging #5044 (c8527ee) into main (bb4ba44) will increase coverage by 0.47%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #5044      +/-   ##
==========================================
+ Coverage   68.09%   68.57%   +0.47%     
==========================================
  Files          79       79              
  Lines       12286    12327      +41     
  Branches     2720     2733      +13     
==========================================
+ Hits         8366     8453      +87     
+ Misses       3920     3874      -46     
Impacted Files Coverage Δ
src/math/p5.Vector.js 84.07% <100.00%> (+1.04%) ⬆️
src/core/main.js 78.81% <0.00%> (-8.43%) ⬇️
src/core/transform.js 59.67% <0.00%> (-3.04%) ⬇️
src/webgl/p5.RendererGL.Immediate.js 51.53% <0.00%> (-2.37%) ⬇️
src/core/shape/vertex.js 63.35% <0.00%> (-1.73%) ⬇️
src/webgl/light.js 88.46% <0.00%> (-1.39%) ⬇️
src/image/loading_displaying.js 72.72% <0.00%> (-1.25%) ⬇️
src/webgl/p5.RendererGL.Retained.js 68.42% <0.00%> (-0.55%) ⬇️
src/image/filters.js 19.17% <0.00%> (-0.52%) ⬇️
src/dom/dom.js 60.21% <0.00%> (ø)
... and 19 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update bb4ba44...c8527ee. Read the comment docs.

@weslord weslord marked this pull request as ready for review July 9, 2021 22:38
@weslord
Copy link
Contributor Author

weslord commented Jul 9, 2021

This is a bit of a looong one, but I've done my best to keep each individual commit mostly clean and self-contained, so it may be easier to review this commit-by-commit.

This should implement class/static methods for all outstanding Vector functions, except for .toString(). Since Function.prototype.toString() already, overriding it on p5.Vector has some potential side-effects. I've put those changes in #5348 so they can be discussed separately.

@Qianqianye
Copy link
Contributor

Thank you @weslord. I am inviting more Math stewards to review this PR @limzykenneth, @jeffawang, @AdilRabbani @davepagurek.

davepagurek
davepagurek previously approved these changes Dec 14, 2022
Copy link
Contributor

@davepagurek davepagurek left a comment

Choose a reason for hiding this comment

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

Just going through and reading everything! Overall this looks good, just a few comments!

* Calculate the angle of rotation for this vector (only 2D vectors).
* p5.Vectors created using <a href="#/p5/createVector">createVector()</a>
* will take the current <a href="#/p5/angleMode">angleMode</a> into
* consideration, and give the angle in radians or degree accordingly.
Copy link
Contributor

Choose a reason for hiding this comment

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

Tiny nitpick, for here and line 2466: this should probably be "degrees" instead of "degree"

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, I see this is actually a typo in the existing docs for the instance methods too 😅 Maybe worth fixing there too, although it's not urgent for this PR

expect(v3.angleBetween(v4)).to.be.closeTo(180, 0.00001);
});
test('should not trip on rounding issues in 2D space', function() {
v1 = new p5.Vector(-11, -20);
Copy link
Contributor

Choose a reason for hiding this comment

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

Was there a reason to switch to new p5.Vector() here over myp5.createVector()?

Copy link
Member

Choose a reason for hiding this comment

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

Since this is a bit of an old PR I would suggest for us to just test changing these back to using myp5.createVector() and see if it is working. If that works fine, we can revert this locally then merge this PR so as not to hold things up for much longer.

Copy link
Contributor Author

@weslord weslord Dec 16, 2022

Choose a reason for hiding this comment

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

Half the file already used new p5.Vector(), and half used myp5.createVector() (one of the many inconsistencies that inspired this query: #5044 (comment)).

My preference is new p5.Vector() because it seems more pure and less stateful. If myp5 has some influence, we probably don't want that. If it doesn't, why use it? Right?

But ultimately it's not a strong preference. I've checked "allow edits by maintainers". Make whatever changes you want.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't have a strong opinion either way either, I just wanted to make sure there wasn't anything that wasn't working when using createVector. I think since the docs mostly describe using createVector, it's maybe best that we make sure we test that primarily, although it's also good to know that new p5.Vector works too!

Thanks for making the other updates, I'll pull your branch and double check that createVector works.

Copy link
Contributor

Choose a reason for hiding this comment

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

Just confirmed that createVector works locally! I'll merge your PR as-is though, you're right that the tests are currently very inconsistent with what form it uses, but it does seem to use new p5.Vector slightly more in the tests, so this adds a bit more consistency (and we can always discuss standardizing on something else in another issue.)

@davepagurek davepagurek dismissed their stale review December 14, 2022 13:17

Accidentally hit the wrong button!

v1 = new p5.Vector(1, 2, 3);
});

suite('p5.Vector.prototype.magSq() [INSTANCE]', function() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for making tests for these methods too!

Copy link
Contributor

@davepagurek davepagurek left a comment

Choose a reason for hiding this comment

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

Thanks again for making these changes!

@davepagurek davepagurek merged commit f825b7a into processing:main Dec 16, 2022
@davepagurek
Copy link
Contributor

@all-contributors please add @weslord for doc, test

@allcontributors
Copy link
Contributor

@davepagurek

I've put up a pull request to add @weslord! 🎉

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 static versions of vector functions
5 participants