// Point clouds for the home page particle field. Each returns m points (x, y, z) centred on the origin, // roughly unit sized; the field scales and places them per section. type Draw = (g: CanvasRenderingContext2D, S: number) => void; const rnd = (a: number, b: number) => a + Math.random() * (b - a); const gauss = () => { let u = 0, v = 0; while (!u) u = Math.random(); while (!v) v = Math.random(); return Math.sqrt(-2 * Math.log(u)) * Math.cos(6.2832 * v); }; function unit(): [number, number, number] { const u = rnd(-1, 1), t = rnd(0, 6.2832), s = Math.sqrt(1 - u * u); return [s * Math.cos(t), u, s * Math.sin(t)]; } // Rasterise a filled outline, then a chamfer distance transform gives every inside pixel its distance to the edge. function maskField(draw: (g: CanvasRenderingContext2D) => void, W: number, H: number) { const c = document.createElement("canvas"); c.width = W; c.height = H; const g = c.getContext("2d")!; g.fillStyle = "#fff"; draw(g); const a = g.getImageData(0, 0, W, H).data, D = new Float32Array(W * H), pts: number[] = []; for (let i = 0; i < W * H; i++) D[i] = a[i * 4 + 3] > 127 ? 1e9 : 0; for (let y = 0; y < H; y++) for (let x = 0; x < W; x++) { const i = y * W + x; if (!D[i]) continue; let d = D[i]; d = x > 0 ? Math.min(d, D[i - 1] + 1) : Math.min(d, 1); if (y > 0) { d = Math.min(d, D[i - W] + 1); if (x > 0) d = Math.min(d, D[i - W - 1] + 1.414); if (x < W - 1) d = Math.min(d, D[i - W + 1] + 1.414); } else d = Math.min(d, 1); D[i] = d; } let max = 1; for (let y = H - 1; y >= 0; y--) for (let x = W - 1; x >= 0; x--) { const i = y * W + x; if (!D[i]) continue; let d = D[i]; d = x < W - 1 ? Math.min(d, D[i + 1] + 1) : Math.min(d, 1); if (y < H - 1) { d = Math.min(d, D[i + W] + 1); if (x < W - 1) d = Math.min(d, D[i + W + 1] + 1.414); if (x > 0) d = Math.min(d, D[i + W - 1] + 1.414); } else d = Math.min(d, 1); D[i] = d; if (d > max) max = d; pts.push(i); } return { D, pts, max }; } function segDist(px: number, py: number, line: number[][]) { let best = 1e9; for (let k = 0; k < line.length - 1; k++) { const [ax, ay] = line[k], [bx, by] = line[k + 1], vx = bx - ax, vy = by - ay; const t = Math.max(0, Math.min(1, ((px - ax) * vx + (py - ay) * vy) / (vx * vx + vy * vy))); const dx = px - ax - vx * t, dy = py - ay - vy * t; best = Math.min(best, dx * dx + dy * dy); } return Math.sqrt(best); } // The brain: a lateral (side-view) outline with the frontal lobe facing left, inflated into two hemispheres, // with the lateral and central sulci plus smaller folds carved into the surface. Units are outline pixels. // Returns the points and a per-point shade (dark in the grooves, bright on the ridges). export function brainPts(m: number) { const W = 400, H = 300; const cer = maskField(g => { g.beginPath(); g.moveTo(36, 150); g.bezierCurveTo(30, 95, 62, 48, 125, 33); g.bezierCurveTo(190, 18, 265, 22, 312, 50); g.bezierCurveTo(358, 78, 386, 125, 378, 170); g.bezierCurveTo(372, 198, 352, 214, 326, 216); g.lineTo(296, 213); g.bezierCurveTo(272, 212, 258, 226, 244, 236); g.bezierCurveTo(205, 252, 150, 252, 118, 236); g.bezierCurveTo(98, 226, 90, 206, 104, 193); g.lineTo(118, 186); g.bezierCurveTo(92, 186, 60, 182, 44, 170); g.bezierCurveTo(38, 165, 35, 158, 36, 150); g.closePath(); g.fill(); }, W, H); const cbl = maskField(g => { g.beginPath(); g.ellipse(314, 240, 58, 33, -0.12, 0, 6.2832); g.fill(); }, W, H); const stem = maskField(g => { g.beginPath(); g.moveTo(236, 228); g.lineTo(268, 230); g.lineTo(278, 298); g.lineTo(252, 299); g.closePath(); g.fill(); }, W, H); const sylvian = [[116, 188], [170, 160], [228, 136], [262, 122], [276, 108]]; const central = [[206, 24], [196, 70], [186, 110], [176, 150]]; const fold = (x: number, y: number, z: number) => { const s = 3.2, qx = x * s, qy = y * s, qz = z * s; const wx = qx + 0.9 * Math.sin(qy * 1.3 + qz * 0.7), wy = qy + 0.9 * Math.sin(qz * 1.1 + qx * 0.8), wz = qz + 0.9 * Math.sin(qx * 1.2 + qy * 0.9); return Math.sin(wx * 1.1 + wy * 0.3) * Math.cos(wy - wz * 0.6) + 0.6 * Math.sin(wz * 1.3 + wx * 0.5 + wy * 0.4); }; const pick = (f: ReturnType) => { const p = f.pts[(Math.random() * f.pts.length) | 0]; return { p, x: (p % W) + Math.random(), y: ((p / W) | 0) + Math.random() }; }; const out = new Float32Array(m * 3), shade = new Float32Array(m), k = 3 / 345, gap = 4; let i = 0; while (i < m) { const r = Math.random(); let x: number, y: number, z: number, sh: number; if (r < 0.84) { const q = pick(cer); x = q.x; y = q.y; const dn = cer.D[q.p] / cer.max; let h = 140 * Math.pow(dn, 0.42); if (dn < 0.16 && Math.random() < 0.22) h *= Math.random(); const side = Math.random() < 0.5 ? -1 : 1; const ds = Math.min(segDist(x, y, sylvian), segDist(x, y, central) * 1.3); if (ds < 4.5 && Math.random() < 0.93) continue; let groove = 1 - Math.min(1, Math.abs(fold(x / 100, y / 100, (gap + h) / 100)) / 0.42); groove = groove * groove * (3 - 2 * groove); if (groove > 0.45 && Math.random() < 0.95) continue; h = Math.max(0, h - groove * 22); z = side * (gap + h); sh = (1 - groove) * Math.min(1, ds / 14 + 0.25); } else if (r < 0.95) { const q = pick(cbl); x = q.x; y = q.y; const f = Math.abs(Math.sin((y - 0.12 * x) * 0.42)); if (f < 0.28 && Math.random() < 0.8) continue; z = (Math.random() < 0.5 ? -1 : 1) * 96 * Math.pow(cbl.D[q.p] / cbl.max, 0.5); sh = 0.25 + f * 0.5; } else { const q = pick(stem); x = q.x; y = q.y; z = (Math.random() < 0.5 ? -1 : 1) * 22 * Math.pow(stem.D[q.p] / stem.max, 0.5); sh = 0.3; } out[i * 3] = (x - 210) * k; out[i * 3 + 1] = -(y - 160) * k; out[i * 3 + 2] = z * k; shade[i] = sh; i++; } return { points: out, shade }; } // Sample points from anything drawn on a canvas, with a little depth export function drawPts(draw: Draw, m: number) { const S = 360, c = document.createElement("canvas"); c.width = c.height = S; const g = c.getContext("2d")!; g.fillStyle = "#fff"; g.strokeStyle = "#fff"; draw(g, S); const d = g.getImageData(0, 0, S, S).data, px: number[] = []; for (let y = 0; y < S; y += 2) for (let x = 0; x < S; x += 2) if (d[(y * S + x) * 4 + 3] > 128) px.push(x, y); const out = new Float32Array(m * 3), n = px.length / 2; for (let i = 0; i < m; i++) { const k = ((Math.random() * n) | 0) * 2; out[i * 3] = (px[k] + Math.random() * 2) / S - 0.5; out[i * 3 + 1] = 0.5 - (px[k + 1] + Math.random() * 2) / S; out[i * 3 + 2] = (Math.random() - 0.5) * 0.14; } return out; } export const glyph = (txt: string, font: string): Draw => (g, S) => { g.font = `600 ${S * 0.46}px ${font}, ui-monospace, Menlo, monospace`; g.textAlign = "center"; g.textBaseline = "middle"; g.fillText(txt, S / 2, S / 2); }; export const arrow: Draw = (g, S) => { const k = S * 0.04, ox = S / 2 - 6.5 * k, oy = S / 2 - 9.8 * k; const p = [[0, 0], [0, 17], [4.4, 13.2], [7.4, 19.6], [10.4, 18.3], [7.5, 12], [13, 12]]; g.beginPath(); p.forEach((q, i) => (i ? g.lineTo(ox + q[0] * k, oy + q[1] * k) : g.moveTo(ox + q[0] * k, oy + q[1] * k))); g.closePath(); g.fill(); }; export const film: Draw = (g, S) => { g.lineWidth = S * 0.045; const x = S * 0.13, y = S * 0.25, w = S * 0.74, h = S * 0.5, r = S * 0.06; g.beginPath(); g.moveTo(x + r, y); g.arcTo(x + w, y, x + w, y + h, r); g.arcTo(x + w, y + h, x, y + h, r); g.arcTo(x, y + h, x, y, r); g.arcTo(x, y, x + w, y, r); g.closePath(); g.stroke(); g.beginPath(); g.moveTo(S * 0.43, S * 0.38); g.lineTo(S * 0.43, S * 0.62); g.lineTo(S * 0.63, S * 0.5); g.closePath(); g.fill(); }; // A big cloud and a small one, for the hosted tool vs Runpod comparison export function clusterPts(m: number) { const out = new Float32Array(m * 3); for (let i = 0; i < m; i++) { const big = i < m * 0.86, d = unit(), R = big ? 0.36 : 0.085, k = R * (1 - Math.pow(Math.random(), 3) * 0.65); const cx = big ? -0.1 : 0.42, cy = big ? 0.06 : -0.3; out[i * 3] = cx + d[0] * k; out[i * 3 + 1] = cy + d[1] * k; out[i * 3 + 2] = d[2] * k; } return out; } export { galaxyPts } from "./galaxy-shape";