The best way to control Claude Code, Codex and more from any computer or phone
| 1 | /** |
| 2 | * Exact Sri Yantra construction shared by the section background patterns |
| 3 | * (`scripts/generate-yantra-patterns.ts` → `public/patterns/`) and the Open |
| 4 | * Graph cards (`OgCard.tsx`). |
| 5 | * |
| 6 | * Geometry (unit circle, y up, symmetric about the vertical axis): |
| 7 | * |
| 8 | * - Nine triangles: four pointing up (U1–U4), five pointing down (D1–D5). |
| 9 | * - U1 and D1 are inscribed: all three corners touch the circle. |
| 10 | * - Every other apex sits on another triangle's base (seven on-axis points). |
| 11 | * - 24 off-axis points where three lines meet exactly (12 per half), which |
| 12 | * with the 9 on-axis points gives the canonical 33 marmas. |
| 13 | * - The bindu sits at the centroid of the innermost triangle. |
| 14 | * |
| 15 | * The concurrency system leaves a few degrees of freedom, so the solver starts |
| 16 | * from the proportions of the classic drawing and projects onto the nearest |
| 17 | * exact solution (min-norm Gauss-Newton). Importing this module throws if any |
| 18 | * concurrency misses by more than 1e-9. |
| 19 | * |
| 20 | * Reference: https://srilalithatripurasundari.wordpress.com/home/sri-chakra-or-shri-yantra/sri-yantra-drawing-procedure/method/geomentry-of-sri-yantra/ |
| 21 | */ |
| 22 | |
| 23 | // --------------------------------------------------------------------------- |
| 24 | // Construction |
| 25 | // --------------------------------------------------------------------------- |
| 26 | |
| 27 | /** |
| 28 | * Parameters: nine horizontal levels L1 > … > L9 (each is one triangle's base) |
| 29 | * followed by the half-widths of the seven non-inscribed triangles. Starting |
| 30 | * values are measured from the classic drawing. |
| 31 | */ |
| 32 | const REFERENCE = [ |
| 33 | 0.773, 0.536, 0.343, 0.215, 0.099, -0.077, -0.202, -0.464, -0.661, |
| 34 | // U2, U3, U4, D2, D3, D4, D5 |
| 35 | 0.72, 0.322, 0.459, 0.509, 0.633, 0.322, 0.227, |
| 36 | ]; |
| 37 | |
| 38 | interface Triangle { |
| 39 | name: string; |
| 40 | /** y of the apex (on the vertical axis). */ |
| 41 | apex: number; |
| 42 | /** y of the horizontal base. */ |
| 43 | base: number; |
| 44 | /** Half-width of the base. */ |
| 45 | half: number; |
| 46 | } |
| 47 | |
| 48 | function triangles(p: number[]): Triangle[] { |
| 49 | const L = (i: number) => p[i - 1]; |
| 50 | const w = (i: number) => p[9 + i]; |
| 51 | const inscribed = (y: number) => Math.sqrt(1 - y * y); |
| 52 | return [ |
| 53 | { name: "U1", apex: 1, base: L(7), half: inscribed(L(7)) }, |
| 54 | { name: "U2", apex: L(1), base: L(8), half: w(0) }, |
| 55 | { name: "U3", apex: L(2), base: L(6), half: w(1) }, |
| 56 | { name: "U4", apex: L(3), base: L(9), half: w(2) }, |
| 57 | { name: "D1", apex: -1, base: L(3), half: inscribed(L(3)) }, |
| 58 | { name: "D2", apex: L(6), base: L(1), half: w(3) }, |
| 59 | { name: "D3", apex: L(9), base: L(2), half: w(4) }, |
| 60 | { name: "D4", apex: L(8), base: L(4), half: w(5) }, |
| 61 | { name: "D5", apex: L(7), base: L(5), half: w(6) }, |
| 62 | ]; |
| 63 | } |
| 64 | |
| 65 | /** Left-half line: a triangle's base or its left side. */ |
| 66 | type Line = { x0: number; y0: number; x1: number; y1: number }; |
| 67 | |
| 68 | function lines(p: number[]): Record<string, Line> { |
| 69 | const out: Record<string, Line> = {}; |
| 70 | for (const t of triangles(p)) { |
| 71 | out[`${t.name}.base`] = { x0: -t.half, y0: t.base, x1: 0, y1: t.base }; |
| 72 | out[`${t.name}.side`] = { x0: 0, y0: t.apex, x1: -t.half, y1: t.base }; |
| 73 | } |
| 74 | return out; |
| 75 | } |
| 76 | |
| 77 | /** The 12 off-axis triple points of the left half (mirrored on the right). */ |
| 78 | const CONCURRENT: [string, string, string][] = [ |
| 79 | ["U1.side", "D2.side", "D3.base"], |
| 80 | ["U1.side", "D3.side", "D1.base"], |
| 81 | ["U2.side", "D2.side", "D1.base"], |
| 82 | ["U2.side", "D4.side", "D4.base"], |
| 83 | ["U3.side", "D2.side", "D4.base"], |
| 84 | ["U3.side", "D5.side", "D5.base"], |
| 85 | ["U4.side", "D2.side", "D5.base"], |
| 86 | ["U3.side", "U3.base", "D3.side"], |
| 87 | ["U4.side", "D4.side", "U3.base"], |
| 88 | ["U2.side", "D1.side", "U1.base"], |
| 89 | ["U4.side", "D3.side", "U1.base"], |
| 90 | ["U4.side", "D1.side", "U2.base"], |
| 91 | ]; |
| 92 | |
| 93 | function intersect(a: Line, b: Line): [number, number] { |
| 94 | const dxa = a.x1 - a.x0; |
| 95 | const dya = a.y1 - a.y0; |
| 96 | const dxb = b.x1 - b.x0; |
| 97 | const dyb = b.y1 - b.y0; |
| 98 | const u = ((b.x0 - a.x0) * dyb - (b.y0 - a.y0) * dxb) / (dxa * dyb - dya * dxb); |
| 99 | return [a.x0 + u * dxa, a.y0 + u * dya]; |
| 100 | } |
| 101 | |
| 102 | function distance(l: Line, [x, y]: [number, number]): number { |
| 103 | const dx = l.x1 - l.x0; |
| 104 | const dy = l.y1 - l.y0; |
| 105 | return ((x - l.x0) * dy - (y - l.y0) * dx) / Math.hypot(dx, dy); |
| 106 | } |
| 107 | |
| 108 | function residuals(p: number[]): number[] { |
| 109 | const L = lines(p); |
| 110 | return CONCURRENT.map(([a, b, c]) => distance(L[c], intersect(L[a], L[b]))); |
| 111 | } |
| 112 | |
| 113 | function solveLinear(A: number[][], b: number[]): number[] { |
| 114 | const n = b.length; |
| 115 | const M = A.map((row, i) => [...row, b[i]]); |
| 116 | for (let c = 0; c < n; c++) { |
| 117 | let pivot = c; |
| 118 | for (let r = c + 1; r < n; r++) if (Math.abs(M[r][c]) > Math.abs(M[pivot][c])) pivot = r; |
| 119 | [M[c], M[pivot]] = [M[pivot], M[c]]; |
| 120 | for (let r = 0; r < n; r++) { |
| 121 | if (r === c) continue; |
| 122 | const f = M[r][c] / M[c][c]; |
| 123 | for (let k = c; k <= n; k++) M[r][k] -= f * M[c][k]; |
| 124 | } |
| 125 | } |
| 126 | return M.map((row, i) => row[n] / row[i]); |
| 127 | } |
| 128 | |
| 129 | /** Projects `start` onto the solution set, staying as close to it as possible. */ |
| 130 | function solve(start: number[]): number[] { |
| 131 | let p = [...start]; |
| 132 | for (let iteration = 0; iteration < 100; iteration++) { |
| 133 | const r = residuals(p); |
| 134 | if (Math.max(...r.map(Math.abs)) < 1e-14) break; |
| 135 | const eps = 1e-7; |
| 136 | const J = r.map(() => Array.from({ length: p.length }, () => 0)); |
| 137 | for (let c = 0; c < p.length; c++) { |
| 138 | const q = [...p]; |
| 139 | q[c] += eps; |
| 140 | const rq = residuals(q); |
| 141 | for (let i = 0; i < r.length; i++) J[i][c] = (rq[i] - r[i]) / eps; |
| 142 | } |
| 143 | const JJt = J.map((a) => J.map((b) => a.reduce((s, v, k) => s + v * b[k], 0))); |
| 144 | const y = solveLinear(JJt, r); |
| 145 | p = p.map((v, c) => v - J.reduce((s, row, i) => s + row[c] * y[i], 0)); |
| 146 | } |
| 147 | const worst = Math.max(...residuals(p).map(Math.abs)); |
| 148 | if (!(worst < 1e-9)) |
| 149 | throw new Error(`Sri Yantra construction did not converge (max miss ${worst})`); |
| 150 | return p; |
| 151 | } |
| 152 | |
| 153 | const params = solve(REFERENCE); |
| 154 | const TRIANGLES = triangles(params); |
| 155 | |
| 156 | /** Largest concurrency miss of the solved construction. */ |
| 157 | export const MAX_MISS = Math.max(...residuals(params).map(Math.abs)); |
| 158 | const LINES = lines(params); |
| 159 | |
| 160 | /** The 33 marmas: 24 off-axis triple points plus 9 on-axis apexes. */ |
| 161 | export const MARMAS: [number, number][] = [ |
| 162 | ...CONCURRENT.flatMap(([a, b]) => { |
| 163 | const [x, y] = intersect(LINES[a], LINES[b]); |
| 164 | return [ |
| 165 | [x, y], |
| 166 | [-x, y], |
| 167 | ] as [number, number][]; |
| 168 | }), |
| 169 | ...TRIANGLES.map((t) => [0, t.apex] as [number, number]), |
| 170 | ]; |
| 171 | |
| 172 | /** |
| 173 | * Innermost triangle: points down, bounded by the D5 base and the two D2 |
| 174 | * sides, with its apex at D2's apex. The bindu is its centroid. |
| 175 | */ |
| 176 | const BINDU_Y = (2 * TRIANGLES[8].base + TRIANGLES[5].apex) / 3; |
| 177 | |
| 178 | // --------------------------------------------------------------------------- |
| 179 | // Drawing |
| 180 | // --------------------------------------------------------------------------- |
| 181 | |
| 182 | /** Enclosure radii and bhupura dimensions, in units of the triangle circle. */ |
| 183 | const RINGS = { |
| 184 | /** Circle the eight petals stand on. */ |
| 185 | lotus8: [1.04, 1.3], |
| 186 | /** Circle between the lotus rings. */ |
| 187 | between: 1.33, |
| 188 | lotus16: [1.36, 1.6], |
| 189 | /** The three girdles (trivrtta). */ |
| 190 | girdles: [1.64, 1.67, 1.7], |
| 191 | /** Outer bhupura line: half-side and T-gate, plus the inward spacing of its three lines. */ |
| 192 | bhupura: { half: 1.84, stem: 0.24, stemDepth: 0.1, bar: 0.4, barDepth: 0.26, spacing: 0.04 }, |
| 193 | }; |
| 194 | |
| 195 | const SIZE = 1000; |
| 196 | const C = SIZE / 2; |
| 197 | const fmt = (n: number) => Number(n.toFixed(2)).toString(); |
| 198 | |
| 199 | interface View { |
| 200 | /** Pixels per unit (triangle circle radius). */ |
| 201 | scale: number; |
| 202 | } |
| 203 | |
| 204 | function point(v: View, x: number, y: number): string { |
| 205 | return `${fmt(C + x * v.scale)},${fmt(C - y * v.scale)}`; |
| 206 | } |
| 207 | |
| 208 | function circle(v: View, r: number, x = 0, y = 0, fill = false): string { |
| 209 | const [px, py] = point(v, x, y).split(","); |
| 210 | return `<circle cx="${px}" cy="${py}" r="${fmt(r * v.scale)}"${fill ? ` fill="currentColor" stroke="none"` : ""}/>`; |
| 211 | } |
| 212 | |
| 213 | function trianglesSvg(v: View): string { |
| 214 | return TRIANGLES.map( |
| 215 | (t) => |
| 216 | `<polygon points="${point(v, 0, t.apex)} ${point(v, -t.half, t.base)} ${point(v, t.half, t.base)}"/>`, |
| 217 | ).join(""); |
| 218 | } |
| 219 | |
| 220 | function polar(v: View, r: number, angle: number): string { |
| 221 | // angle 0 = straight up, clockwise |
| 222 | return point(v, r * Math.sin(angle), r * Math.cos(angle)); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Left edge of a petal in petal-local coordinates: `t` runs from the base |
| 227 | * circle (0) to the tip (1), `u` is the angular offset from the petal's center |
| 228 | * line in units of the petal's angular width. The edge leaves the base along |
| 229 | * the wedge boundary (so neighbors touch without overlapping), swells into a |
| 230 | * rounded shoulder, then curves inward to a pointed tip. |
| 231 | */ |
| 232 | const PETAL_EDGE: [number, number][] = [ |
| 233 | [0, -0.5], |
| 234 | [0.25, -0.5], |
| 235 | [0.42, -0.45], |
| 236 | [0.5, -0.33], |
| 237 | [0.58, -0.21], |
| 238 | [0.8, -0.04], |
| 239 | [1, 0], |
| 240 | ]; |
| 241 | |
| 242 | /** |
| 243 | * A ring of `n` petals standing on radius r0 with tips at r1. A cusp sits on |
| 244 | * the vertical axis so the ring shares the triangles' mirror symmetry. |
| 245 | */ |
| 246 | function lotus(v: View, n: number, r0: number, r1: number): string { |
| 247 | const step = (2 * Math.PI) / n; |
| 248 | const d = r1 - r0; |
| 249 | let out = ""; |
| 250 | for (let i = 0; i < n; i++) { |
| 251 | const mid = (i + 0.5) * step; |
| 252 | const at = ([t, u]: [number, number], side: 1 | -1) => |
| 253 | polar(v, r0 + t * d, mid + side * u * step); |
| 254 | const left = PETAL_EDGE.map((p) => at(p, 1)); |
| 255 | const right = PETAL_EDGE.map((p) => at(p, -1)).reverse(); |
| 256 | out += |
| 257 | `<path d="M${left[0]} C${left[1]} ${left[2]} ${left[3]} C${left[4]} ${left[5]} ${left[6]}` + |
| 258 | ` C${right[1]} ${right[2]} ${right[3]} C${right[4]} ${right[5]} ${right[6]}"/>`; |
| 259 | } |
| 260 | return out; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * One square line of the bhupura with a T-shaped gate on each side, offset |
| 265 | * inward from the outer line by `inset` so the three lines stay parallel. |
| 266 | */ |
| 267 | function bhupuraLine(v: View, inset: number): string { |
| 268 | const { half: H, stem, stemDepth, bar, barDepth } = RINGS.bhupura; |
| 269 | const half = H - inset; |
| 270 | const s = stem - inset; |
| 271 | const b = bar - inset; |
| 272 | const d1 = H + stemDepth + inset; |
| 273 | const d2 = H + stemDepth + barDepth - inset; |
| 274 | // top side, left to right, in (along, out) coordinates |
| 275 | const side: [number, number][] = [ |
| 276 | [-half, half], |
| 277 | [-s, half], |
| 278 | [-s, d1], |
| 279 | [-b, d1], |
| 280 | [-b, d2], |
| 281 | [b, d2], |
| 282 | [b, d1], |
| 283 | [s, d1], |
| 284 | [s, half], |
| 285 | ]; |
| 286 | const rotations: ((a: number, o: number) => [number, number])[] = [ |
| 287 | (a, o) => [a, o], |
| 288 | (a, o) => [o, -a], |
| 289 | (a, o) => [-a, -o], |
| 290 | (a, o) => [-o, a], |
| 291 | ]; |
| 292 | const pts = rotations.flatMap((rot) => side.map(([a, o]) => rot(a, o))); |
| 293 | return `<polygon points="${pts.map(([x, y]) => point(v, x, y)).join(" ")}"/>`; |
| 294 | } |
| 295 | |
| 296 | function bhupura(v: View): string { |
| 297 | return [0, 1, 2].map((i) => bhupuraLine(v, i * RINGS.bhupura.spacing)).join(""); |
| 298 | } |
| 299 | |
| 300 | function enclosures(v: View, { lotuses = true, girdles = true } = {}): string { |
| 301 | let out = circle(v, 1) + circle(v, RINGS.lotus8[0]); |
| 302 | if (lotuses) { |
| 303 | out += lotus(v, 8, RINGS.lotus8[0], RINGS.lotus8[1]); |
| 304 | out += circle(v, RINGS.between) + circle(v, RINGS.lotus16[0]); |
| 305 | out += lotus(v, 16, RINGS.lotus16[0], RINGS.lotus16[1]); |
| 306 | } |
| 307 | if (girdles) out += RINGS.girdles.map((r) => circle(v, r)).join(""); |
| 308 | return out; |
| 309 | } |
| 310 | |
| 311 | const bindu = (v: View, r = 0.012) => circle(v, r, 0, BINDU_Y, true); |
| 312 | |
| 313 | /** Outer extent of the bhupura gates, in units. */ |
| 314 | const OUTER = RINGS.bhupura.half + RINGS.bhupura.stemDepth + RINGS.bhupura.barDepth; |
| 315 | |
| 316 | export interface PatternStyle { |
| 317 | /** Line and bindu color. Default black (the site uses the SVGs as masks). */ |
| 318 | color?: string; |
| 319 | /** Stroke width in the 1000-unit viewBox. Default 1.1. */ |
| 320 | strokeWidth?: number; |
| 321 | } |
| 322 | |
| 323 | const BODIES = { |
| 324 | // The complete yantra: bhupura, girdles, both lotus rings, triangles, bindu. |
| 325 | "yantra-1": (() => { |
| 326 | const v = { scale: 490 / OUTER }; |
| 327 | return bhupura(v) + enclosures(v) + trianglesSvg(v) + bindu(v); |
| 328 | })(), |
| 329 | // Lotus rings around the triangles. |
| 330 | "yantra-2": (() => { |
| 331 | const v = { scale: 490 / RINGS.girdles[2] }; |
| 332 | return enclosures(v) + trianglesSvg(v) + bindu(v); |
| 333 | })(), |
| 334 | // The nine interlocking triangles alone. |
| 335 | "yantra-3": (() => { |
| 336 | const v = { scale: 470 }; |
| 337 | return circle(v, 1) + trianglesSvg(v) + bindu(v); |
| 338 | })(), |
| 339 | // Outer enclosures: bhupura and girdles around the triangle circle. |
| 340 | "yantra-4": (() => { |
| 341 | const v = { scale: 490 / OUTER }; |
| 342 | return bhupura(v) + enclosures(v, { lotuses: false }) + trianglesSvg(v); |
| 343 | })(), |
| 344 | // The lotus rings and girdles alone, around a central bindu. |
| 345 | "yantra-5": (() => { |
| 346 | const v = { scale: 490 / RINGS.girdles[2] }; |
| 347 | return enclosures(v) + circle(v, 0.02, 0, 0, true); |
| 348 | })(), |
| 349 | // Construction view: base chords across the circle, axis, and the 33 marmas. |
| 350 | "yantra-6": (() => { |
| 351 | const v = { scale: 470 }; |
| 352 | const levels = [...new Set(TRIANGLES.map((t) => t.base))]; |
| 353 | const chords = levels |
| 354 | .map((y) => { |
| 355 | const x = Math.sqrt(1 - y * y); |
| 356 | return `<line x1="${point(v, -x, y).split(",")[0]}" y1="${point(v, -x, y).split(",")[1]}" x2="${point(v, x, y).split(",")[0]}" y2="${point(v, x, y).split(",")[1]}" stroke-dasharray="4 6"/>`; |
| 357 | }) |
| 358 | .join(""); |
| 359 | const axis = `<line x1="${C}" y1="${C - 470}" x2="${C}" y2="${C + 470}" stroke-dasharray="4 6"/>`; |
| 360 | const marmas = MARMAS.map(([x, y]) => circle(v, 0.018, x, y)).join(""); |
| 361 | return circle(v, 1) + chords + axis + trianglesSvg(v) + marmas + bindu(v); |
| 362 | })(), |
| 363 | } satisfies Record<string, string>; |
| 364 | |
| 365 | export type PatternName = keyof typeof BODIES; |
| 366 | export const PATTERN_NAMES = Object.keys(BODIES) as PatternName[]; |
| 367 | |
| 368 | /** One background pattern as a standalone 1000×1000 SVG document. */ |
| 369 | export function sriYantraPattern( |
| 370 | name: PatternName, |
| 371 | { color = "#000", strokeWidth = 1.1 }: PatternStyle = {}, |
| 372 | ): string { |
| 373 | return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${SIZE} ${SIZE}" fill="none" stroke="${color}" color="${color}" stroke-width="${strokeWidth}" stroke-linejoin="round">${BODIES[name]}</svg>\n`; |
| 374 | } |