diff --git a/content/00_randomness.html b/content/00_randomness.html index 7f11e610..dbcb6eb9 100644 --- a/content/00_randomness.html +++ b/content/00_randomness.html @@ -128,20 +128,20 @@

Coding Conventions

background(255); }

Finally, during each cycle through draw(), the walker takes a step and draws a dot:

-
//{!1} Then draw() loops forever and ever (until you quit).
-function draw() {
-  // Call functions on the walker.
-  walker.step();
-  walker.show();
-}
-

Since the background is drawn once in setup(), rather than clearing it continually each time through draw(), the trail of the random walk is visible in the canvas.

Example 0.1: A Traditional Random Walk

Each time you see one of these example boxes in the book, it means that corresponding code is available in the p5.js web editor and on the book’s website. If you’re reading this book offline, you’ll see only a screenshot of the resulting canvas.
+
//{!1} Then draw() loops forever and ever (until you quit).
+function draw() {
+  // Call functions on the walker.
+  walker.step();
+  walker.show();
+}
+

Since the background is drawn once in setup(), rather than clearing it continually each time through draw(), the trail of the random walk is visible in the canvas.

I could make a couple of adjustments to the random walker. For one, this Walker object’s steps are limited to four options: up, down, left, and right. But any given pixel in the canvas could have eight possible neighbors, including diagonals (see Figure 0.1). A ninth possibility, to stay in the same place, could also be an option.

Figure 0.1: The steps of a random walker, with and without diagonals diff --git a/content/10_nn.html b/content/10_nn.html index c1d07627..d6445caf 100644 --- a/content/10_nn.html +++ b/content/10_nn.html @@ -395,10 +395,12 @@

The Perceptron Code

return 0.5 * x - 1; }

Now there’s the matter of the p5.js canvas defaulting to (0, 0) in the top-left corner with the y-axis pointing down. For this discussion, I’ll assume I’ve built the following into the code to reorient the canvas to match a more traditional Cartesian space:

-
// Move the origin (0, 0) to the center.
+
+
// Move the origin (0, 0) to the center.
 translate(width / 2, height / 2);
 // Flip the y-axis orientation (positive points up!).
 scale(1, -1);
+

I can now pick a random point in the 2D space:

let x = random(-100, 100);
 let y = random(-100, 100);