版本比较
图-查看器
正文
增加 154 行删除 26 行
2 行未改动34567789101176 行未改动888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138204 行未改动全部展开343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392110 行未改动全部展开5035045055064225075085095105115125135145153 行未改动51952052152252352452552652752852953053153253310 行未改动54454554654745454854955055129 行未改动5815825835844925855865875885895905915925935945954954965965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456463 行未改动6506516526535086546556566576 行未改动6646656666675236686696706716725296736746756766776786795375386806816826834 行未改动6886896906915516926936946955 行未改动70170270370456570570670770829 行未改动738739740741603742743744745133 行未改动全部展开8798808818827458838848858869 行未改动89689789889976390090190290322 行未改动92692792892979493093193293327 行未改动96196296396483096596696796849 行未改动10181019102010218881022102310241025102610271028102910308981031103210331034333 行未改动全部展开1368136913701371124012411372137313741375137615 行未改动13921393139413951265139613971398139910 行未改动141014111412141312841414141514161417908 行未改动全部展开23262327232823292201233023312332233322062334233523362337225 行未改动全部展开2563256425652566256725682569257025712 行未改动2 行未改动<html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>图论可视化 · 2D + 实数/负权 + 自环/重边 + 代码模式 + 导出</title><title>图论可视化</title><style> :root{ --bg:#f1f5f9; --border:#e2e8f0; --text:#0f172a; --muted:#64748b; --primary:#2563eb;76 行未改动 .btn-row .btn{margin-top:0;}
.hint{font-size:12px;color:var(--muted);line-height:1.6;margin:9px 0 0;}
/* ================= 滑块 ================= */ .slider-row{margin-bottom:14px;} .slider-row:last-of-type{margin-bottom:0;} .slider-row label{ display:flex;justify-content:space-between;align-items:center; font-size:12.5px;color:#334155;margin-bottom:6px;gap:8px; } .slider-row label .name{font-weight:500;} .slider-row label .val{ font-family:ui-monospace,Menlo,monospace;font-size:12px; color:var(--primary);font-weight:700;background:#eff6ff; padding:1px 8px;border-radius:5px;min-width:52px;text-align:center; } .slider-row input[type=range]{ width:100%;height:5px;border-radius:3px; background:#e2e8f0;outline:none;-webkit-appearance:none; appearance:none;cursor:pointer;padding:0;margin:0; } .slider-row input[type=range]::-webkit-slider-thumb{ -webkit-appearance:none;appearance:none; width:16px;height:16px;border-radius:50%; background:var(--primary);cursor:pointer; border:2px solid #fff;box-shadow:0 1px 4px rgba(37,99,235,.45); transition:transform .12s ease; } .slider-row input[type=range]::-webkit-slider-thumb:hover{ transform:scale(1.15); } .slider-row input[type=range]::-webkit-slider-thumb:active{ transform:scale(1.25); } .slider-row input[type=range]::-moz-range-thumb{ width:14px;height:14px;border-radius:50%; background:var(--primary);cursor:pointer; border:2px solid #fff;box-shadow:0 1px 4px rgba(37,99,235,.45); } .slider-row input[type=range]::-moz-range-track{ height:5px;border-radius:3px;background:#e2e8f0; } .slider-row .desc{ font-size:11px;color:#94a3b8;margin-top:5px;line-height:1.4; }
.code-area{ width:100%;min-height:180px;padding:10px 12px;margin-top:8px; border:1px solid #cbd5e1;border-radius:9px;background:#0f172a;color:#e2e8f0; font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace;204 行未改动全部展开 <label class="check"><input type="checkbox" id="chkPhysics" checked> 弹性碰撞箱(解压手感)</label> <p class="hint">开启后顶点互相碰撞、拖拽带惯性;关闭后顶点静止且可自由重叠。</p> </section>
<section class="card" id="physTuningCard"> <h2>物理调参</h2>
<div class="slider-row"> <label> <span class="name">速度阻尼(阻力)</span> <span class="val" id="dampingVal">0.925</span> </label> <input type="range" id="dampingSlider" min="0.5" max="1" step="0.005" value="0.925"> <div class="desc">每帧速度保留比例。越小阻力越大,顶点越快停下;越大越顺滑。</div> </div>
<div class="slider-row"> <label> <span class="name">拖拽阻尼</span> <span class="val" id="dragDragVal">0.88</span> </label> <input type="range" id="dragDragSlider" min="0.3" max="1" step="0.01" value="0.88"> <div class="desc">拖动顶点时速度衰减。越小拖动越稳,越大松手飞得越远。</div> </div>
<div class="slider-row"> <label> <span class="name">碰撞弹性</span> <span class="val" id="restitutionVal">0.78</span> </label> <input type="range" id="restitutionSlider" min="0" max="1" step="0.02" value="0.78"> <div class="desc">两顶点碰撞后能量保留比例。0 = 完全非弹性,1 = 完全弹性。</div> </div>
<div class="slider-row"> <label> <span class="name">撞墙弹性</span> <span class="val" id="wallBounceVal">0.62</span> </label> <input type="range" id="wallBounceSlider" min="0" max="1" step="0.02" value="0.62"> <div class="desc">撞到画布边界时能量保留比例。</div> </div>
<button class="btn" id="btnPhysReset" style="margin-top:10px">↻ 恢复默认</button> </section>
<section class="card"> <h2>编辑模式</h2> <div class="seg" id="modeSeg"> <button class="seg-btn active" data-mode="drag">拖动</button>110 行未改动全部展开 let highlight = { nodeIds: new Set(), edgeIds: new Set() }; let topoColorMap = new Map(); let flowHighlight = new Map();
/* 每条边的几何元数据 */ let edgeMeta = new Map();
/* ============================================================ * 物理参数(可通过滑块调整) * ============================================================ */ const PHYS = { restitution: 0.78, damping: 0.925, wallBounce: 0.62,3 行未改动 sleepSpeed: 0.075, iterations: 2 };
const PHYS_DEFAULTS = { restitution: 0.78, damping: 0.925, wallBounce: 0.62, dragDrag: 0.88, };
let physicsEnabled = chkPhysics.checked; let physicsOn = false; let lastFrame = 0;
10 行未改动 const s = v.toFixed(4).replace(/0+$/, '').replace(/\.$/, ''); return s || '0'; }
/* 导出时用完整精度字符串,保证 round-trip */ function fullNumStr(v) { if (typeof v !== 'number' || !Number.isFinite(v)) return '1'; if (Number.isInteger(v)) return String(v); return String(v);29 行未改动 function clearFlow() { flowHighlight = new Map(); } function clearAllVisual() { clearHighlight(); clearTopo(); clearFlow(); closePopup(); }
/* ============================================================ * 边几何(重边弯曲修复版) * 物理调参滑块 * ============================================================ */ const dampingSlider = document.getElementById('dampingSlider'); const dampingVal = document.getElementById('dampingVal'); const dragDragSlider= document.getElementById('dragDragSlider'); const dragDragVal = document.getElementById('dragDragVal'); const restitutionSlider = document.getElementById('restitutionSlider'); const restitutionVal = document.getElementById('restitutionVal'); const wallBounceSlider = document.getElementById('wallBounceSlider'); const wallBounceVal = document.getElementById('wallBounceVal');
/* 统一规范垂直方向:始终基于(小 id → 大 id) 这样 offset 的符号有稳定含义,与边的存储顺序无关 */ function syncPhysSliders() { dampingSlider.value = PHYS.damping; dampingVal.textContent = PHYS.damping.toFixed(3);
dragDragSlider.value = PHYS.dragDrag; dragDragVal.textContent = PHYS.dragDrag.toFixed(2);
restitutionSlider.value = PHYS.restitution; restitutionVal.textContent = PHYS.restitution.toFixed(2);
wallBounceSlider.value = PHYS.wallBounce; wallBounceVal.textContent = PHYS.wallBounce.toFixed(2); }
dampingSlider.addEventListener('input', () => { PHYS.damping = parseFloat(dampingSlider.value); dampingVal.textContent = PHYS.damping.toFixed(3); wake(); });
dragDragSlider.addEventListener('input', () => { PHYS.dragDrag = parseFloat(dragDragSlider.value); dragDragVal.textContent = PHYS.dragDrag.toFixed(2); wake(); });
restitutionSlider.addEventListener('input', () => { PHYS.restitution = parseFloat(restitutionSlider.value); restitutionVal.textContent = PHYS.restitution.toFixed(2); wake(); });
wallBounceSlider.addEventListener('input', () => { PHYS.wallBounce = parseFloat(wallBounceSlider.value); wallBounceVal.textContent = PHYS.wallBounce.toFixed(2); wake(); });
document.getElementById('btnPhysReset').addEventListener('click', () => { Object.assign(PHYS, PHYS_DEFAULTS); syncPhysSliders(); wake(); });
/* ============================================================ * 边几何 * ============================================================ */ function canonicalPerp(A, B) { const dx = B.x - A.x, dy = B.y - A.y; const d = Math.hypot(dx, dy) || 1; const ux = dx / d, uy = dy / d;3 行未改动
function computeEdgeMeta() { edgeMeta = new Map();
/* 非自环:按无序节点对分组 */ const groups = new Map(); for (const e of edges) { if (e.a === e.b) continue; const key = Math.min(e.a, e.b) + '-' + Math.max(e.a, e.b);6 行未改动 edgeMeta.set(group[0].id, { offset: 0 }); continue; } if (!directed) { /* 无向图:均匀扇开 */ const total = group.length; group.forEach((e, i) => { edgeMeta.set(e.id, { offset: (i - (total - 1) / 2) * 30 }); }); } else { /* 有向图:按方向分开,但 offset 是相对规范方向而言 */ const minId = Number(key.split('-')[0]); const forward = group.filter(e => e.a === minId); const backward = group.filter(e => e.a !== minId); const hasF = forward.length > 0; const hasB = backward.length > 0;
if (hasF && hasB) { /* 注意:backward 组的规范方向也要用 minId→maxId 由于 backward 边实际是 maxId→minId,反向时 offset 要取反 */ forward.forEach((e, i) => edgeMeta.set(e.id, { offset: 28 + i * 30 })); backward.forEach((e, i) => edgeMeta.set(e.id, { offset: -28 - i * 30 })); } else { const list = hasF ? forward : backward;4 行未改动 } } }
/* 自环:按节点分组 */ const selfGroups = new Map(); for (const e of edges) { if (e.a !== e.b) continue; if (!selfGroups.has(e.a)) selfGroups.set(e.a, []);5 行未改动 }); } }
/* 直线 / 弯曲边路径(用规范垂直方向) */ function edgePath(A, B, offset) { const dx = B.x - A.x, dy = B.y - A.y; const d = Math.hypot(dx, dy) || 1; const ux = dx / d, uy = dy / d;29 行未改动 y: 0.25 * A.y + 0.5 * cy + 0.25 * B.y }; }
/* 自环几何 */ function selfLoopGeometry(node, idx, total) { const R = NODE_R; const count = Math.max(1, total); const baseAngle = -Math.PI / 2 + (idx * 2 * Math.PI) / count;133 行未改动全部展开 </marker> </defs> <rect data-blank="1" x="0" y="0" width="${W}" height="${H}" fill="url(#grid)"/>`;
/* 预计算每条边的路径与中点 */ const edgeDraws = []; for (const e of edges) { const A = nmap.get(e.a), B = nmap.get(e.b); if (!A || !B) continue;9 行未改动 } edgeDraws.push({ e, d, mid }); }
/* 边主体 */ for (const { e, d } of edgeDraws) { const hot = highlight.edgeIds.has(e.id); const hasFlow = flowHighlight.has(e.id); const isNeg = isNegEdge(e);22 行未改动 html += `<path data-edge="${e.id}" d="${d}" fill="none" stroke="none" stroke-width="18" pointer-events="stroke" style="cursor:pointer"/>`; }
/* 容量标签 */ if (capEnabled) { for (const { e, mid } of edgeDraws) { const isNeg = isNegEdge(e); const txt = fmtNum((typeof e.cap === 'number') ? e.cap : 1);27 行未改动 </g>`; } }
/* 最大流箭头 + 流量数字 */ if (flowHighlight.size > 0) { for (const [edgeId, info] of flowHighlight) { const e = edges.find(x => x.id === edgeId); if (!e) continue;49 行未改动 } } }
/* 连边预览 */ if (mode === 'edge' && pending !== null && hoverPt) { const pn = nmap.get(pending); if (pn) { html += `<line x1="${pn.x}" y1="${pn.y}" x2="${hoverPt.x}" y2="${hoverPt.y}" stroke="#2563eb" stroke-width="2" stroke-dasharray="6 6" opacity="0.55" pointer-events="none"/>`; } }
/* 顶点 */ for (let i = 0; i < nodes.length; i++) { const nd = nodes[i]; const isPend = pending === nd.id; const isHot = highlight.nodeIds.has(nd.id);333 行未改动全部展开 return lines.join('\n'); }
function updateExport() { const text = generateExportText(); exportArea.value = text; exportArea.value = generateExportText(); }
function showExportStatus(msg, isErr) { exportStatus.className = 'code-status ' + (isErr ? 'err' : 'ok');15 行未改动 } else { throw new Error('no clipboard'); } } catch (err) { /* 兼容旧浏览器:临时解除 readonly 后 select + copy */ try { const wasReadonly = exportArea.readOnly; exportArea.readOnly = false; exportArea.select();10 行未改动 }
document.getElementById('btnExportCopy').addEventListener('click', copyExport);
/* 卡片展开时刷新内容 */ exportCard.addEventListener('toggle', () => { if (exportCard.open) updateExport(); });
908 行未改动全部展开 } else if (mode === 'edge') { if (pending === null) { pending = id; } else if (pending === id) { /* 自环 */ edges.push({ id: edgeSeq++, a: id, b: id, cap: 1 }); pending = null; clearAllVisual(); } else { /* 允许重边 */ edges.push({ id: edgeSeq++, a: pending, b: id, cap: 1 }); pending = null; clearAllVisual(); }225 行未改动全部展开 const r = stage.getBoundingClientRect(); W = Math.max(240, Math.round(r.width)); H = Math.max(240, Math.round(r.height)); } syncPhysSliders(); randomGenerate();})();</script></body>2 行未改动