2024-12-08 19:53:06 -05:00
|
|
|
// Hint: try changing the iteration count
|
|
|
|
const iterations = 100000;
|
2024-11-17 17:30:07 -05:00
|
|
|
|
2024-12-08 19:53:06 -05:00
|
|
|
// 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-12-15 21:19:09 -05:00
|
|
|
];
|
2024-11-24 22:37:53 -05:00
|
|
|
|
2024-12-15 21:19:09 -05:00
|
|
|
function* chaosGame({ width, height }) {
|
|
|
|
let img =
|
|
|
|
new ImageData(width, height);
|
2024-12-08 19:53:06 -05:00
|
|
|
let [x, y] = [
|
|
|
|
randomBiUnit(),
|
|
|
|
randomBiUnit()
|
|
|
|
];
|
2024-11-17 17:30:07 -05:00
|
|
|
|
2024-12-15 21:19:09 -05:00
|
|
|
for (let i = 0; i < iterations; i++) {
|
|
|
|
const index =
|
|
|
|
randomInteger(0, xforms.length);
|
|
|
|
[x, y] = xforms[index](x, y);
|
2024-11-17 17:30:07 -05:00
|
|
|
|
2024-12-15 21:19:09 -05:00
|
|
|
if (i > 20)
|
2024-12-08 19:53:06 -05:00
|
|
|
plot(x, y, img);
|
2024-11-24 18:59:11 -05:00
|
|
|
|
2024-12-15 21:19:09 -05:00
|
|
|
if (i % 1000 === 0)
|
2024-12-08 19:53:06 -05:00
|
|
|
yield img;
|
|
|
|
}
|
2024-11-24 18:59:11 -05:00
|
|
|
|
2024-12-08 19:53:06 -05:00
|
|
|
yield img;
|
2024-11-29 23:08:47 -05:00
|
|
|
}
|
|
|
|
|
2024-12-15 21:19:09 -05:00
|
|
|
render(<Gasket f={chaosGame} />);
|