-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Add jit function #7698
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
Comments
Interesting!Thanks @quinton-ashley for this thoughtful proposal. It raises a number of interesting issues.
This comment points to a problem with the current p5 API that I hadn't noticed before, and I think it points the way to some possible improvements too. I'll try to share some quick thoughts. I'm pretty curious to know what you think @ksen0, but I'll happily wait until we're on the other side of the 2.0 release :) Problems with the current p5.js API make it hard to extendAlthough adding a function like PredictabilityBased on the current behavior of
This is exactly what you indicated in your write-up. Unfortunately, this is not the case! Although this description of Syntax is an issue as well. Whereas ExtensibilityIt seems hard to add functions that generate points inside 2D and 3D cubes, say, since the natural names for those functions are already taken. If such features were to exist in p5, surely they'd be called Possible solution (involves breaking changes)When it comes to breaking changes, it's always good to proceed with caution. But, we might consider deprecating For example, p5 itself or a community-contributed add-on could then provide a more complete set of functions, if desired:
Note: This Unity tutorial, which is aimed at beginners, notes a lack of a convenient function for generating points in cubes (or cuboids). The tutorial also shows how a workaround can be explained to beginners. This suggests that built-in random-point-in-cuboid generators might be useful, but also that these aren't essential (except for the 1D case). Note: Here, I've proposed More discussion is required to determine whether the particular nine functions above would be good for p5. I'm mainly proposing that, as a more immediate priority, we discuss how (or whether) to address the existing inconsistencies. Next steps?The last feature request I looked at faced similar issues. In that case, implementing a new feature would propagate bugs caused by problems with the existing p5 API. When it comes to variations on After the release of 2.0, I wonder if it might make sense to open a discussion about what 3.0 might look like. Whereas 2.0 modernized p5.js and brought exciting new features, maybe 3.0 could focus on cleaning up problems that have accumulated in the interface. For example, that might involve resolving larger tensions between 2D and 3D rendering features (setting detail for 2D vs. 3D primitives, shapes vs. geometries, Focusing on existing features wouldn't necessarily preclude the addition of new features; it might just be a way to narrow the scope to features that make the existing API more consistent and intuitive. Examples might include random-point generation inside cubes in 2D and 3D, a modified I think the last point is worth repeating, since it seems to come up sometimes in discussions: although it's counterintuitive, adding functions can actually reduce complexity, making an API more economical from a user standpoint. If we were to provide a consistent feature set, such as the nine random generators outlined above (including a function such as To be extra clear, let's say we have a table of related features, and some of the features are missing. In that case, it'll tend to be harder to remember which features are available. Also, since patterns will be obfuscated, it might even make the individual features harder to remember. This is especially clear if we borrow an idea from information theory (specifically Kolmolgorov complexity): it's harder to remember the number Edits:
|
@GregStanton Thanks for the correction, clearly I never use Although, that's precisely why I suggested adding API similar to Unity's, that includes the name of the shape and where the random points will be generated.
Your suggested naming scheme doesn't make sense to me. The priority for naming these functions shouldn't be consistency or brevity, it should be descriptiveness. Unity already nailed it imo. A function like
Also, jitter is common vernacular right? "First day jitters", "do the jitterbug", "the camera is jittering", etc. I chose
|
@quinton-ashley I was just making edits to my previous comment when you replied. I indicated what the edits were at the bottom, in case you want to take a look. Regarding the "mistake" you made, my point was that it wasn't fundamentally your mistake. A seasoned programmer such as yourself will guess the most reasonable behavior based on a function name, and that's exactly what you did. The real mistake is in the current p5 API. Documentation doesn't fully resolve the issue if the natural interpretation conflicts with the actual behavior. I think the Unity names are helpful and might be better if we can adapt them to the context of p5. It might take a bit more thought to make that scheme work well within the context of p5, though. I'd like to think more about it, but I'm kind of slammed with work now. |
Another quick idea, before I forget (I haven't fully vetted it):
Notes:
It still seems to me that the first priority is to resolve the inconsistencies with |
Thanks for the thoughtful discussion both @GregStanton and @quinton-ashley made great points. |
@GregStanton I like the new approach with the randomPoint* functions, makes it clear that the functions return a point. I think perhaps it'd be more useful, and for consistency with the rect and box functions, to offer randomPointInEllipse and randomPointInSpheroid instead. Those functions would take a and b lengths, I think randomPointInRect should take a width and height, which is the default rectMode too, not corners. Again, randomDeviation is too long. I think anything longer than random, 6 characters, would make it no longer a shortcut and then its main purpose is lost. There's a precedent for |
Hi folks, this thread’s getting a bit dense—would love if we could keep things a bit more concise and understandable for newcomers to the topic. Maybe one way forward is trying these ideas out in an add-on library? If it catches on, it could be a good candidate for core later. Plus, starting separately gives more room to experiment with the API details and see what works best: which additional functions provide a lot of value but still keep the additions as a whole focused. |
@ksen0 Yeah, I should've put my footnote about adding other random math functionality like Unity's in a separate issue to keep the conversation focussed on There's no need for an addon library for this, it's 3 lines of code. function jit(v) {
return random(-v, v);
} I spent a long time writing this request in hopes that |
Increasing access
Improve writability and readability for generating random positions
Most appropriate sub-area of p5.js?
Feature request details
Proposal to add a function to p5.js that'd make it a bit easier for users to generate random positions.
In P2D mode, generating random positions anywhere within the bounds of the canvas, when no transformations are applied, is pretty simple.
But generating symmetric random values around a point, such as the transform origin (initially at the center of the canvas in WEBGL mode), requires subtraction (ex.
random(10) - 5
) or defining lower and upper bound params inrandom
(ex.random(-5, 5)
). With small numbers this is a non-issue, but when working with variables, you can see that the length of the equation or function call increases linearly.The length of the line could be cut down by defining variables like this:
If the user wanted to add a margin of 100 pixels from the canvas edges, where random points should not be placed, they could do so like this:
Here's another approach using the existing
p5.Vector
API:But I'd prefer if this effect was achievable inside a function like
circle
as a clean one liner.Previously I'd proposed
randomX
andrandomY
functions, with separate implementations for P2D and WEBGL mode to attain the same visual result of making a random point within the bounds of the canvas, when no transformations are applied.Margin of 100 pixels:
But to summarize the discussion in issue #7671, numerous problems with the proposal were found:
random
Now I also think it's strange in principle for math functions to have different implementations and return different values depending on which renderer is being used. So perhaps interoperability shouldn't be a goal here and seemingly can't be if greater flexibility is a priority.
I've thought up another function that simply generates symmetric random values. Yet, the name
randomSymmetric
is too long, it'd defeat the purpose. The short formrandSym
is also awful due to its similarity to the words "rancide" and "ransom".jit(amount)
What about the name
jit
?The full name
jitter
would be a fine too but ideally the shorter the better for this. No other words in English start with "jit" besides "jitney". For the seasoned programmers among us the acronym JIT Just-In-Time will come to mind but it'll be clear from how the function is used that that meaning doesn't apply.Jitter is uncontrolled deviation from a signal. I think it's a fitting metaphor for random variation around 0 or whatever the result of
jit
is being added to.EDIT: Jitter is also common vernacular: "First day jitters", "do the jitterbug", "the camera is jittering", etc.
Implementation
Examples
https://q5js.org/learn/#jit
Example of adding circles within a jitter range around the mouse position.
Creating a random x position in p5's WEBGL mode.
Summary
Though not as common as using
random
to generate values within a range with defined lower and upper bounds,jit
would be a very small but useful addition to p5.js that provides a more convenient way to fulfill a common need with broad applications. Thoughjit
is certainly something users could easily implement manually as a helper function in their own code, my guess is they probably wouldn't consider doing so (I never have either). Ifjit
was a p5 library function that becomes familiar to the community, using it could become a no-brainer though.jit
is one dimensional for simple use when specifying input params for other functions. EDIT: (mistake removed)However, functions for generating a random point within a unit circle or unit sphere could be worth adding to p5 as well.
https://docs.unity3d.com/6000.0/Documentation/Manual/class-random.html
Thoughts on this?
@davepagurek @ksen0 @GregStanton @VANSH3104
The text was updated successfully, but these errors were encountered: