From 7f7813934e30448873c30d38f65f6b4183633dc1 Mon Sep 17 00:00:00 2001 From: shiffman Date: Wed, 23 Aug 2023 16:59:24 +0000 Subject: [PATCH] Notion - Update docs --- content/07_ca.html | 8 ++- .../07_ca/exercise_7_9_hexagon_ca/index.html | 15 +++++ .../07_ca/exercise_7_9_hexagon_ca/sketch.js | 60 +++++++++++++++++++ .../07_ca/exercise_7_9_hexagon_ca/style.css | 7 +++ 4 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 content/examples/07_ca/exercise_7_9_hexagon_ca/index.html create mode 100644 content/examples/07_ca/exercise_7_9_hexagon_ca/sketch.js create mode 100644 content/examples/07_ca/exercise_7_9_hexagon_ca/style.css diff --git a/content/07_ca.html b/content/07_ca.html index 8ab4a382..8dfe59fb 100644 --- a/content/07_ca.html +++ b/content/07_ca.html @@ -543,7 +543,7 @@

The Implementation

neighbors -= board[i][j];

Finally, once I know the total number of live neighbors, I can decide what the cell’s new state should be according to the rules: birth, death, or stasis.

// {.code-wide} If it is alive and has less than 2 live neighbors, it dies from loneliness.
-if        (board[i][j] == 1 && sum < 2) {
+if (board[i][j] == 1 && sum < 2) {
   next[i][j] = 0;
 // {.code-wide} If it is alive and has more than 3 live neighbors, it dies from overpopulation.
 } else if (board[x][y] == 1 && sum >  3) {
@@ -598,6 +598,7 @@ 

Example 7.2: Game of Life

square(i * w, j * w, w); } }
+

In this example, I’m introducing yet another method for drawing the squares based on a cell’s state. Remember, multiplying the cell’s state by 255 gives you white for “on” and black for “off”. To invert this, I start with 255 and subtract cell’s state multiplied by 255: black for “on” and white for “off.”

Exercise 7.6

Create a Game of Life simulation that allows you to manually configure the grid, either by hardcoding initial cell states or by drawing directly to the canvas. Use the simulation to explore some of the known Game of Life patterns.

@@ -682,6 +683,7 @@

Example 7.3: Object-Oriented Ga } } } +

By transforming the cell into an object, numerous possibilities emerge for enhancing the cell’s properties and behavior. What if each cell had a lifespan property, incrementing with each cycle which influenced its color or shape over time? Imagine if a cell had a terrain property that could be “land”, “water”, “mountain”, or “forest”. How could a two-dimensional CA integrate into a tile-based strategy game or other context?

Variations on Traditional CA

Now that I’ve covered the basic concepts, algorithms, and programming strategies behind the most famous 1D and 2D cellular automata, it’s time to think about how you might take this foundation of code and build on it, developing creative applications of CAs in your own work. In this section, I’ll talk through some ideas for expanding the features of a CA. Example answers to these exercises can be found on the book website.

1) Non-rectangular grids. There’s no particular reason why you should limit yourself to having your cells in a rectangular grid. What happens if you design a CA with another type of shape?

@@ -689,7 +691,7 @@

Variations on Traditional CA

Exercise 7.9

Create a CA using a grid of hexagons (as below), each with six neighbors.

- +
@@ -723,7 +725,7 @@

Exercise 7.13

Exercise 7.14

Use CA rules in a flocking system. What if each boid had a state (that perhaps informs its steering behaviors) and its neighborhood changed from frame to frame as it moved closer to or farther from other boids?

-

7) Nesting. Another feature of complex systems is that they can be nested. Our world tends to work this way: a city is a complex system of people, a person is a complex system of organs, an organ is a complex system of cells, and so on and so forth.

+

7) Nesting. As discussed in chapter 5, a feature of complex systems is that they can be nested. A city is a complex system of people, a person is a complex system of organs, an organ is a complex system of cells, and so on and so forth. How could this be applied to CA?

Exercise 7.15

Design a CA in which each cell itself is a smaller CA.

diff --git a/content/examples/07_ca/exercise_7_9_hexagon_ca/index.html b/content/examples/07_ca/exercise_7_9_hexagon_ca/index.html new file mode 100644 index 00000000..3cb923ff --- /dev/null +++ b/content/examples/07_ca/exercise_7_9_hexagon_ca/index.html @@ -0,0 +1,15 @@ + + + + + + + + + + +
+
+ + + diff --git a/content/examples/07_ca/exercise_7_9_hexagon_ca/sketch.js b/content/examples/07_ca/exercise_7_9_hexagon_ca/sketch.js new file mode 100644 index 00000000..0c82c044 --- /dev/null +++ b/content/examples/07_ca/exercise_7_9_hexagon_ca/sketch.js @@ -0,0 +1,60 @@ +let w, h; +let rows, columns; + +let board; + +function setup() { + createCanvas(640, 240); + angleMode(DEGREES); + w = 20; + h = sin(60) * w; + columns = floor((width / w) * 3); + rows = floor(height / h) + 2; + board = create2DArray(columns, rows); + for (let i = 0; i < columns; i++) { + for (let j = 0; j < rows; j++) { + board[i][j] = floor(random(2)); + } + } +} + +function draw() { + background(220); + + for (let i = 0; i < columns; i++) { + for (let j = 0; j < rows; j++) { + let x = i * w * 3; + let y = j * h; + if (j % 2 == 0) { + x += 1.5 * w; + } + fill(255 - board[i][j] * 255); + drawHexagon(x, y, w); + } + } +} + +function drawHexagon(x, y, w) { + push(); + translate(x, y); + stroke(0); + beginShape(); + for (let i = 0; i < 360; i += 60) { + let xoff = cos(i) * w; + let yoff = sin(i) * w; + vertex(xoff, yoff); + } + endShape(CLOSE); + pop(); +} + +function create2DArray(rows, columns) { + let arr = new Array(rows); + for (let i = 0; i < rows; i++) { + arr[i] = new Array(columns); + for (let j = 0; j < columns; j++) { + arr[i][j] = 0; + } + } + return arr; +} diff --git a/content/examples/07_ca/exercise_7_9_hexagon_ca/style.css b/content/examples/07_ca/exercise_7_9_hexagon_ca/style.css new file mode 100644 index 00000000..9386f1c2 --- /dev/null +++ b/content/examples/07_ca/exercise_7_9_hexagon_ca/style.css @@ -0,0 +1,7 @@ +html, body { + margin: 0; + padding: 0; +} +canvas { + display: block; +}