speice.io/blog/2024-11-15-playing-with-fire/1-introduction/chaosGame.js

35 lines
659 B
JavaScript
Raw Normal View History

// Hint: try changing the iteration count
const iterations = 100000;
// Hint: negating `x` and `y` creates some cool images
const xforms = [
(x, y) => [x / 2, y / 2],
(x, y) => [(x + 1) / 2, y / 2],
(x, y) => [x / 2, (y + 1) / 2]
];
2024-11-24 22:37:53 -05:00
function* chaosGame({ width, height }) {
let img =
new ImageData(width, height);
let [x, y] = [
randomBiUnit(),
randomBiUnit()
];
for (let i = 0; i < iterations; i++) {
const index =
randomInteger(0, xforms.length);
[x, y] = xforms[index](x, y);
if (i > 20)
plot(x, y, img);
2024-11-24 18:59:11 -05:00
if (i % 1000 === 0)
yield img;
}
2024-11-24 18:59:11 -05:00
yield img;
}
render(<Gasket f={chaosGame} />);