-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
[p5.js 2.0] Vector nD getter and setter, added values. #7277
base: dev-2.0
Are you sure you want to change the base?
Conversation
this._values = newValues.slice() | ||
} | ||
|
||
get x() { |
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.
this allows us to still consume the vector x, y and z without having an explicit class attributes with that name, this making it more flexible
@limzykenneth
get y() { | ||
return this._values[1] || 0 | ||
} | ||
get z() { |
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.
should we add a w
value for 4D? or for nD do users will just search values by number?
@limzykenneth
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.
I'm ok adding a w personally, since math for 3D stuff uses 4D vectors/matrices quite often. it seems fairly common in other apis too, e.g. native js DOMPoint. And then anything above 4D would use indices
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.
Yeah, adding a w
is fine for me as well. When working on the documentation we need to balance this convention of using w
and it potentially being unintuitive for those very new to linear algebra, possibly by de-emphaiszing w
in some way or other solution.
return this._values[0] || 0 | ||
} | ||
|
||
setValue(index, value) { |
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.
probably I need to create also a get value(index)
so users can get entries of the vector by ID
@limzykenneth
src/math/p5.Vector.js
Outdated
@@ -158,7 +207,7 @@ function vector(p5, fn) { | |||
* </div> | |||
*/ | |||
toString() { | |||
return `p5.Vector Object : [${this.x}, ${this.y}, ${this.z}]`; | |||
return `p5.Vector Object : [${this.values.join(', ')}]`; |
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.
One of the things I'm considering is to have this be serialized into something more standard/machine readable, so maybe just the JSON part at the end.
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 you mean something like this?
toString() {
return `${this.values.join(', ')}]`;
}
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.
yeah perhaps, might be worth considering how other libraries serialize their vector objects if they do at all
I have added new inline documentation for the new methods. Here I share the analysis of the pending methods to update and some questions/challenges I'm facing: Vector methods
Static methods
|
For the inline documentation, it is not great at inferring the right name and parent class/module with the code only so we may need to add additional hints around these, you can have a look at the other already documented entries and follow their convention. We can revisit the documentation at a later date though so no need to solve this immediately. For For
|
Sounds good, will do my best now and will revisit the docs later
Actually this is my exact question that I'm facing with operations that have multiple dimensions like div and mult, I have added some comments to the PR, I believe that dimension needs to change also in the setters when the values are changed, increased or decrease in components
Understood, thanks
Sounds right, I will indicate explicitly what functions will stay in fixed dimentions, I will think also of possible generalisations for nD for the defined axis |
This PR is ready for review. |
I'd like to test the documentation generation first to see how it works, will get to it when I have a bit more time, you can go ahead with next steps branching off of this branch. |
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.
This is looking great! I just flagged some bits that might be inconsistent in case it's unintentional.
} | ||
if (dimensions === 0) { | ||
this.dimensions = 2; | ||
this._values = [0, 0, 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.
Should the dimensions here match the number of items in _values
?
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.
One thing I'm just now thinking about, should the default vector when no argument is passed be 2D instead of 3D? Seeing as most people start learning to create sketches with 2D before moving into 3D.
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.
I think that makes sense. Even in webgl mode, you may very well still be using 2D vectors for a number of things, so its not a bad idea to have to be explicit to make 3D vectors
[p5.js 2.0] Matrix move to math folder
…nt of p5 namespace
I have added my proposal for the adapter for toggling between All existing tests pass with both versions of the matrix, please let me know your thoughts @limzykenneth @davepagurek. |
Since we might have two bundles of |
Resolves ##6765
Changes:
Modify Vector class to include a
values
list to make it n-dimensional while mantaining the existing API ofx
,y
,z
by setting up accessors, getter and setter.Pending Update documentation and add new Unit tests
Screenshots of the change:
PR Checklist
npm run lint
passesCC: @limzykenneth @davepagurek @Qianqianye