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

36 lines
1.0 KiB
TypeScript
Raw 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";
2024-12-01 21:57:10 -05:00
import {ChaosGameWeightedProps} from "../1-introduction/chaosGameWeighted";
2024-11-30 17:35:42 -05:00
// hidden-end
2024-12-01 21:57:10 -05:00
export type ChaosGameFinalProps = ChaosGameWeightedProps & {
final: Transform,
quality?: number,
step?: number,
}
export function* chaosGameFinal({width, height, transforms, final, quality, step}: ChaosGameFinalProps) {
2024-11-30 17:35:42 -05:00
let image = new ImageData(width, height);
let [x, y] = [randomBiUnit(), randomBiUnit()];
2024-12-01 21:57:10 -05:00
const iterations = (quality ?? 0.5) * width * height;
step = step ?? 1000;
2024-11-30 17:35:42 -05:00
for (let i = 0; i < iterations; i++) {
const [_, transform] = randomChoice(transforms);
[x, y] = transform(x, y);
// highlight-start
[x, y] = final(x, y);
// highlight-end
if (i > 20)
plot(x, y, image);
if (i % step === 0)
yield image;
}
yield image;
}