mirror of
https://github.com/bspeice/speice.io
synced 2024-12-23 00:58:09 -05:00
41 lines
1.3 KiB
TypeScript
41 lines
1.3 KiB
TypeScript
// hidden-start
|
|
import {randomBiUnit} from "../src/randomBiUnit";
|
|
import {randomChoice} from "../src/randomChoice";
|
|
import {Props as ChaosGameFinalProps} from "../2-transforms/chaosGameFinal";
|
|
import {camera, histIndex} from "../src/camera";
|
|
|
|
const quality = 10;
|
|
const step = 100_000;
|
|
// hidden-end
|
|
export type Props = ChaosGameFinalProps & {
|
|
paint: (width: number, height: number, histogram: number[]) => ImageData;
|
|
}
|
|
export function* chaosGameHistogram({width, height, transforms, final, paint}: Props) {
|
|
let iterations = quality * width * height;
|
|
|
|
const histogram = Array<number>(width * height).fill(0);
|
|
|
|
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);
|
|
|
|
if (i > 20) {
|
|
const [pixelX, pixelY] = camera(finalX, finalY, width);
|
|
const hIndex = histIndex(pixelX, pixelY, width, 1);
|
|
|
|
if (hIndex < 0 || hIndex >= histogram.length) {
|
|
continue;
|
|
}
|
|
|
|
histogram[hIndex] += 1;
|
|
}
|
|
|
|
if (i % step === 0)
|
|
yield paint(width, height, histogram);
|
|
}
|
|
|
|
yield paint(width, height, histogram);
|
|
} |