React-sim
GitHub
Code for this example

Fibonacci sequence

Both of these examples are inspired by the Fibonacci sequence, where:

  • F0 = 0,
  • F1 = 1,
  • Fn = Fn - 1 + Fn - 2 for each n > 2.

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

0
20

The corresponding udpateData function is as follows:

function updateData({ data, tick }) {
if (tick === 0) {
return [0];
}
if (tick === 1) {
return [0, 1];
}
const lastNumber = data[tick - 1] + data[tick - 2];
return [...data, lastNumber];
}

Fn / Fn-1 converges to φ, the golden ratio - (1 - √5) / 2.

Because of this convergence, the Fibonacci sequence is sometimes associated with spirals. We could:

  • draw a square of side Fn, then
  • draw a square of side Fn+1 to its right, then
  • draw a square of side Fn+2 below these two squares, then
  • draw a square of side Fn+3 to the left of these squares, then
  • a square of side Fn+4 above, etc, etc.

and by linking their corners, obtain a Golden spiral.

But we can also draw a Golden Spiral without even needing to initiate or update data.

We can start from a rectangle φ * k by k rectangle, divide its longer side by φ, draw that square, and keep on dividing the remaining rectangle. We'll have squares of same proportions and can also draw a similar spiral.

0
15

All we need is the number of steps we want to go (tick) and a Frame to render the squares and spiral.

const FibonacciSpiral = () => (
<Model initialParams={{ size: 500 }} delay={100} maxTime={20}>
<FibonacciSpiralFrame />
</Model>
);
Edit this page on GitHub
Previous:
Epidemic simulator
Next:
Game of Life
React-SimGitHub