speice.io/blog/2024-11-15-playing-with-fire/3-log-density/chaosGameHistogram.ts

34 lines
1.2 KiB
TypeScript
Raw Normal View History

2024-12-01 21:57:10 -05:00
// hidden-start
import {randomBiUnit} from "../src/randomBiUnit";
import {randomChoice} from "../src/randomChoice";
2024-12-01 21:57:10 -05:00
import {ChaosGameFinalProps} from "../2-transforms/chaosGameFinal";
import {camera, histIndex} from "../src/camera";
// hidden-end
export type ChaosGameHistogramProps = ChaosGameFinalProps & {
paint: (width: number, histogram: Uint32Array) => ImageData;
2024-12-01 21:57:10 -05:00
}
export function* chaosGameHistogram({width, height, transforms, final, quality, step, paint}: ChaosGameHistogramProps) {
2024-12-02 22:36:25 -05:00
let iterations = (quality ?? 1) * width * height;
2024-12-01 21:57:10 -05:00
step = step ?? 100_000;
const histogram = new Uint32Array(width * height);
let [x, y] = [randomBiUnit(), randomBiUnit()];
for (let i = 0; i < iterations; i++) {
const [_, transform] = randomChoice(transforms);
[x, y] = transform(x, y);
const [finalX, finalY] = final(x, y);
2024-12-01 21:57:10 -05:00
if (i > 20) {
const [pixelX, pixelY] = camera(finalX, finalY, width);
2024-12-01 21:57:10 -05:00
const pixelIndex = histIndex(pixelX, pixelY, width, 1);
histogram[pixelIndex] += 1;
}
if (i % step === 0)
yield paint(width, histogram);
}
yield paint(width, histogram);
}