2024-11-24 22:37:53 -05:00
|
|
|
// hidden-start
|
|
|
|
import { randomBiUnit } from "../src/randomBiUnit";
|
|
|
|
import { randomChoice } from "../src/randomChoice";
|
2024-12-15 21:19:09 -05:00
|
|
|
import { plot } from "./plot";
|
|
|
|
import { Transform } from "../src/transform";
|
2024-12-09 22:18:13 -05:00
|
|
|
|
|
|
|
const quality = 0.5;
|
2024-11-29 23:08:47 -05:00
|
|
|
const step = 1000;
|
2024-11-24 22:37:53 -05:00
|
|
|
// hidden-end
|
2024-12-08 22:50:46 -05:00
|
|
|
export type Props = {
|
2024-12-15 21:19:09 -05:00
|
|
|
width: number,
|
|
|
|
height: number,
|
|
|
|
transforms: [number, Transform][]
|
2024-12-01 21:57:10 -05:00
|
|
|
}
|
2024-12-15 21:19:09 -05:00
|
|
|
|
2024-12-08 19:53:06 -05:00
|
|
|
export function* chaosGameWeighted(
|
2024-12-15 21:19:09 -05:00
|
|
|
{ width, height, transforms }: Props
|
2024-12-08 19:53:06 -05:00
|
|
|
) {
|
2024-12-15 21:19:09 -05:00
|
|
|
let img =
|
|
|
|
new ImageData(width, height);
|
2024-12-08 19:53:06 -05:00
|
|
|
let [x, y] = [
|
2024-12-15 21:19:09 -05:00
|
|
|
randomBiUnit(),
|
|
|
|
randomBiUnit()
|
2024-12-08 19:53:06 -05:00
|
|
|
];
|
2024-11-24 22:37:53 -05:00
|
|
|
|
2024-12-15 21:19:09 -05:00
|
|
|
const pixels = width * height;
|
|
|
|
const iterations = quality * pixels;
|
|
|
|
for (let i = 0; i < iterations; i++) {
|
2024-12-08 19:53:06 -05:00
|
|
|
// highlight-start
|
2024-12-15 21:19:09 -05:00
|
|
|
const [_, xform] =
|
|
|
|
randomChoice(transforms);
|
2024-12-08 19:53:06 -05:00
|
|
|
// highlight-end
|
|
|
|
[x, y] = xform(x, y);
|
2024-11-24 22:37:53 -05:00
|
|
|
|
2024-12-15 21:19:09 -05:00
|
|
|
if (i > 20)
|
2024-12-08 19:53:06 -05:00
|
|
|
plot(x, y, img);
|
2024-11-24 22:37:53 -05:00
|
|
|
|
2024-12-15 21:19:09 -05:00
|
|
|
if (i % step === 0)
|
2024-12-08 19:53:06 -05:00
|
|
|
yield img;
|
|
|
|
}
|
2024-11-24 22:37:53 -05:00
|
|
|
|
2024-12-08 19:53:06 -05:00
|
|
|
yield img;
|
2024-11-24 22:37:53 -05:00
|
|
|
}
|