Download Why-plotters/vector-field-why-plotter.html from perrabyte/unifying_electro_magnetism: direct link, hf CLI and curl.
- Browser
- Download file 34.8 kB
-
https://huggingface.co/datasets/perrabyte/unifying_electro_magnetism/resolve/main/Why-plotters/vector-field-why-plotter.html
- Command line
-
hf download hf://datasets/perrabyte/unifying_electro_magnetism/Why-plotters/vector-field-why-plotter.html
-
curl -L -o vector-field-why-plotter.html https://huggingface.co/datasets/perrabyte/unifying_electro_magnetism/resolve/main/Why-plotters/vector-field-why-plotter.html
34.8 kB
| <html lang="en"> | |
| <head> | |
| <meta charset="utf-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1"> | |
| <title>Why does the field point this way? β Vector field analyzer</title> | |
| <style> | |
| :root { | |
| --bg: #0a0a12; | |
| --panel: #12121e; | |
| --border: #2a2a3a; | |
| --text: #e8e8f0; | |
| --dim: #8888a0; | |
| --accent: #6cf; | |
| --good: #4caf50; | |
| --bad: #ff5566; | |
| --warn: #ffb454; | |
| } | |
| * { box-sizing: border-box; } | |
| body { | |
| margin: 0; | |
| background: var(--bg); | |
| color: var(--text); | |
| font-family: ui-sans-serif, system-ui, -apple-system, sans-serif; | |
| font-size: 14px; | |
| line-height: 1.55; | |
| } | |
| header { padding: 16px 24px; border-bottom: 1px solid var(--border); } | |
| header h1 { margin: 0 0 4px; font-size: 18px; font-weight: 600; } | |
| header p { margin: 0; color: var(--dim); font-size: 13px; max-width: 900px; } | |
| main { | |
| display: grid; | |
| grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); | |
| gap: 16px; | |
| padding: 16px; | |
| max-width: 1440px; | |
| margin: 0 auto; | |
| } | |
| @media (max-width: 940px) { main { grid-template-columns: 1fr; } } | |
| .panel { | |
| background: var(--panel); | |
| border: 1px solid var(--border); | |
| border-radius: 8px; | |
| overflow: hidden; | |
| } | |
| .panel-header { | |
| padding: 9px 14px; | |
| border-bottom: 1px solid var(--border); | |
| display: flex; | |
| align-items: center; | |
| justify-content: space-between; | |
| gap: 10px; | |
| flex-wrap: wrap; | |
| } | |
| .panel-header h2 { | |
| margin: 0; font-size: 12px; font-weight: 600; | |
| text-transform: uppercase; letter-spacing: 0.06em; color: var(--dim); | |
| } | |
| .controls { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; } | |
| select, input[type="text"] { | |
| background: #0e0e18; | |
| color: var(--text); | |
| border: 1px solid var(--border); | |
| border-radius: 4px; | |
| padding: 4px 8px; | |
| font-size: 12px; | |
| font-family: ui-monospace, monospace; | |
| } | |
| select:focus, input:focus { outline: 1px solid var(--accent); } | |
| .canvas-wrap { position: relative; background: #06060c; line-height: 0; } | |
| canvas { display: block; width: 100%; height: auto; touch-action: none; } | |
| #field { cursor: crosshair; } | |
| .overlay { | |
| position: absolute; inset: 0; | |
| display: flex; align-items: center; justify-content: center; | |
| background: rgba(0,0,0,0.55); color: var(--dim); font-size: 13px; | |
| pointer-events: none; opacity: 0; transition: opacity 0.2s; | |
| } | |
| .overlay.show { opacity: 1; } | |
| .readout { | |
| padding: 12px 14px; | |
| font-size: 12.5px; | |
| border-top: 1px solid var(--border); | |
| display: grid; | |
| grid-template-columns: repeat(2, minmax(0, 1fr)); | |
| gap: 6px 14px; | |
| font-family: ui-monospace, monospace; | |
| } | |
| .readout .k { color: var(--dim); } | |
| .readout .v { color: var(--text); } | |
| #explanation { padding: 14px 16px; font-size: 13px; max-height: 520px; overflow-y: auto; } | |
| #explanation h3 { margin: 0 0 10px; font-size: 13px; color: var(--accent); font-weight: 600; } | |
| #explanation p { margin: 8px 0; } | |
| .verdict { | |
| padding: 8px 12px; border-radius: 4px; margin: 10px 0; | |
| font-weight: 600; font-size: 13px; | |
| } | |
| .verdict.bad { background: rgba(255,85,102,0.12); color: #ff8a99; border-left: 3px solid #ff5566; } | |
| .verdict.good { background: rgba(76,175,80,0.12); color: #8fdc92; border-left: 3px solid #4caf50; } | |
| .verdict.warn { background: rgba(255,180,84,0.12); color: #ffcf8a; border-left: 3px solid #ffb454; } | |
| #explanation table { | |
| width: 100%; border-collapse: collapse; | |
| font-family: ui-monospace, monospace; font-size: 11.5px; margin: 8px 0; | |
| } | |
| #explanation th, #explanation td { | |
| text-align: left; padding: 3px 6px; border-bottom: 1px solid var(--border); | |
| } | |
| #explanation th { color: var(--dim); font-weight: 500; } | |
| .jac { | |
| font-family: ui-monospace, monospace; font-size: 12.5px; | |
| background: #0e0e18; border: 1px solid var(--border); border-radius: 4px; | |
| padding: 8px 10px; margin: 8px 0; display: inline-block; line-height: 1.9; | |
| } | |
| .note { | |
| color: var(--dim); font-size: 12px; border-top: 1px solid var(--border); | |
| padding-top: 10px; margin-top: 14px; line-height: 1.6; | |
| } | |
| .hint { | |
| padding: 9px 14px; font-size: 11.5px; color: var(--dim); | |
| border-top: 1px solid var(--border); background: rgba(255,255,255,0.02); | |
| line-height: 1.5; | |
| } | |
| .hint b { color: var(--text); } | |
| .legend { display: flex; gap: 12px; flex-wrap: wrap; font-size: 11px; color: var(--dim); } | |
| .legend span { display: inline-flex; align-items: center; gap: 5px; } | |
| .swatch { width: 10px; height: 10px; border-radius: 2px; display: inline-block; } | |
| kbd { | |
| background: #1e1e2e; border: 1px solid var(--border); border-radius: 3px; | |
| padding: 1px 5px; font-size: 10.5px; font-family: ui-monospace, monospace; | |
| } | |
| .crosshair-legend { | |
| position: absolute; bottom: 8px; left: 8px; font-size: 10.5px; | |
| color: var(--dim); background: rgba(0,0,0,0.5); padding: 3px 7px; | |
| border-radius: 4px; pointer-events: none; font-family: ui-monospace, monospace; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <header> | |
| <h1>Why does the field point this way?</h1> | |
| <p>Pick a vector field. Hover any point to see the local vector, the numerically estimated Jacobian, and a plain-language diagnosis: is this point a source, a sink, a rotation, a saddle? The color shows the direction of the field; the arrows show magnitude and heading.</p> | |
| </header> | |
| <main> | |
| <div class="panel"> | |
| <div class="panel-header"> | |
| <h2>Vector field</h2> | |
| <div class="controls"> | |
| <select id="preset"></select> | |
| <input type="text" id="custom" size="26" placeholder="f(x,y)=..., g(x,y)=..." style="display:none"> | |
| </div> | |
| </div> | |
| <div class="canvas-wrap"> | |
| <canvas id="field" width="640" height="640"></canvas> | |
| <div class="overlay" id="renderingOverlay">Renderingβ¦</div> | |
| <div class="crosshair-legend" id="coordReadout">hover the field</div> | |
| </div> | |
| <div class="hint"> | |
| <b>Arrows</b> = direction + relative magnitude. <b>Color</b> = heading (hue) & magnitude (brightness). | |
| Scroll to zoom, drag to pan, double-click to reset. <kbd>Shift</kbd>+scroll changes arrow density. | |
| </div> | |
| <div class="readout" id="readout"> | |
| <div class="k">f(x,y)</div><div class="v" id="rv-f">β</div> | |
| <div class="k">g(x,y)</div><div class="v" id="rv-g">β</div> | |
| <div class="k">div F = βf/βx + βg/βy</div><div class="v" id="rv-div">β</div> | |
| <div class="k">curl F = βg/βx β βf/βy</div><div class="v" id="rv-curl">β</div> | |
| <div class="k">speed |F|</div><div class="v" id="rv-mag">β</div> | |
| <div class="k">det J = Ξ»βΞ»β</div><div class="v" id="rv-det">β</div> | |
| <div class="k">trace J = Ξ»β+Ξ»β</div><div class="v" id="rv-trace">β</div> | |
| <div class="k">eigenvalues</div><div class="v" id="rv-eig">β</div> | |
| </div> | |
| </div> | |
| <div class="panel"> | |
| <div class="panel-header"> | |
| <h2>Local analysis</h2> | |
| <div class="controls legend"> | |
| <span><i class="swatch" style="background:#4caf50"></i>outward</span> | |
| <span><i class="swatch" style="background:#ff5566"></i>inward</span> | |
| <span><i class="swatch" style="background:#6cf"></i>field</span> | |
| </div> | |
| </div> | |
| <div class="canvas-wrap"> | |
| <canvas id="zoom" width="640" height="480"></canvas> | |
| </div> | |
| <div id="explanation"> | |
| <p style="color: var(--dim)">Hover over the field to analyze a point.</p> | |
| </div> | |
| </div> | |
| </main> | |
| <script> | |
| ; | |
| // ---------------------------------------------------------------- config | |
| const W = 640, H = 640; | |
| const ZW = 640, ZH = 480; | |
| const GLYPH_GRID_BASE = 26; // arrows across when density = 1 | |
| const EPS = 1e-5; // finite-difference step | |
| const fieldCanvas = document.getElementById("field"); | |
| const fieldCtx = fieldCanvas.getContext("2d"); | |
| const zoomCanvas = document.getElementById("zoom"); | |
| const zoomCtx = zoomCanvas.getContext("2d"); | |
| const explanationEl = document.getElementById("explanation"); | |
| const overlayEl = document.getElementById("renderingOverlay"); | |
| const coordReadout = document.getElementById("coordReadout"); | |
| const presetSel = document.getElementById("preset"); | |
| const customInput = document.getElementById("custom"); | |
| // ---------------------------------------------------------------- view | |
| let view = { xmin: -3, xmax: 3, ymin: -3, ymax: 3 }; | |
| const HOME_VIEW = { ...view }; | |
| let density = 1.0; | |
| let hoverP = null; | |
| let renderToken = 0; | |
| let dragging = false; | |
| let dragStart = null; | |
| // ---------------------------------------------------------------- presets | |
| // Each field is a pair of functions f(x,y), g(x,y). | |
| const PRESETS = { | |
| "source β radial out": { f: (x, y) => x, g: (x, y) => y }, | |
| "sink β radial in": { f: (x, y) => -x, g: (x, y) => -y }, | |
| "rotation β CCW": { f: (x, y) => -y, g: (x, y) => x }, | |
| "rotation + spiral out": { f: (x, y) => -y + 0.3 * x, g: (x, y) => x + 0.3 * y }, | |
| "saddle β hyperbolic": { f: (x, y) => x, g: (x, y) => -y }, | |
| "shear β parallel slide": { f: (x, y) => y, g: (x, y) => 0 }, | |
| "uniform β constant": { f: (x, y) => 1, g: (x, y) => 0.5 }, | |
| "dipole β two charges": { f: (x, y) => cx(x, y), g: (x, y) => cy(x, y) }, | |
| "quadrupole β 4 poles": { f: (x, y) => x * x - y * y, g: (x, y) => 2 * x * y }, | |
| "Duffing β nonlinear": { f: (x, y) => y, g: (x, y) => x - x * x * x - 0.3 * y }, | |
| "pendulum phase flow": { f: (x, y) => y, g: (x, y) => -Math.sin(x) - 0.2 * y }, | |
| "van der Pol": { f: (x, y) => y, g: (x, y) => (1 - x * x) * y - x }, | |
| "custom (type below)": { f: (x, y) => x, g: (x, y) => y }, | |
| }; | |
| // dipole helper: sum of a source at (-1.5,0) and a sink at (1.5,0) | |
| function cx(x, y) { | |
| const dx1 = x + 1.5, dy1 = y, r1 = dx1 * dx1 + dy1 * dy1 + 0.25; | |
| const dx2 = x - 1.5, dy2 = y, r2 = dx2 * dx2 + dy2 * dy2 + 0.25; | |
| return dx1 / r1 - dx2 / r2; | |
| } | |
| function cy(x, y) { | |
| const dx1 = x + 1.5, dy1 = y, r1 = dx1 * dx1 + dy1 * dy1 + 0.25; | |
| const dx2 = x - 1.5, dy2 = y, r2 = dx2 * dx2 + dy2 * dy2 + 0.25; | |
| return dy1 / r1 - dy2 / r2; | |
| } | |
| let F = PRESETS["source β radial out"].f; | |
| let G = PRESETS["source β radial out"].g; | |
| let currentName = "source β radial out"; | |
| // ---------------------------------------------------------------- palette | |
| function lerp(a, b, t) { return a + (b - a) * t; } | |
| function headingColor(angle, mag, magMax) { | |
| // angle in radians -> hue 0..360 | |
| let a = angle / (2 * Math.PI) * 360; | |
| if (a < 0) a += 360; | |
| const t = Math.max(0, Math.min(1, mag / (magMax || 1))); | |
| const light = 25 + 45 * t; | |
| const sat = 70 + 20 * t; | |
| return `hsl(${a}, ${sat}%, ${light}%)`; | |
| } | |
| // ---------------------------------------------------------------- field eval | |
| function fieldAt(x, y) { | |
| let fx = F(x, y), fy = G(x, y); | |
| if (!isFinite(fx)) fx = 0; | |
| if (!isFinite(fy)) fy = 0; | |
| return { f: fx, g: fy }; | |
| } | |
| // numerical Jacobian [[df/dx, df/dy],[dg/dx, dg/dy]] | |
| function jacobianAt(x, y) { | |
| const h = EPS; | |
| const fp = fieldAt(x + h, y), fm = fieldAt(x - h, y); | |
| const gp = fieldAt(x, y + h), gm = fieldAt(x, y - h); | |
| return { | |
| a: (fp.f - fm.f) / (2 * h), // βf/βx | |
| b: (gp.f - gm.f) / (2 * h), // βf/βy | |
| c: (fp.g - fm.g) / (2 * h), // βg/βx | |
| d: (gp.g - gm.g) / (2 * h), // βg/βy | |
| }; | |
| } | |
| // ---------------------------------------------------------------- arrow glyphs | |
| function drawArrow(ctx, x0, y0, x1, y1, color, width, headScale) { | |
| const dx = x1 - x0, dy = y1 - y0; | |
| const len = Math.hypot(dx, dy); | |
| if (len < 0.5) return; | |
| const ux = dx / len, uy = dy / len; | |
| const head = Math.min(Math.max(3, len * 0.35), 9 * headScale); | |
| ctx.strokeStyle = color; | |
| ctx.fillStyle = color; | |
| ctx.lineWidth = width; | |
| ctx.beginPath(); | |
| ctx.moveTo(x0, y0); | |
| ctx.lineTo(x1 - ux * head * 0.6, y1 - uy * head * 0.6); | |
| ctx.stroke(); | |
| ctx.beginPath(); | |
| ctx.moveTo(x1, y1); | |
| ctx.lineTo(x1 - ux * head - uy * head * 0.5, y1 - uy * head + ux * head * 0.5); | |
| ctx.lineTo(x1 - ux * head + uy * head * 0.5, y1 - uy * head - ux * head * 0.5); | |
| ctx.closePath(); | |
| ctx.fill(); | |
| } | |
| // ---------------------------------------------------------------- field render | |
| function toScreen(x, y) { | |
| return { | |
| sx: (x - view.xmin) / (view.xmax - view.xmin) * W, | |
| sy: (view.ymax - y) / (view.ymax - view.ymin) * H, | |
| }; | |
| } | |
| function toWorld(sx, sy) { | |
| return { | |
| x: view.xmin + (sx / W) * (view.xmax - view.xmin), | |
| y: view.ymax - (sy / H) * (view.ymax - view.ymin), | |
| }; | |
| } | |
| function renderField() { | |
| const token = ++renderToken; | |
| overlayEl.classList.add("show"); | |
| const ctx = fieldCtx; | |
| // background gradient by speed sampled coarsely | |
| const bg = ctx.createImageData(W, H); | |
| const data = bg.data; | |
| const stepPx = 4; // coarse fill | |
| // determine a magnitude scale for the background by sampling | |
| const sampleN = 30; | |
| let magMax = 0; | |
| for (let i = 0; i <= sampleN; i++) { | |
| for (let j = 0; j <= sampleN; j++) { | |
| const wx = view.xmin + (i / sampleN) * (view.xmax - view.xmin); | |
| const wy = view.ymin + (j / sampleN) * (view.ymax - view.ymin); | |
| const v = fieldAt(wx, wy); | |
| const m = Math.hypot(v.f, v.g); | |
| if (isFinite(m) && m > magMax) magMax = m; | |
| } | |
| } | |
| if (magMax === 0) magMax = 1; | |
| let yPx = 0; | |
| function stepBg() { | |
| if (token !== renderToken) return; | |
| const t0 = performance.now(); | |
| while (yPx < H && performance.now() - t0 < 12) { | |
| for (let xPx = 0; xPx < W; xPx += stepPx) { | |
| const w = toWorld(xPx, yPx); | |
| const v = fieldAt(w.x, w.y); | |
| const mag = Math.hypot(v.f, v.g); | |
| const t = Math.min(1, mag / magMax); | |
| // shade blue-ish dark -> bright by magnitude | |
| const r = Math.round(6 + 18 * t); | |
| const g = Math.round(8 + 26 * t); | |
| const b = Math.round(16 + 60 * t); | |
| for (let k = 0; k < stepPx && xPx + k < W; k++) { | |
| const idx = (yPx * W + xPx + k) * 4; | |
| data[idx] = r; data[idx + 1] = g; data[idx + 2] = b; data[idx + 3] = 255; | |
| } | |
| } | |
| // copy the same row for the block | |
| for (let dy = 1; dy < stepPx && yPx + dy < H; dy++) { | |
| const src = (yPx * W) * 4; | |
| data.copyWithin(((yPx + dy) * W) * 4, src, src + W * 4); | |
| } | |
| yPx += stepPx; | |
| } | |
| ctx.putImageData(bg, 0, 0); | |
| if (yPx < H) requestAnimationFrame(stepBg); | |
| else { | |
| drawGlyphs(magMax); | |
| overlayEl.classList.remove("show"); | |
| drawHoverMarker(); | |
| } | |
| } | |
| requestAnimationFrame(stepBg); | |
| } | |
| function drawGlyphs(magMax) { | |
| const ctx = fieldCtx; | |
| const n = Math.max(6, Math.round(GLYPH_GRID_BASE * density)); | |
| const cell = W / n; | |
| const spacing = cell; | |
| const maxLen = spacing * 0.95; | |
| // sample field to find max in view for scaling | |
| let localMax = 0, minPos = Infinity; | |
| const samples = []; | |
| for (let i = 0; i < n; i++) { | |
| for (let j = 0; j < n; j++) { | |
| const sx = (i + 0.5) * cell; | |
| const sy = (j + 0.5) * cell; | |
| const w = toWorld(sx, sy); | |
| const v = fieldAt(w.x, w.y); | |
| const m = Math.hypot(v.f, v.g); | |
| samples.push({ sx, sy, v, m, w }); | |
| if (m > localMax) localMax = m; | |
| } | |
| } | |
| if (localMax === 0) localMax = 1; | |
| // scale factor so the largest glyph is ~maxLen | |
| const scale = maxLen / localMax; | |
| // world-per-pixel to convert vector components into screen direction | |
| const wppX = (view.xmax - view.xmin) / W; | |
| const wppY = (view.ymax - view.ymin) / H; | |
| for (const s of samples) { | |
| if (s.m < 1e-9) continue; | |
| // vector in screen space: vx_screen = f / wppX, vy_screen = -g / wppY | |
| let vxs = s.v.f / wppX; | |
| let vys = -s.v.g / wppY; | |
| const slen = Math.hypot(vxs, vys); | |
| if (slen < 1e-9) continue; | |
| const drawLen = Math.min(slen * scale * wppX * (1 / wppX) * wppX, maxLen); | |
| // normalize direction, set length | |
| const ux = vxs / slen, uy = vys / slen; | |
| const L = Math.min(maxLen, (s.m / localMax) * maxLen); | |
| const x0 = s.sx - ux * L / 2; | |
| const y0 = s.sy - uy * L / 2; | |
| const x1 = s.sx + ux * L / 2; | |
| const y1 = s.sy + uy * L / 2; | |
| const angle = Math.atan2(s.v.g, s.v.f); | |
| const col = headingColor(angle, s.m, localMax); | |
| drawArrow(ctx, x0, y0, x1, y1, col, 1.4 + 0.8 * (s.m / localMax), 1); | |
| } | |
| } | |
| // ---------------------------------------------------------------- hover marker | |
| function drawHoverMarker() { | |
| if (!hoverP) return; | |
| const ctx = fieldCtx; | |
| const { sx, sy } = toScreen(hoverP.x, hoverP.y); | |
| ctx.strokeStyle = "#ffffff"; | |
| ctx.lineWidth = 1.5; | |
| ctx.beginPath(); | |
| ctx.arc(sx, sy, 9, 0, 2 * Math.PI); | |
| ctx.stroke(); | |
| ctx.beginPath(); | |
| ctx.moveTo(sx - 14, sy); ctx.lineTo(sx - 5, sy); | |
| ctx.moveTo(sx + 5, sy); ctx.lineTo(sx + 14, sy); | |
| ctx.moveTo(sx, sy - 14); ctx.lineTo(sx, sy - 5); | |
| ctx.moveTo(sx, sy + 5); ctx.lineTo(sx, sy + 14); | |
| ctx.stroke(); | |
| ctx.fillStyle = "#fff"; | |
| ctx.font = "10px ui-monospace, monospace"; | |
| ctx.fillText(`(${hoverP.x.toFixed(2)}, ${hoverP.y.toFixed(2)})`, sx + 16, sy - 10); | |
| } | |
| // ---------------------------------------------------------------- zoomed local view | |
| function drawLocalView(x, y) { | |
| const ctx = zoomCtx; | |
| ctx.clearRect(0, 0, ZW, ZH); | |
| ctx.fillStyle = "#06060c"; | |
| ctx.fillRect(0, 0, ZW, ZH); | |
| const span = 1.0; // world half-width of the zoom window | |
| const pxPerW = ZW / (2 * span); // px per world unit (x) | |
| const pyPerW = ZH / (2 * span); // px per world unit (y) | |
| const toZ = (wx, wy) => ({ | |
| sx: ZW / 2 + (wx - x) * pxPerW, | |
| sy: ZH / 2 - (wy - y) * pyPerW, | |
| }); | |
| // axes | |
| ctx.strokeStyle = "#1e1e2e"; | |
| ctx.lineWidth = 1; | |
| ctx.beginPath(); | |
| const ax = toZ(0, 0); | |
| ctx.moveTo(0, ax.sy); ctx.lineTo(ZW, ax.sy); | |
| ctx.moveTo(ax.sx, 0); ctx.lineTo(ax.sx, ZH); | |
| ctx.stroke(); | |
| ctx.fillStyle = "#3a3a4a"; | |
| ctx.font = "10px ui-monospace, monospace"; | |
| ctx.fillText("x", ZW - 16, ax.sy - 5); | |
| ctx.fillText("y", ax.sx + 5, 12); | |
| // vector field glyphs in the local window | |
| const gn = 15; | |
| let mMax = 0; | |
| const cells = []; | |
| for (let i = 0; i < gn; i++) { | |
| for (let j = 0; j < gn; j++) { | |
| const wx = x - span + ((i + 0.5) / gn) * 2 * span; | |
| const wy = y - span + ((j + 0.5) / gn) * 2 * span; | |
| const v = fieldAt(wx, wy); | |
| const m = Math.hypot(v.f, v.g); | |
| cells.push({ wx, wy, v, m }); | |
| if (m > mMax) mMax = m; | |
| } | |
| } | |
| if (mMax === 0) mMax = 1; | |
| const cellSize = Math.min(ZW / gn, ZH / gn); | |
| const maxLen = cellSize * 0.9; | |
| for (const c0 of cells) { | |
| const p = toZ(c0.wx, c0.wy); | |
| if (c0.m < 1e-9) { | |
| ctx.fillStyle = "#2a2a3a"; | |
| ctx.beginPath(); ctx.arc(p.sx, p.sy, 1.2, 0, 2 * Math.PI); ctx.fill(); | |
| continue; | |
| } | |
| const vxs = c0.v.f * (pxPerW / pyPerW) * 0; // unused | |
| // direction in screen space | |
| const ddx = c0.v.f / 1; // world x-comp | |
| const ddy = c0.v.g; | |
| let sx = ddx, sy = -ddy; | |
| const sl = Math.hypot(sx, sy); | |
| const ux = sx / sl, uy = sy / sl; | |
| const L = Math.min(maxLen, (c0.m / mMax) * maxLen); | |
| const x0 = p.sx - ux * L / 2, y0 = p.sy - uy * L / 2; | |
| const x1 = p.sx + ux * L / 2, y1 = p.sy + uy * L / 2; | |
| const angle = Math.atan2(c0.v.g, c0.v.f); | |
| drawArrow(ctx, x0, y0, x1, y1, headingColor(angle, c0.m, mMax), 1.2, 0.9); | |
| } | |
| // the point itself | |
| const p = toZ(x, y); | |
| ctx.fillStyle = "#fff"; | |
| ctx.beginPath(); ctx.arc(p.sx, p.sy, 3.5, 0, 2 * Math.PI); ctx.fill(); | |
| // local vector at the point, drawn bold | |
| const v = fieldAt(x, y); | |
| const mag = Math.hypot(v.f, v.g); | |
| const J = jacobianAt(x, y); | |
| const div = J.a + J.d; | |
| const curl = J.c - J.b; | |
| // draw the local field vector from the point | |
| if (mag > 1e-12) { | |
| const ux = v.f / mag, uy = -v.g / mag; | |
| const L = Math.min(110, 40 + 30 * Math.log10(1 + mag)); | |
| drawArrow(ctx, p.sx, p.sy, p.sx + ux * L, p.sy + uy * L, "#6cf", 2.5, 1.4); | |
| // trace streamline a little forward and backward | |
| traceStreamline(ctx, x, y, pxPerW, pyPerW, toZ, 1); | |
| traceStreamline(ctx, x, y, pxPerW, pyPerW, toZ, -1); | |
| } | |
| // divergence halo: green outward, red inward | |
| const haloC = div > 0.05 ? "rgba(76,175,80,0.85)" : div < -0.05 ? "rgba(255,85,102,0.85)" : "rgba(255,180,84,0.85)"; | |
| ctx.strokeStyle = haloC; | |
| ctx.lineWidth = 2; | |
| ctx.beginPath(); | |
| ctx.arc(p.sx, p.sy, 13, 0, 2 * Math.PI); | |
| ctx.stroke(); | |
| // curl indicator: arc showing rotational tendency | |
| if (Math.abs(curl) > 0.05) { | |
| ctx.strokeStyle = "rgba(160,120,255,0.8)"; | |
| ctx.lineWidth = 2; | |
| ctx.beginPath(); | |
| const start = curl > 0 ? -0.5 : Math.PI + 0.5; | |
| const end = curl > 0 ? Math.PI + 0.5 : -0.5; | |
| ctx.arc(p.sx, p.sy, 20, start, end, false); | |
| ctx.stroke(); | |
| // arrowhead | |
| const ang = curl > 0 ? Math.PI + 0.5 : -0.5; | |
| const hx = p.sx + 20 * Math.cos(ang), hy = p.sy + 20 * Math.sin(ang); | |
| drawArrow(ctx, hx, hy, hx + (curl > 0 ? -1 : 1) * 6, hy + 1, "rgba(160,120,255,0.9)", 2, 1); | |
| } | |
| // legend text | |
| ctx.fillStyle = "#8888a0"; | |
| ctx.font = "10.5px ui-monospace, monospace"; | |
| ctx.fillText("local window: Β±1.0 world units", 10, ZH - 10); | |
| ctx.fillText(`div=${div.toFixed(3)} curl=${curl.toFixed(3)}`, 10, ZH - 26); | |
| } | |
| function traceStreamline(ctx, x0, y0, pxPerW, pyPerW, toZ, dir) { | |
| const dt = 0.02 * dir; | |
| let x = x0, y = y0; | |
| ctx.strokeStyle = dir > 0 ? "rgba(108,204,255,0.55)" : "rgba(108,204,255,0.25)"; | |
| ctx.lineWidth = 1.6; | |
| ctx.beginPath(); | |
| let first = true; | |
| for (let i = 0; i < 120; i++) { | |
| const v = fieldAt(x, y); | |
| const m = Math.hypot(v.f, v.g); | |
| if (m < 1e-9) break; | |
| // RK2-ish step, step size scaled so movement isn't huge | |
| const sp = 0.03 / Math.max(0.2, Math.min(3, m)); | |
| const dx = (v.f / m) * sp * dir; | |
| const dy = (v.g / m) * sp * dir; | |
| const nx = x + dx, ny = y + dy; | |
| const p = toZ(nx, ny); | |
| if (first) { ctx.moveTo(p.sx, p.sy); first = false; } | |
| else ctx.lineTo(p.sx, p.sy); | |
| x = nx; y = ny; | |
| if (Math.abs(x - x0) > 1.6 || Math.abs(y - y0) > 1.6) break; | |
| } | |
| ctx.stroke(); | |
| } | |
| // ---------------------------------------------------------------- explanation | |
| function fmt(x, d = 4) { | |
| if (Math.abs(x) < 1e-12) return "0"; | |
| if (Math.abs(x) >= 1e4 || Math.abs(x) < 1e-3) return x.toExponential(2); | |
| return x.toFixed(d); | |
| } | |
| function classify(x, y, f, g, J) { | |
| const div = J.a + J.d; | |
| const curl = J.c - J.b; | |
| const det = J.a * J.d - J.b * J.c; | |
| const tr = J.a + J.d; | |
| const disc = tr * tr - 4 * det; | |
| // eigenvalues | |
| let l1, l2, eigText; | |
| if (Math.abs(disc) < 1e-9) { | |
| l1 = l2 = tr / 2; | |
| eigText = `Ξ»β = Ξ»β = ${fmt(l1, 4)}`; | |
| } else if (disc > 0) { | |
| l1 = (tr + Math.sqrt(disc)) / 2; | |
| l2 = (tr - Math.sqrt(disc)) / 2; | |
| eigText = `Ξ»β = ${fmt(l1, 4)}, Ξ»β = ${fmt(l2, 4)}`; | |
| } else { | |
| const re = tr / 2, im = Math.sqrt(-disc) / 2; | |
| l1 = re; l2 = re; | |
| eigText = `Ξ» = ${fmt(re, 4)} Β± ${fmt(im, 4)}i`; | |
| } | |
| const mag = Math.hypot(f, g); | |
| let title, klass, verdict, why; | |
| const nearZero = mag < 1e-3; | |
| if (nearZero && Math.hypot(div, curl) > 1e-3) { | |
| klass = "fixed point"; | |
| title = "Fixed point (stationary point)"; | |
| if (det < 0) title = "Saddle point"; | |
| else if (l1.imag !== undefined) { | |
| title = div > 0 ? "Spiral source (unstable focus)" : div < 0 ? "Spiral sink (stable focus)" : "Center (pure rotation)"; | |
| } else { | |
| if (Math.abs(disc) < 1e-9) title = tr < 0 ? "Stable node (degenerate)" : "Unstable node (degenerate)"; | |
| else if (div > 0) title = "Source node (unstable node)"; | |
| else if (div < 0) title = "Sink node (stable node)"; | |
| else title = "Saddle-like (det<0 or trace 0)"; | |
| } | |
| } else if (nearZero) { | |
| klass = "flat"; | |
| title = "Zero vector here"; | |
| } else { | |
| klass = "regular point"; | |
| title = "Regular point"; | |
| } | |
| // build verdict + reasoning tuned to actual numbers | |
| const reasons = []; | |
| if (Math.abs(div) > 0.05) { | |
| if (div > 0) { | |
| verdict = { type: "bad", text: `Source-like at this point β div F = ${fmt(div, 3)} > 0 (field expands)` }; | |
| reasons.push(`Positive divergence means the field pushes <i>outward</i> on average here: a tiny disk of test particles would grow in area. Since div = βf/βx + βg/βy = ${fmt(J.a, 3)} + ${fmt(J.d, 3)} = ${fmt(div, 3)}, both partials are mostly positive, so the flow stretches.`); | |
| } else { | |
| verdict = { type: "good", text: `Sink-like at this point β div F = ${fmt(div, 3)} < 0 (field converges)` }; | |
| reasons.push(`Negative divergence means the field pulls <i>inward</i> on average here: a tiny disk of test particles would shrink in area. div = ${fmt(J.a, 3)} + ${fmt(J.d, 3)} = ${fmt(div, 3)}.`); | |
| } | |
| } else if (Math.abs(curl) > 0.05) { | |
| const dir = curl > 0 ? "counter-clockwise" : "clockwise"; | |
| verdict = { type: "warn", text: `Irotational-like: curl F = ${fmt(curl, 3)} (${dir} rotation)` }; | |
| reasons.push(`Curl measures local spin. Here curl = βg/βx β βf/βy = ${fmt(J.c, 3)} β ${fmt(J.b, 3)} = ${fmt(curl, 3)}, so a small paddle wheel placed here would turn ${dir}. The field is locally rotational even though it may not look like a circle.`); | |
| } else { | |
| verdict = { type: "warn", text: `Locally trivial β div β ${fmt(div, 3)}, curl β ${fmt(curl, 3)} (very little expansion or spin here)` }; | |
| reasons.push(`Both the divergence and the curl are near zero, so at this scale the field looks like a simple translation: it mostly just carries points along without stretching or spinning them.`); | |
| } | |
| // field value explanation | |
| reasons.push(`The vector here is <b>F = (${fmt(f, 3)}, ${fmt(g, 3)})</b>, magnitude ${fmt(mag, 3)}. Its direction is atan2(g, f) = ${(Math.atan2(g, f) * 180 / Math.PI).toFixed(1)}Β°, which is why the arrow points that way.`); | |
| // fixed point extra | |
| if (klass === "fixed point") { | |
| reasons.push(`This is (or is extremely close to) an equilibrium point, because F β 0 there. The behaviour of nearby trajectories is decided entirely by the Jacobian J, whose eigenvalues are ${eigText}. Trace = ${fmt(tr, 3)}, determinant = ${fmt(det, 3)}.`); | |
| if (det < 0) { | |
| reasons.push(`Because det J < 0, the two eigenvalues have opposite signs: the point attracts along one direction and repels along another β a <b>saddle</b>. Trajectories near a saddle swing past and leave.`); | |
| } else if (Math.abs(disc) < 1e-9 && Math.abs(tr) < 1e-6) { | |
| reasons.push(`Trace β 0 and the discriminant is β 0: this is a degenerate borderline case. Numerically it is hard to pin down, so treat the classification as tentative.`); | |
| } else if (disc < 0) { | |
| reasons.push(`Because the discriminant is negative, the eigenvalues are a complex pair, so nearby trajectories <b>spiral</b>. The real part (${fmt(tr / 2, 3)}) decides stability: negative β spiral in, positive β spiral out.`); | |
| } else { | |
| reasons.push(`Both eigenvalues are real, so nearby trajectories approach or leave along two straight directions (the eigenvectors) rather than spiralling.`); | |
| } | |
| } | |
| return { title, klass, verdict, reasons, div, curl, det, tr, eigText, l1, l2, disc, mag }; | |
| } | |
| function explain(x, y) { | |
| const v = fieldAt(x, y); | |
| const J = jacobianAt(x, y); | |
| const info = classify(x, y, v.f, v.g, J); | |
| const modeNames = { name: currentName }; | |
| let html = `<h3>${currentName}</h3>`; | |
| html += `<p><b>point</b> (x, y) = (${fmt(x, 3)}, ${fmt(y, 3)})</p>`; | |
| html += `<div class="verdict ${info.verdict.type}">${info.verdict.text}</div>`; | |
| html += `<p><b>${info.title}</b>${info.klass === "regular point" ? " β the field is smooth and non-zero here." : "."}</p>`; | |
| // Jacobian box | |
| html += `<div class="jac">J = β‘ ${fmt(J.a, 3)} ${fmt(J.b, 3)} β€<br> β£ ${fmt(J.c, 3)} ${fmt(J.d, 3)} β¦</div>`; | |
| html += `<p>${info.reasons.join("</p><p>")}</p>`; | |
| html += `<table>`; | |
| html += `<tr><th>quantity</th><th>value</th><th>meaning</th></tr>`; | |
| html += `<tr><td>div F</td><td>${fmt(info.div, 3)}</td><td>${info.div > 0.05 ? "expanding" : info.div < -0.05 ? "converging" : "area-preserving"}</td></tr>`; | |
| html += `<tr><td>curl F</td><td>${fmt(info.curl, 3)}</td><td>${info.curl > 0.05 ? "CCW spin" : info.curl < -0.05 ? "CW spin" : "no spin"}</td></tr>`; | |
| html += `<tr><td>det J</td><td>${fmt(info.det, 3)}</td><td>${info.det > 0 ? "orientation kept" : info.det < 0 ? "orientation flipped" : "singular"}</td></tr>`; | |
| html += `<tr><td>trace J</td><td>${fmt(info.tr, 3)}</td><td>${info.tr > 0.05 ? "net growth" : info.tr < -0.05 ? "net decay" : "neutral"}</td></tr>`; | |
| html += `<tr><td>eigenvalues</td><td colspan="2">${info.eigText}</td></tr>`; | |
| html += `</table>`; | |
| html += `<div class="note">`; | |
| html += `<b>Why these numbers come from two derivatives.</b> The whole local story is carried by the Jacobian, the 2Γ2 matrix of partial derivatives of (f, g). Its <i>trace</i> is the divergence (area growth), its <i>antisymmetric</i> part is the curl (rotation), and its <i>determinant</i> tells you whether the map preserves or flips orientation. The eigenvalues then split behaviour into just a few cases β node, focus, saddle, center β which is why physicists can classify an entire flow by looking at a single point.`; | |
| html += `</div>`; | |
| return html; | |
| } | |
| function updateReadout(x, y) { | |
| const v = fieldAt(x, y); | |
| const J = jacobianAt(x, y); | |
| const div = J.a + J.d; | |
| const curl = J.c - J.b; | |
| const det = J.a * J.d - J.b * J.c; | |
| const tr = J.a + J.d; | |
| const disc = tr * tr - 4 * det; | |
| let eig; | |
| if (Math.abs(disc) < 1e-9) eig = `${fmt(tr / 2, 3)} (double)`; | |
| else if (disc > 0) eig = `${fmt((tr + Math.sqrt(disc)) / 2, 2)}, ${fmt((tr - Math.sqrt(disc)) / 2, 2)}`; | |
| else eig = `${fmt(tr / 2, 2)} Β± ${fmt(Math.sqrt(-disc) / 2, 2)}i`; | |
| document.getElementById("rv-f").textContent = fmt(v.f, 4); | |
| document.getElementById("rv-g").textContent = fmt(v.g, 4); | |
| document.getElementById("rv-div").textContent = fmt(div, 4); | |
| document.getElementById("rv-curl").textContent = fmt(curl, 4); | |
| document.getElementById("rv-mag").textContent = fmt(Math.hypot(v.f, v.g), 4); | |
| document.getElementById("rv-det").textContent = fmt(det, 4); | |
| document.getElementById("rv-trace").textContent = fmt(tr, 4); | |
| document.getElementById("rv-eig").textContent = eig; | |
| } | |
| // ---------------------------------------------------------------- interaction | |
| function updatePoint(x, y) { | |
| hoverP = { x, y }; | |
| drawLocalView(x, y); | |
| explanationEl.innerHTML = explain(x, y); | |
| updateReadout(x, y); | |
| coordReadout.textContent = `(${x.toFixed(3)}, ${y.toFixed(3)})`; | |
| } | |
| fieldCanvas.addEventListener("mousemove", (e) => { | |
| if (dragging) return; | |
| const rect = fieldCanvas.getBoundingClientRect(); | |
| const sx = (e.clientX - rect.left) / rect.width * W; | |
| const sy = (e.clientY - rect.top) / rect.height * H; | |
| const w = toWorld(sx, sy); | |
| updatePoint(w.x, w.y); | |
| }); | |
| fieldCanvas.addEventListener("mouseleave", () => { | |
| hoverP = null; | |
| coordReadout.textContent = "hover the field"; | |
| }); | |
| fieldCanvas.addEventListener("wheel", (e) => { | |
| e.preventDefault(); | |
| const rect = fieldCanvas.getBoundingClientRect(); | |
| const pxFrac = (e.clientX - rect.left) / rect.width; | |
| const pyFrac = (e.clientY - rect.top) / rect.height; | |
| if (e.shiftKey) { | |
| density = Math.max(0.4, Math.min(3, density * (e.deltaY < 0 ? 1.15 : 0.87))); | |
| renderField(); | |
| return; | |
| } | |
| const cx = view.xmin + pxFrac * (view.xmax - view.xmin); | |
| const cy = view.ymax - pyFrac * (view.ymax - view.ymin); | |
| const factor = e.deltaY < 0 ? 0.8 : 1.25; | |
| const nw = (view.xmax - view.xmin) * factor; | |
| const nh = (view.ymax - view.ymin) * factor; | |
| view.xmin = cx - pxFrac * nw; | |
| view.xmax = view.xmin + nw; | |
| view.ymax = cy + pyFrac * nh; | |
| view.ymin = view.ymax - nh; | |
| renderField(); | |
| }, { passive: false }); | |
| fieldCanvas.addEventListener("mousedown", (e) => { | |
| dragging = true; | |
| dragStart = { x: e.clientX, y: e.clientY, view: { ...view } }; | |
| }); | |
| window.addEventListener("mousemove", (e) => { | |
| if (!dragging) return; | |
| const rect = fieldCanvas.getBoundingClientRect(); | |
| const dx = (e.clientX - dragStart.x) / rect.width * (dragStart.view.xmax - dragStart.view.xmin); | |
| const dy = (e.clientY - dragStart.y) / rect.height * (dragStart.view.ymax - dragStart.view.ymin); | |
| view.xmin = dragStart.view.xmin - dx; | |
| view.xmax = dragStart.view.xmax - dx; | |
| view.ymin = dragStart.view.ymin + dy; | |
| view.ymax = dragStart.view.ymax + dy; | |
| renderField(); | |
| }); | |
| window.addEventListener("mouseup", () => { dragging = false; }); | |
| fieldCanvas.addEventListener("dblclick", () => { | |
| view = { ...HOME_VIEW }; | |
| renderField(); | |
| }); | |
| // ---------------------------------------------------------------- presets UI | |
| function buildPresets() { | |
| presetSel.innerHTML = ""; | |
| for (const name of Object.keys(PRESETS)) { | |
| const o = document.createElement("option"); | |
| o.value = name; | |
| o.textContent = name; | |
| presetSel.appendChild(o); | |
| } | |
| presetSel.value = currentName; | |
| } | |
| presetSel.addEventListener("change", () => { | |
| currentName = presetSel.value; | |
| customInput.style.display = currentName.startsWith("custom") ? "inline-block" : "none"; | |
| if (!currentName.startsWith("custom")) { | |
| F = PRESETS[currentName].f; | |
| G = PRESETS[currentName].g; | |
| } | |
| hoverP = null; | |
| explanationEl.innerHTML = '<p style="color: var(--dim)">Hover over the field to analyze a point.</p>'; | |
| renderField(); | |
| }); | |
| // custom parser: "f(x,y)=..., g(x,y)=..." using Function() on a whitelist of names | |
| function parseCustom(text) { | |
| // expected: f = ..., g = ... OR f(x,y)=..., g(x,y)=... | |
| const parts = text.split(",").map(s => s.trim()); | |
| let fExpr = null, gExpr = null; | |
| for (const p of parts) { | |
| let m = p.match(/^f\s*(\(x,y\))?\s*=\s*(.+)$/i); | |
| if (m) { fExpr = m[2]; continue; } | |
| m = p.match(/^g\s*(\(x,y\))?\s*=\s*(.+)$/i); | |
| if (m) { gExpr = m[2]; continue; } | |
| } | |
| if (!fExpr || !gExpr) return null; | |
| const allowed = /^[0-9a-zA-Z_+\-*/^().,\s]*$/; | |
| if (!allowed.test(fExpr) || !allowed.test(gExpr)) return null; | |
| try { | |
| const fSrc = fExpr.replace(/\^/g, "**"); | |
| const gSrc = gExpr.replace(/\^/g, "**"); | |
| const f = new Function("x", "y", "with(Math){return (" + fSrc + ");}"); | |
| const g = new Function("x", "y", "with(Math){return (" + gSrc + ");}"); | |
| // smoke test | |
| const t1 = f(0.3, 0.4), t2 = g(0.3, 0.4); | |
| if (typeof t1 !== "number" || typeof t2 !== "number") return null; | |
| return { f, g }; | |
| } catch (err) { | |
| return null; | |
| } | |
| } | |
| customInput.addEventListener("input", () => { | |
| const parsed = parseCustom(customInput.value); | |
| if (parsed) { | |
| F = parsed.f; | |
| G = parsed.g; | |
| currentName = "custom: " + customInput.value; | |
| renderField(); | |
| } else { | |
| customInput.style.borderColor = "#ff5566"; | |
| setTimeout(() => { customInput.style.borderColor = ""; }, 600); | |
| } | |
| }); | |
| // ---------------------------------------------------------------- init | |
| buildPresets(); | |
| renderField(); | |
| </script> | |
| </body> | |
| </html> |