Branchwell

AI Resource Pro / website · airesourcepro.com / src / components / home / particle-shapes.ts

1// Point clouds for the home page particle field. Each returns m points (x, y, z) centred on the origin,
2// roughly unit sized; the field scales and places them per section.
3
4type Draw = (g: CanvasRenderingContext2D, S: number) => void;
5
6const rnd = (a: number, b: number) => a + Math.random() * (b - a);
7const gauss = () => {
8 let u = 0, v = 0;
9 while (!u) u = Math.random();
10 while (!v) v = Math.random();
11 return Math.sqrt(-2 * Math.log(u)) * Math.cos(6.2832 * v);
12};
13function unit(): [number, number, number] {
14 const u = rnd(-1, 1), t = rnd(0, 6.2832), s = Math.sqrt(1 - u * u);
15 return [s * Math.cos(t), u, s * Math.sin(t)];
16}
17
18// Rasterise a filled outline, then a chamfer distance transform gives every inside pixel its distance to the edge.
19function maskField(draw: (g: CanvasRenderingContext2D) => void, W: number, H: number) {
20 const c = document.createElement("canvas"); c.width = W; c.height = H;
21 const g = c.getContext("2d")!; g.fillStyle = "#fff"; draw(g);
22 const a = g.getImageData(0, 0, W, H).data, D = new Float32Array(W * H), pts: number[] = [];
23 for (let i = 0; i < W * H; i++) D[i] = a[i * 4 + 3] > 127 ? 1e9 : 0;
24 for (let y = 0; y < H; y++) for (let x = 0; x < W; x++) {
25 const i = y * W + x; if (!D[i]) continue;
26 let d = D[i];
27 d = x > 0 ? Math.min(d, D[i - 1] + 1) : Math.min(d, 1);
28 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);
29 D[i] = d;
30 }
31 let max = 1;
32 for (let y = H - 1; y >= 0; y--) for (let x = W - 1; x >= 0; x--) {
33 const i = y * W + x; if (!D[i]) continue;
34 let d = D[i];
35 d = x < W - 1 ? Math.min(d, D[i + 1] + 1) : Math.min(d, 1);
36 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);
37 D[i] = d; if (d > max) max = d; pts.push(i);
38 }
39 return { D, pts, max };
40}
41
42function segDist(px: number, py: number, line: number[][]) {
43 let best = 1e9;
44 for (let k = 0; k < line.length - 1; k++) {
45 const [ax, ay] = line[k], [bx, by] = line[k + 1], vx = bx - ax, vy = by - ay;
46 const t = Math.max(0, Math.min(1, ((px - ax) * vx + (py - ay) * vy) / (vx * vx + vy * vy)));
47 const dx = px - ax - vx * t, dy = py - ay - vy * t; best = Math.min(best, dx * dx + dy * dy);
48 }
49 return Math.sqrt(best);
50}
51
52// The brain: a lateral (side-view) outline with the frontal lobe facing left, inflated into two hemispheres,
53// with the lateral and central sulci plus smaller folds carved into the surface. Units are outline pixels.
54// Returns the points and a per-point shade (dark in the grooves, bright on the ridges).
55export function brainPts(m: number) {
56 const W = 400, H = 300;
57 const cer = maskField(g => {
58 g.beginPath(); g.moveTo(36, 150);
59 g.bezierCurveTo(30, 95, 62, 48, 125, 33); g.bezierCurveTo(190, 18, 265, 22, 312, 50);
60 g.bezierCurveTo(358, 78, 386, 125, 378, 170); g.bezierCurveTo(372, 198, 352, 214, 326, 216);
61 g.lineTo(296, 213); g.bezierCurveTo(272, 212, 258, 226, 244, 236); g.bezierCurveTo(205, 252, 150, 252, 118, 236);
62 g.bezierCurveTo(98, 226, 90, 206, 104, 193); g.lineTo(118, 186); g.bezierCurveTo(92, 186, 60, 182, 44, 170);
63 g.bezierCurveTo(38, 165, 35, 158, 36, 150); g.closePath(); g.fill();
64 }, W, H);
65 const cbl = maskField(g => { g.beginPath(); g.ellipse(314, 240, 58, 33, -0.12, 0, 6.2832); g.fill(); }, W, H);
66 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);
67 const sylvian = [[116, 188], [170, 160], [228, 136], [262, 122], [276, 108]];
68 const central = [[206, 24], [196, 70], [186, 110], [176, 150]];
69 const fold = (x: number, y: number, z: number) => {
70 const s = 3.2, qx = x * s, qy = y * s, qz = z * s;
71 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);
72 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);
73 };
74 const pick = (f: ReturnType<typeof maskField>) => {
75 const p = f.pts[(Math.random() * f.pts.length) | 0];
76 return { p, x: (p % W) + Math.random(), y: ((p / W) | 0) + Math.random() };
77 };
78 const out = new Float32Array(m * 3), shade = new Float32Array(m), k = 3 / 345, gap = 4;
79 let i = 0;
80 while (i < m) {
81 const r = Math.random(); let x: number, y: number, z: number, sh: number;
82 if (r < 0.84) {
83 const q = pick(cer); x = q.x; y = q.y;
84 const dn = cer.D[q.p] / cer.max;
85 let h = 140 * Math.pow(dn, 0.42);
86 if (dn < 0.16 && Math.random() < 0.22) h *= Math.random();
87 const side = Math.random() < 0.5 ? -1 : 1;
88 const ds = Math.min(segDist(x, y, sylvian), segDist(x, y, central) * 1.3);
89 if (ds < 4.5 && Math.random() < 0.93) continue;
90 let groove = 1 - Math.min(1, Math.abs(fold(x / 100, y / 100, (gap + h) / 100)) / 0.42);
91 groove = groove * groove * (3 - 2 * groove);
92 if (groove > 0.45 && Math.random() < 0.95) continue;
93 h = Math.max(0, h - groove * 22);
94 z = side * (gap + h);
95 sh = (1 - groove) * Math.min(1, ds / 14 + 0.25);
96 } else if (r < 0.95) {
97 const q = pick(cbl); x = q.x; y = q.y;
98 const f = Math.abs(Math.sin((y - 0.12 * x) * 0.42));
99 if (f < 0.28 && Math.random() < 0.8) continue;
100 z = (Math.random() < 0.5 ? -1 : 1) * 96 * Math.pow(cbl.D[q.p] / cbl.max, 0.5);
101 sh = 0.25 + f * 0.5;
102 } else {
103 const q = pick(stem); x = q.x; y = q.y;
104 z = (Math.random() < 0.5 ? -1 : 1) * 22 * Math.pow(stem.D[q.p] / stem.max, 0.5);
105 sh = 0.3;
106 }
107 out[i * 3] = (x - 210) * k; out[i * 3 + 1] = -(y - 160) * k; out[i * 3 + 2] = z * k; shade[i] = sh; i++;
108 }
109 return { points: out, shade };
110}
111
112// Sample points from anything drawn on a canvas, with a little depth
113export function drawPts(draw: Draw, m: number) {
114 const S = 360, c = document.createElement("canvas"); c.width = c.height = S;
115 const g = c.getContext("2d")!; g.fillStyle = "#fff"; g.strokeStyle = "#fff"; draw(g, S);
116 const d = g.getImageData(0, 0, S, S).data, px: number[] = [];
117 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);
118 const out = new Float32Array(m * 3), n = px.length / 2;
119 for (let i = 0; i < m; i++) {
120 const k = ((Math.random() * n) | 0) * 2;
121 out[i * 3] = (px[k] + Math.random() * 2) / S - 0.5;
122 out[i * 3 + 1] = 0.5 - (px[k + 1] + Math.random() * 2) / S;
123 out[i * 3 + 2] = (Math.random() - 0.5) * 0.14;
124 }
125 return out;
126}
127
128export const glyph = (txt: string, font: string): Draw => (g, S) => {
129 g.font = `600 ${S * 0.46}px ${font}, ui-monospace, Menlo, monospace`;
130 g.textAlign = "center"; g.textBaseline = "middle"; g.fillText(txt, S / 2, S / 2);
131};
132
133export const arrow: Draw = (g, S) => {
134 const k = S * 0.04, ox = S / 2 - 6.5 * k, oy = S / 2 - 9.8 * k;
135 const p = [[0, 0], [0, 17], [4.4, 13.2], [7.4, 19.6], [10.4, 18.3], [7.5, 12], [13, 12]];
136 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();
137};
138
139export const film: Draw = (g, S) => {
140 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;
141 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();
142 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();
143};
144
145// A big cloud and a small one, for the hosted tool vs Runpod comparison
146export function clusterPts(m: number) {
147 const out = new Float32Array(m * 3);
148 for (let i = 0; i < m; i++) {
149 const big = i < m * 0.86, d = unit(), R = big ? 0.36 : 0.085, k = R * (1 - Math.pow(Math.random(), 3) * 0.65);
150 const cx = big ? -0.1 : 0.42, cy = big ? 0.06 : -0.3;
151 out[i * 3] = cx + d[0] * k; out[i * 3 + 1] = cy + d[1] * k; out[i * 3 + 2] = d[2] * k;
152 }
153 return out;
154}
155
156export { galaxyPts } from "./galaxy-shape";