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