speice.io/blog/2024-11-15-playing-with-fire/1-introduction/Gasket.jsx

28 lines
685 B
React
Raw Normal View History

2024-11-17 20:42:42 -05:00
// Hint: try increasing the iteration count
const iterations = 10000;
2024-11-17 21:34:32 -05:00
// Hint: negating `x` and `y` creates some interesting images
2024-11-17 20:42:42 -05:00
const functions = [
(x, y) => [x / 2, y / 2],
(x, y) => [(x + 1) / 2, y / 2],
(x, y) => [x / 2, (y + 1) / 2]
]
2024-11-17 20:42:42 -05:00
function chaosGame(image) {
2024-11-23 15:26:48 -05:00
var plotter = new Plotter(image.width, image.height);
2024-11-17 20:42:42 -05:00
var [x, y] = [randomBiUnit(), randomBiUnit()];
2024-11-17 20:42:42 -05:00
for (var count = 0; count < iterations; count++) {
const i = randomInteger(0, functions.length);
[x, y] = functions[i](x, y);
2024-11-17 20:42:42 -05:00
if (count > 20) {
2024-11-23 15:26:48 -05:00
plotter.plot(x, y);
2024-11-17 20:42:42 -05:00
}
}
2024-11-23 15:26:48 -05:00
plotter.paint(image);
}
2024-11-17 20:42:42 -05:00
render(<Canvas renderFn={chaosGame}/>)