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

Implementation of new functions setRandom2D() and setRandom3D() in p5.Vector #6145

Closed
wants to merge 11 commits into from
Closed

Implementation of new functions setRandom2D() and setRandom3D() in p5.Vector #6145

wants to merge 11 commits into from

Conversation

inaridarkfox4231
Copy link
Contributor

@inaridarkfox4231 inaridarkfox4231 commented May 17, 2023

Among the functions that obtain random values, functions such as shuffle() and randomGaussian() are affected by randomSeed() because they use p5.js random() internally.

However, p5.Vector's random2D and random3D functions are static, so p5.js's random() cannot be used and the seed value cannot be fixed.

Therefore, I thought it would be nice to have a function that can obtain such a random unit vector with a fixed seed value, so I decided to propose an implementation.

Resolves #6141

Changes:

Implements functions that operate on vectors and set their components to a 2D or 3D unit vector. A unit vector is chosen randomly. In the 2D case, only objects with a z component of 0 are selected.

Pass a reference to random() in p5.js when the p5.Vector is created with createVector(). As a result, when applying this function to a vector prepared by createVector(), the p5.js function random() is used. For example, it will be affected by randomSeed().

example

for setRandom3D():

  randomSeed(999);
  const v1 = new p5.Vector().setRandom3D();
  const v2 = createVector().setRandom3D();
  randomSeed(999);
  const v3 = new p5.Vector().setRandom3D();
  const v4 = createVector().setRandom3D();

  console.log(v1.mag()); // 1.
  console.log(v1.x === v3.x && v1.y === v3.y && v1.z === v3.z); // almost always false.
  console.log(v2.x === v4.x && v2.y === v4.y && v2.z === v4.z); // always true.

PR Checklist

Pass a reference to random() when creating a vector with createVector().
Rewrite the constructor so that it registers a reference to p5.js random() when created with createVector().
Rewrite copy() accordingly. cross() also needs to be rewritten, but the second half of the process overlaps with copy(). So I'll leave that to copy().
Implement setRandom2D(), setRandom3D().
no arguments, Chainable.
remove trailing spaces
Minor fixes such as semicolons.
Fix TWO_PI to constants.TWO_PI.
Wrong call to fromAngle() on Random2D. The function called this.fromAngle when it should have called p5.Vector.fromAngle().

However, rather than correcting this, it would be easier to write using a newly created function and rewrite it accordingly, so I fixed it that way.
@inaridarkfox4231
Copy link
Contributor Author

I thought it was a bug, but I was wrong...

That aside, I modified random2D() and random3D() because it's easier to write using the newly created functions.

Since vectors created with createVector() and vectors created with new p5.Vector() have slightly different properties, I thought it would be better to clarify that.
@inaridarkfox4231
Copy link
Contributor Author

Since vectors created with createVector() and vectors created with new p5.Vector() have slightly different properties, I thought it would be better to clarify that.

Fixed some notation for spacing.
@inaridarkfox4231
Copy link
Contributor Author

As for the example, I would like to replace it with a simpler one that just confirms the effect of randomSeed().
Also, replace the sentence I added at createVector() with something simpler.
As for the annotation itself, I think it makes sense.
Because the difference between these vectors is not mentioned anywhere in the reference.

I have simplified my comments by narrowing down the topic.
Simplified examples of setRandom2D() and setRandom3D().
@inaridarkfox4231
Copy link
Contributor Author

It may be better to increase the parameters so that the range can be specified.

@inaridarkfox4231 inaridarkfox4231 deleted the setRandom2D3D branch May 22, 2023 08:29
@inaridarkfox4231 inaridarkfox4231 restored the setRandom2D3D branch May 22, 2023 11:46
@inaridarkfox4231
Copy link
Contributor Author

Since the issue is open, I think I'll open it as well.

@inaridarkfox4231
Copy link
Contributor Author

The reason is as written in the issue. I would like to close this pull request.

@inaridarkfox4231 inaridarkfox4231 deleted the setRandom2D3D branch May 22, 2023 14:25
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.

Regarding the p5.Vector functions random2D() and random3D() using Math.random()
1 participant