Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144import 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<CreatureNode>(numNodes).fill(undefined).map((_) => { return randomNode(rng, anchor); }); return { nodes, muscles: [] };}
export function renderCreatureSvg(creature: Creature): string { if (creature.nodes.length === 0) return "<svg></svg>";
// 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 += `<line x1="${muscle.n1.pos.x}" y1="${muscle.n1.pos.y}" x2="${muscle.n2.pos.x}" y2="${muscle.n2.pos.y}" stroke="#d65d5d" stroke-width="${strokeWidth}" stroke-linecap="round" />`; }
// Draw Nodes for (const node of creature.nodes) { svgInner += `<circle cx="${node.pos.x}" cy="${node.pos.y}" r="${node.size}" fill="#333" stroke="#fff" stroke-width="${node.size * 0.1}" />`; }
// 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 `<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="${viewBoxX} ${viewBoxY} ${viewBoxW} ${viewBoxH}" style="background-color: #f0f0f0; border: 1px solid #ccc;" preserveAspectRatio="xMidYMid meet" > ${svgInner} </svg>`;}
export function step(creature: Creature): Creature { const muscles = creature.muscles.map((muscle) => { return { ...muscle, intervalProgress: muscle.intervalProgress + 1 }; });
return { ...creature, };}