2024-12-01 21:57:10 -05:00
|
|
|
// hidden-start
|
2024-12-01 18:17:36 -05:00
|
|
|
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 & {
|
|
|
|
painter: (width: number, histogram: Uint32Array) => ImageData;
|
|
|
|
}
|
|
|
|
export function* chaosGameHistogram({width, height, transforms, final, quality, step, painter}: ChaosGameHistogramProps) {
|
|
|
|
let iterations = (quality ?? 10) * width * height;
|
|
|
|
step = step ?? 100_000;
|
|
|
|
|
|
|
|
const histogram = new Uint32Array(width * height);
|
2024-12-01 18:17:36 -05:00
|
|
|
|
|
|
|
let [x, y] = [randomBiUnit(), randomBiUnit()];
|
|
|
|
|
|
|
|
for (let i = 0; i < iterations; i++) {
|
|
|
|
const [_, transform] = randomChoice(transforms);
|
|
|
|
[x, y] = transform(x, y);
|
2024-12-01 21:57:10 -05:00
|
|
|
[x, y] = final(x, y);
|
2024-12-01 18:17:36 -05:00
|
|
|
|
2024-12-01 21:57:10 -05:00
|
|
|
if (i > 20) {
|
|
|
|
const [pixelX, pixelY] = camera(x, y, width);
|
|
|
|
const pixelIndex = histIndex(pixelX, pixelY, width, 1);
|
|
|
|
histogram[pixelIndex] += 1;
|
|
|
|
}
|
2024-12-01 18:17:36 -05:00
|
|
|
|
|
|
|
if (i % step === 0)
|
2024-12-01 21:57:10 -05:00
|
|
|
yield painter(width, histogram);
|
2024-12-01 18:17:36 -05:00
|
|
|
}
|
|
|
|
|
2024-12-01 21:57:10 -05:00
|
|
|
yield painter(width, histogram);
|
2024-12-01 18:17:36 -05:00
|
|
|
}
|