Smilebags / p5.dimensions.js

An addon for p5.js which adds support for higher dimensional calculations.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Not sure how this is going to work?

Smilebags opened this issue · comments

@limzykenneth
This repo is my working example (in the console) of the multidimensional distance and vectors, but I'm not sure of the best way to maintain this code without unneeded duplication of p5 core. Any advice?

You shouldn't really need to duplicate anything from p5 core, remember since this is a p5 addon, it would need be loaded after p5.js is loaded, which means you will have access to all of the p5.js methods as well if you need them. What do you think you will need for this?

I think the library should be relatively simple so you won't need a build tool. Unit tests are quite nice but not essential for now. You pretty much just need the addon library itself, you don't need to include the p5.js library in here. Some examples would be very useful for people trying to use this.

I had a look at max0410's PR over at p5.js's repo and that's implementing the 2n arguments per n dimension which for higher dimensions can be a bit hard to use for users especially those not familiar enough with javascript. See if you can get in touch with max0410 and maybe with Lauren as well, find out what would be the preferred way to go forward with this feature. Good job so far!

Thanks for the advice. I did head over to that issue and commented to let them know what I'm doing. I saw his alternate implementation and agree it might be easier to code but more cumbersome to use.

I'll tidy this code up and try to get a few working examples running at some point.

@limzykenneth, I have tidied up the code, no documentation yet but there is an example. I'm guessing such a small addon won't be featured on the p5js website, but what should be the next step to get this in a better state.

Hey, it's looking good I think. I think for the next step you can consider developing this into a more useful multidimensional vector library. You can include function for nDimensional cross and dot products etc. You can also consider including a version of nDist that doesn't do square roots as that will be faster and can be used in the case where simple comparison between distance is enough.

Also utility functions for adding and subtracting vectors would be useful as well. Normalize is also very useful. You get the idea, just let me know if you need more suggestion.

Thanks for the advice. I've worked on a bunch of them now and will continue to extend it in a similar manner as to mirror the p5.Vector functions in nD.

I believe this is almost ready and will get in touch with hello@p5js.org soon with a request to add it to the addons page.

Looking good! A few more things before you publish, you probably should not pollute the global namespace, specifically dimensionalSymbols, getVectorValues and generateMethods are in the global namespace and that means your users cannot be using those names in the global namespace as well or it will overwrite your function.

You can either have those as hidden properties in the p5 object or use other ways of not exposing those to the global namespace. If you need my help, I might be able to make a PR in a couple days time.

Ah I know what you mean. That didn't come across my mind. I think I know how to do what you're saying. If I was to add them to the p5 prototype like the other functions would that solve the problem?

Better not add them to the prototype. Adding them to the prototype means exposing them to the end user. A better way is to have the whole thing in a self executing function block.

;(function(){
  // Your code here
})();

Have a test, I'm not sure if it will work directly, if not then you may need to do something like

;(function(p5){
  // Your code here
})(p5);

Okay, I have addressed this issue in ae75878 so the addon it's self should be ready. I'll work on adding some documentation.

I think it is ready as of cdbf8ef!

Great! I'll see if I can help you test them in the next few days but if you are ready, go ahead and submit it.

Down the track I would like to implement unit tests to make sure every function is rock solid but I think it is ready for a beta release. (Since there might be something I missed which will change functionality)

Had a quick look and everything looks good. Just going to nit pick a few tiny things, they are not essential or high priority but I think they would help improve the code quality.

  1. The old to semicolons or not to semicolons, it's better to always use it or not at all. Mixing the two can be confusing and it's freaking my code linter out. 😝 You can checkout how p5.js repo uses .jshintrc and .editorconfig to control code styles.
  2. On this line , it's more efficient to use object literal, ie
var obj = {};

instead. Same goes for line 66 as well.
3. Consider providing a minified version, but given the small size of your library, it probably isn't that important.

That's all for now. Overall everything are clear and simple which is awesome!

Thanks for the advice, I'll look into how to get the semicolons more consistent - the formatter in VS Code doesn't have any configuration options that I'm aware of, but it is something I've wanted to work out.

I'll make your recommended changes regarding object creation, I wasn't aware of the performance gain.

For now I'm not going to offer a minified version because the performance difference would be negligible, but will consider it if the library gets bigger.

Thanks again.

@max0410 could you apply
var obj = {};
syntax wherever we are creating a new object?

You mean like:
var outputVector = new nVector(coords)?

So where there is currently the words new Object it should just be the object literal {} because it is meant to run faster on some engines. For example, line 54 is var obj = new Object(); but should be var obj = {};

I didn't know of this difference until @limzykenneth told me, but could be good to implement. What do you think @max0410?

Well, it saves us 4750 ms. So I don't see why not. If someone happened to be using this for big calculations that might save them a few hours!

Indeed! Thanks for applying the fix.

@max0410 wow, I knew it has performance differences but I'm not expecting that much! 😮 Good thing it's fixed then.