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-09 22:18:13 -05:00
|
|
|
import {Props as ChaosGameFinalProps} from "../2-transforms/chaosGameFinal";
|
2024-12-01 21:57:10 -05:00
|
|
|
import {camera, histIndex} from "../src/camera";
|
2024-12-09 22:18:13 -05:00
|
|
|
|
|
|
|
const quality = 10;
|
|
|
|
const step = 100_000;
|
2024-12-01 21:57:10 -05:00
|
|
|
// hidden-end
|
2024-12-09 22:18:13 -05:00
|
|
|
export type Props = ChaosGameFinalProps & {
|
|
|
|
paint: (width: number, height: number, histogram: number[]) => ImageData;
|
2024-12-01 21:57:10 -05:00
|
|
|
}
|
2024-12-09 22:18:13 -05:00
|
|
|
export function* chaosGameHistogram({width, height, transforms, final, paint}: Props) {
|
|
|
|
let iterations = quality * width * height;
|
2024-12-01 21:57:10 -05:00
|
|
|
|
2024-12-09 22:18:13 -05:00
|
|
|
const histogram = Array<number>(width * height).fill(0);
|
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-08 15:35:27 -05:00
|
|
|
const [finalX, finalY] = final(x, y);
|
2024-12-01 18:17:36 -05:00
|
|
|
|
2024-12-01 21:57:10 -05:00
|
|
|
if (i > 20) {
|
2024-12-08 15:35:27 -05:00
|
|
|
const [pixelX, pixelY] = camera(finalX, finalY, width);
|
2024-12-09 22:18:13 -05:00
|
|
|
const hIndex = histIndex(pixelX, pixelY, width, 1);
|
|
|
|
|
|
|
|
if (hIndex < 0 || hIndex >= histogram.length) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
histogram[hIndex] += 1;
|
2024-12-01 21:57:10 -05:00
|
|
|
}
|
2024-12-01 18:17:36 -05:00
|
|
|
|
|
|
|
if (i % step === 0)
|
2024-12-09 22:18:13 -05:00
|
|
|
yield paint(width, height, histogram);
|
2024-12-01 18:17:36 -05:00
|
|
|
}
|
|
|
|
|
2024-12-09 22:18:13 -05:00
|
|
|
yield paint(width, height, histogram);
|
2024-12-01 18:17:36 -05:00
|
|
|
}
|