speice.io/blog/2024-11-15-playing-with-fire/1-introduction/chaosGameWeighted.ts

32 lines
877 B
TypeScript
Raw Normal View History

2024-11-24 22:37:53 -05:00
// hidden-start
import { randomBiUnit } from "../src/randomBiUnit";
import { randomChoice } from "../src/randomChoice";
import { plot } from "./plot"
import {Transform} from "../src/transform";
const iterations = 50_000;
const step = 1000;
2024-11-24 22:37:53 -05:00
// hidden-end
2024-12-01 21:57:10 -05:00
export type ChaosGameWeightedProps = {
width: number,
height: number,
transforms: [number, Transform][]
}
export function* chaosGameWeighted({width, height, transforms}: ChaosGameWeightedProps) {
let image = new ImageData(width, height);
2024-11-24 22:37:53 -05:00
var [x, y] = [randomBiUnit(), randomBiUnit()];
for (let i = 0; i < iterations; i++) {
// highlight-start
const [_, transform] = randomChoice(transforms);
// highlight-end
[x, y] = transform(x, y);
2024-11-29 19:25:29 -05:00
if (i > 20)
2024-11-24 22:37:53 -05:00
plot(x, y, image);
2024-11-29 19:25:29 -05:00
if (i % step === 0)
2024-11-24 22:37:53 -05:00
yield image;
}
yield image;
}