import prand, { unsafeUniformIntDistribution, type RandomGenerator, } from "pure-rand"; import { Point } from "@mathigon/euclid"; import type Rand from "rand-seed"; export interface Creature { nodes: CreatureNode[]; muscles: CreatureMuscle[]; } export interface CreatureNode { pos: Point; vel: Point; size: number; friction: number; } export interface CreatureMuscle { n1: CreatureNode; n2: CreatureNode; interval: number; intervalProgress: number; contractTime: number; // extendTime is 1-contractTime contractLength: number; extendLength: number; stiffness: number; } function generateFloat64(rng: RandomGenerator) { const g1 = prand.unsafeUniformIntDistribution(0, (1 << 26) - 1, rng); const g2 = prand.unsafeUniformIntDistribution(0, (1 << 27) - 1, rng); const value = (g1 * Math.pow(2, 27) + g2) * Math.pow(2, -53); return value; } function randomNode(rng: RandomGenerator, anchor: Point): CreatureNode { const scale = 1.0; const delta = new Point( generateFloat64(rng) * scale - scale, generateFloat64(rng) * scale - scale, ); const pos = anchor.add(delta); const vel = new Point(0, 0); const size = generateFloat64(rng) * 0.1 + 0.1; const friction = generateFloat64(rng) * 0.1 + 0.1; return { pos, vel, size, friction }; } export function randomCreature(rng: RandomGenerator): Creature { const anchor = new Point(0, 0); const numNodes = unsafeUniformIntDistribution(3, 8, rng); const nodes = new Array(numNodes).fill(undefined).map((_) => { return randomNode(rng, anchor); }); return { nodes, muscles: [] }; } export function renderCreatureSvg(creature: Creature): string { if (creature.nodes.length === 0) return ""; // 1. Calculate Bounds // We initialize with Infinity so the first node sets the baseline. let minX = Infinity; let minY = Infinity; let maxX = -Infinity; let maxY = -Infinity; for (const node of creature.nodes) { // We must account for the node's radius (size) so the circle // isn't clipped at the edge of the image. minX = Math.min(minX, node.pos.x - node.size); minY = Math.min(minY, node.pos.y - node.size); maxX = Math.max(maxX, node.pos.x + node.size); maxY = Math.max(maxY, node.pos.y + node.size); } // 2. Add Padding // We calculate the content dimensions and add a 5% buffer // so the creature isn't touching the SVG borders. const contentWidth = maxX - minX; const contentHeight = maxY - minY; const padding = Math.max(contentWidth, contentHeight) * 0.05; const viewBoxX = minX - padding; const viewBoxY = minY - padding; const viewBoxW = contentWidth + padding * 2; const viewBoxH = contentHeight + padding * 2; // 3. Generate SVG Content // We use the raw coordinates directly; the viewBox handles the scaling/centering. let svgInner = ""; // Draw Muscles for (const muscle of creature.muscles) { // Heuristic: thickness relative to the overall size of the creature // or keep it based on stiffness. Here we ensure it's at least visible relative to the view. const strokeWidth = Math.max(viewBoxW / 200, muscle.stiffness * 0.1); svgInner += ``; } // Draw Nodes for (const node of creature.nodes) { svgInner += ``; } // 4. Return SVG with calculated ViewBox // The width/height attributes are set to "100%" to let the container control the display size, // while the viewBox ensures the creature is tightly framed. return ` ${svgInner} `; } export function step(creature: Creature): Creature { const muscles = creature.muscles.map((muscle) => { return { ...muscle, intervalProgress: muscle.intervalProgress + 1 }; }); return { ...creature, }; }