Skip to content

Commit e3b018c

Browse files
authored
Merge pull request #826 from nature-of-code/notion-update-docs
6-8
2 parents 5be29b5 + fad1d3a commit e3b018c

File tree

4 files changed

+4
-8
lines changed

4 files changed

+4
-8
lines changed

content/06_libraries.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,7 @@ <h2 id="collision-events">Collision Events</h2>
10051005
<p><strong>Step 3: Bodies, could you tell me which particles you’re associated with?</strong></p>
10061006
<p>Getting from the relevant Matter.js bodies to the <code>Particle</code> objects they’re associated with is a little harder. After all, Matter.js doesn’t know anything about my code. Sure, it’s doing all sorts of stuff to keep track of the relationships between bodies and constraints, but it’s up to me to manage my own objects and their associations with Matter.js elements. That said, every Matter.js body is instantiated with an empty object—<code>{ }</code>—called <code>plugin</code>, ready to store any custom data about that body. I can link the body to a custom object (in this case, a <code>Particle</code>) by storing a reference to that object in the <code>plugin</code> property.</p>
10071007
<p>Take a look at the updated constructor in the <code>Particle</code> class where the body is made. Note that the body-making procedure has been expanded by one line of code to add a <code>particle</code> property inside <code>plugin</code>. It’s important to make sure you’re adding a new property to the existing <code>plugin</code> object (in this case, <code>plugin.particle = this</code>) rather than overwriting the <code>plugin</code> object (for example, with <code>plugin = this</code>). The latter could interfere with other features or customizations.</p>
1008-
<div class="snip-below">
1008+
<div class="snip-below avoid-break">
10091009
<pre class="codesplit" data-code-language="javascript">class Particle {
10101010

10111011
constructor(x, y, radius) {

content/07_ca.html

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ <h2 id="the-game-of-life">The Game of Life</h2>
431431
<p>In 1970, Martin Gardner wrote a <em>Scientific American</em> article that documented mathematician John Conway’s new Game of Life, describing it as <em>recreational mathematics</em>: “To play life you must have a fairly large checkerboard and a plentiful supply of flat counters of two colors. It is possible to work with pencil and graph paper but it is much easier, particularly for beginners, to use counters and a board.”</p>
432432
<p>The Game of Life has become something of a computational cliché, as myriad projects display the game on LEDs, screens, projection surfaces, and so on. But practicing building the system with code is still valuable for a few reasons.</p>
433433
<p>For one, the Game of Life provides a good opportunity to practice skills with 2D arrays, nested loops, and more. Perhaps more important, however, this CA’s core principles are tied directly to a core goal of this book: simulating the natural world with code. The Game of Life algorithm and technical implementation will provide you with the inspiration and foundation to build simulations that exhibit the characteristics and behaviors of biological systems of reproduction.</p>
434-
<p>Unlike von Neumann, who created an extraordinarily complex system of states and rules, Conway wanted to achieve a similar lifelike result with the simplest set of rules possible. Gardner outlined Conway’s goals as follows:</p>
434+
<p>Unlike von Neumann, who created an extraordinarily complex system of states and rules, Conway wanted to achieve a similar lifelike result with the simplest set of rules possible. Gardner outlined Conway’s goals as follows.</p>
435435
<ol>
436436
<li><em>There should be no initial pattern for which there is a simple proof that the population can grow without limit.</em></li>
437437
<li><em>There should be initial patterns that apparently do grow without limit.</em></li>
@@ -571,7 +571,6 @@ <h3 id="the-implementation">The Implementation</h3>
571571
<p>Putting this all together:</p>
572572
<pre class="codesplit" data-code-language="javascript">// The next board
573573
let next = create2DArray(columns, rows);
574-
575574
//{!2} Loop but skip the edge cells.
576575
for (let i = 1; i &#x3C; columns - 1; i++) {
577576
for (let j = 1; j &#x3C; rows - 1; j++) {
@@ -585,15 +584,13 @@ <h3 id="the-implementation">The Implementation</h3>
585584
}
586585
// Correct by subtracting the cell state.
587586
neighborSum -= board[i][j];
588-
589587
//{!4} The rules of life!
590588
if (board[i][j] === 1 &#x26;&#x26; neighborSum &#x3C; 2) next[i][j] = 0;
591589
else if (board[i][j] === 1 &#x26;&#x26; neighborSum > 3) next[i][j] = 0;
592590
else if (board[i][j] === 0 &#x26;&#x26; neighborSum === 3) next[i][j] = 1;
593591
else next[i][j] = board[i][j];
594592
}
595593
}
596-
597594
board = next;</pre>
598595
<p>Now I just need to draw the board. I’ll draw a square for each spot: white for off, black for on.</p>
599596
<div data-type="example">

content/08_fractals.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,12 @@ <h3 id="implementing-recursive-functions">Implementing Recursive Functions</h3>
117117
return n * factorial(n - 1);
118118
}
119119
}</pre>
120+
<p>The <code>factorial()</code> function calls itself within its own definition. It may look a bit odd at first, but it works, as long as a stopping condition exists (in this case, <code>n &#x3C;= 1</code>) so the function doesn’t get stuck calling itself forever. (I’m using <code>&#x3C;=</code> instead of <code>===</code> as a safeguard against infinite recursion, but I should probably include additional error checking to manage noninteger or negative inputs to be more mathematically accurate.) Figure 8.7 illustrates the steps that unfold when <code>factorial(4)</code> is called.</p>
121+
<p>The function keeps calling itself, descending deeper and deeper down a rabbit hole of nested function calls until it reaches the stopping condition. Then it works its way up out of the hole, returning values until it arrives back home at the original call of <code>factorial(4)</code>.</p>
120122
<figure>
121123
<img src="images/08_fractals/08_fractals_9.png" alt="Figure 8.7: Visualizing the process of calling the recursive factorial() function">
122124
<figcaption>Figure 8.7: Visualizing the process of calling the recursive <code>factorial()</code> function</figcaption>
123125
</figure>
124-
<p>The <code>factorial()</code> function calls itself within its own definition. It may look a bit odd at first, but it works, as long as a stopping condition exists (in this case, <code>n &#x3C;= 1</code>) so the function doesn’t get stuck calling itself forever. (I’m using <code>&#x3C;=</code> instead of <code>===</code> as a safeguard against infinite recursion, but I should probably include additional error checking to manage noninteger or negative inputs to be more mathematically accurate.) Figure 8.7 illustrates the steps that unfold when <code>factorial(4)</code> is called.</p>
125-
<p>The function keeps calling itself, descending deeper and deeper down a rabbit hole of nested function calls until it reaches the stopping condition. Then it works its way up out of the hole, returning values until it arrives back home at the original call of <code>factorial(4)</code>.</p>
126126
<p>You can apply the same recursive principle illustrated by the <code>factorial()</code> function to graphics in a canvas, only instead of returning values, you draw shapes. This is precisely what you’ll see in the examples throughout this chapter. To begin, here’s a simple recursive function that draws increasingly smaller circles.</p>
127127
<div data-type="example">
128128
<h3 id="example-81-recursive-circles-once">Example 8.1: Recursive Circles Once</h3>

content/examples/06_libraries/6_1_default_matter_js/sketch.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ function setup() {
1010

1111
// Make the Engine
1212
let engine = Engine.create();
13-
engine.gravity.set(1, 0);
1413

1514
let render = Matter.Render.create({
1615
canvas: canvas.elt,

0 commit comments

Comments
 (0)