speice.io/blog/2024-11-15-playing-with-fire/2-transforms/chaosGameFinal.ts

51 lines
1.0 KiB
TypeScript
Raw Permalink Normal View History

2024-11-30 17:35:42 -05:00
// hidden-start
import { randomBiUnit } from "../src/randomBiUnit";
import { randomChoice } from "../src/randomChoice";
import { plotBinary as plot } from "../src/plotBinary";
import { Transform } from "../src/transform";
import { Props as WeightedProps } from "../1-introduction/chaosGameWeighted";
2024-12-08 22:50:46 -05:00
const quality = 0.5;
const step = 1000;
2024-11-30 17:35:42 -05:00
// hidden-end
export type Props = WeightedProps & {
final: Transform,
2024-12-01 21:57:10 -05:00
}
2024-11-30 17:35:42 -05:00
export function* chaosGameFinal(
{
width,
height,
transforms,
final
}: Props
) {
let img =
new ImageData(width, height);
let [x, y] = [
randomBiUnit(),
randomBiUnit()
];
2024-11-30 17:35:42 -05:00
const pixels = width * height;
const iterations = quality * pixels;
for (let i = 0; i < iterations; i++) {
const [_, transform] =
randomChoice(transforms);
[x, y] = transform(x, y);
2024-11-30 17:35:42 -05:00
// highlight-start
const [finalX, finalY] = final(x, y);
// highlight-end
2024-11-30 17:35:42 -05:00
if (i > 20)
// highlight-start
plot(finalX, finalY, img);
// highlight-end
2024-11-30 17:35:42 -05:00
if (i % step === 0)
yield img;
}
yield img;
2024-11-30 17:35:42 -05:00
}