2024-11-29 23:08:47 -05:00
|
|
|
// Hint: try increasing the iteration count
|
|
|
|
const iterations = 10000;
|
2024-11-17 17:30:07 -05:00
|
|
|
|
2024-11-29 23:08:47 -05:00
|
|
|
// Hint: negating `x` and `y` creates some interesting images
|
|
|
|
const transforms = [
|
|
|
|
(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
|
|
|
|
2024-11-29 23:08:47 -05:00
|
|
|
function* chaosGame() {
|
|
|
|
let image = new ImageData(500, 500);
|
|
|
|
let [x, y] = [randomBiUnit(), randomBiUnit()];
|
2024-11-17 17:30:07 -05:00
|
|
|
|
2024-11-29 23:08:47 -05:00
|
|
|
for (var count = 0; count < iterations; count++) {
|
|
|
|
const i = randomInteger(0, transforms.length);
|
|
|
|
[x, y] = transforms[i](x, y);
|
2024-11-17 17:30:07 -05:00
|
|
|
|
2024-11-29 23:08:47 -05:00
|
|
|
if (count > 20)
|
|
|
|
plot(x, y, image);
|
2024-11-24 18:59:11 -05:00
|
|
|
|
2024-11-29 23:08:47 -05:00
|
|
|
if (count % 1000 === 0)
|
|
|
|
yield image;
|
2024-11-17 17:30:07 -05:00
|
|
|
}
|
2024-11-24 18:59:11 -05:00
|
|
|
|
2024-11-29 23:08:47 -05:00
|
|
|
yield image;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wiring so the code above displays properly
|
|
|
|
function Gasket() {
|
|
|
|
const {setPainter} = useContext(PainterContext);
|
|
|
|
setPainter(chaosGame());
|
|
|
|
|
|
|
|
return (<></>)
|
2024-11-17 17:30:07 -05:00
|
|
|
}
|
2024-11-24 18:59:11 -05:00
|
|
|
render(<Gasket/>)
|