wiki-sierpinski-carpet.ts

943 B · typescript · 20 lines

1import * as math from "mrlyjs/math";2import { Grid, type Ink, type Pen } from "mrlyjs/view";34const CODE = "495";5const LEVELS = 4;67export const units = { math };8export const still = (LEVELS - 1) / LEVELS;9export const loop = 8;10export const frames = LEVELS;1112export default function draw(pen: Pen, ink: Ink, t: number) {13  const level = 1 + Math.min(Math.max(Math.floor(t * LEVELS), 0), LEVELS - 1);14  const cells = math.two.create(CODE, 3, level, 0, 3);15  const side = cells.shape[1];16  const filled = cells.types.reduce((sum: number, kind: number) => sum + (kind !== 0 ? 1 : 0), 0);17  if (side !== 3 ** level) throw new Error(`wiki-sierpinski-carpet: side ${side} at level ${level}, want ${3 ** level}`);18  if (filled !== 8 ** level) throw new Error(`wiki-sierpinski-carpet: ${filled} cells at level ${level}, want ${8 ** level}`);19  new Grid(pen.frame(0.08), side, side, 0).paint(pen, cells, (kind) => (kind !== 0 ? ink.blue : null));20}