mirror of
https://github.com/nature-of-code/noc-book-2
synced 2024-11-16 07:47:48 +01:00
Notion - Update docs
This commit is contained in:
parent
8177aa9988
commit
453263812e
3 changed files with 16 additions and 21 deletions
|
@ -952,7 +952,6 @@ static add(v1, v2) {
|
|||
<p>When calling a static method, instead of referencing an object instance, you reference the name of the class. Here’s the right way to implement the vector addition example:</p>
|
||||
<pre class="codesplit" data-code-language="javascript">let v = createVector(0, 0);
|
||||
let u = createVector(4, 5);
|
||||
|
||||
<s>let w = v.add(u);</s>
|
||||
<strong>let w = p5.Vector.add(v, u);</strong></pre>
|
||||
<p>The <code>p5.Vector</code> class has static versions of <code>add()</code>, <code>sub()</code>, <code>mult()</code>, and <code>div()</code>. These static methods allow you to perform generic mathematical operations on vectors without changing the value of one of the input vectors in the process.</p>
|
||||
|
|
|
@ -515,22 +515,18 @@ function setup() {
|
|||
<p>Imagine it’s a Saturday morning. You’ve just gone out for a lovely jog, had a delicious bowl of cereal, and are sitting quietly at your computer with a cup of warm chamomile tea. It’s your old friend so-and-so’s birthday, and you’ve decided you’d like to make a greeting card with p5.js. How about simulating some confetti? Purple confetti, pink confetti, star-shaped confetti, square confetti, fast confetti, fluttery confetti—all kinds of confetti, all with different appearances and different behaviors, exploding onto the screen all at once.</p>
|
||||
<p>What you have is clearly a particle system: a collection of individual pieces (particles) of confetti. You might be able to cleverly redesign the <code>Particle</code> class to have variables that store color, shape, behavior, and more. To create a variety of particles, you might initialize those variables with random values. But what if some of your particles are drastically different? It could become very messy to have all sorts of code for different ways of being a particle in the same class. Another option might be to do the following:</p>
|
||||
<pre class="codesplit" data-code-language="javascript">class HappyConfetti {
|
||||
|
||||
}
|
||||
|
||||
class FunConfetti {
|
||||
|
||||
}
|
||||
|
||||
class WackyConfetti {
|
||||
|
||||
}</pre>
|
||||
<p>This is a nice solution: create three classes to describe the different kinds of confetti that are part of your particle system. The <code>Emitter</code> constructor could then have some code to pick randomly from the three classes when filling the array (note that this probabilistic method is the same one I employed in the random walk examples in Chapter 0):</p>
|
||||
<div class="snip-below">
|
||||
<pre class="codesplit" data-code-language="javascript">class Emitter {
|
||||
constructor(num) {
|
||||
this.particles = [];
|
||||
|
||||
for (let i = 0; i < num; i++) {
|
||||
let r = random(1);
|
||||
// Randomly pick a kind of particle.
|
||||
|
|
Loading…
Reference in a new issue