Repository navigation
Expand file tree
/
Copy pathindex_python.html
More file actions
2742 lines (2266 loc) · 72.2 KB
/
Copy pathindex_python.html
File metadata and controls
2742 lines (2266 loc) · 72.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Stallings Folding 可视化</title>
<style>
:root {
--ink:#172238;
--muted:#5e6b82;
--line:#dbe2ec;
--bg:#f5f7fb;
--accent:#3559c7;
--orange:#d97706;
--fold:#f59e0b;
}
* { box-sizing:border-box; }
body {
margin:0;
background:var(--bg);
color:var(--ink);
font-family:Inter,system-ui,-apple-system,"Segoe UI",sans-serif;
}
main {
max-width:1320px;
margin:auto;
padding:26px 20px 40px;
}
header { margin-bottom:20px; }
.header-top {
display:flex;
align-items:flex-start;
justify-content:space-between;
gap:16px;
}
#lang-toggle {
flex:0 0 auto;
min-width:88px;
margin-top:2px;
border-color:#b9c6da;
background:#fff;
color:#33466b;
}
#lang-toggle:hover:not(:disabled) {
background:#edf2fb;
border-color:#879bc0;
}
@media(max-width:520px) {
.header-top { align-items:center; gap:8px; }
#lang-toggle { min-width:72px; padding:8px; }
}
h1 {
font-size:clamp(25px,3vw,35px);
letter-spacing:-.035em;
margin:0 0 8px;
}
.subtitle {
color:var(--muted);
line-height:1.65;
max-width:900px;
margin:0;
}
.layout {
display:grid;
grid-template-columns:310px minmax(0,1fr);
gap:18px;
align-items:start;
}
.panel {
background:#fff;
border:1px solid var(--line);
border-radius:16px;
box-shadow:0 5px 20px rgba(26,43,78,.045);
}
.sidebar { padding:18px; }
.content { min-width:0; }
.section-title {
font-weight:750;
font-size:15px;
margin:0 0 13px;
}
label {
display:block;
font-size:13px;
font-weight:650;
margin:14px 0 7px;
}
input[type=number],textarea {
width:100%;
border:1px solid #cbd5e1;
border-radius:9px;
padding:10px 11px;
background:#fff;
color:var(--ink);
font:inherit;
outline:none;
}
input[type=number] { max-width:115px; }
input:focus,textarea:focus {
border-color:#6d83dc;
box-shadow:0 0 0 3px #3559c71a;
}
textarea {
min-height:120px;
resize:vertical;
font:14px/1.7 ui-monospace,Consolas,monospace;
}
.hint {
color:var(--muted);
font-size:12px;
line-height:1.65;
margin:8px 0 0;
}
.examples,.button-group {
display:flex;
flex-wrap:wrap;
gap:7px;
}
.examples { margin-top:12px; }
button {
appearance:none;
border:1px solid #cbd5e1;
background:#fff;
color:var(--ink);
border-radius:9px;
padding:9px 11px;
font:650 13px system-ui,sans-serif;
cursor:pointer;
transition:background .15s,border-color .15s;
}
button:hover:not(:disabled) {
background:#f1f4fa;
border-color:#9aaac0;
}
button:disabled {
opacity:.4;
cursor:not-allowed;
}
button.primary {
background:var(--accent);
color:#fff;
border-color:var(--accent);
width:100%;
margin-top:15px;
padding:11px 12px;
}
button.play {
background:#fff7e7;
border-color:#f1c879;
color:#854d0e;
}
button.play:hover:not(:disabled) {
background:#ffefc9;
}
.example {
font-size:12px;
padding:6px 8px;
color:#35466b;
background:#f7f9fd;
}
.notice {
display:none;
margin-top:12px;
border:1px solid #f1c7ba;
background:#fff6f2;
color:#8f3218;
border-radius:10px;
padding:10px 12px;
font-size:13px;
line-height:1.6;
}
.notice.show { display:block; }
/* 所有步骤控制均位于左栏 */
.step-picker {
margin-top:17px;
padding-top:15px;
border-top:1px solid var(--line);
}
.step-picker-head {
display:flex;
align-items:center;
justify-content:space-between;
gap:8px;
}
.step-picker-head label { margin:0; }
.step-count {
color:var(--muted);
font-size:12px;
font-variant-numeric:tabular-nums;
white-space:nowrap;
}
#step-range {
display:block;
width:100%;
margin:13px 0 5px;
accent-color:var(--accent);
}
.step-extremes {
display:flex;
justify-content:space-between;
color:var(--muted);
font-size:11px;
}
.step-buttons {
display:grid;
grid-template-columns:1fr 1fr;
gap:7px;
margin-top:12px;
}
.step-buttons button { padding:9px 5px; }
.step-buttons .play {
grid-column:1 / -1;
padding:11px 8px;
}
.step-info {
border-radius:9px;
background:#f6f8fc;
padding:10px;
margin-top:12px;
font-size:12px;
line-height:1.65;
color:#4b5870;
}
.step-info strong {
color:#263754;
}
/* 右侧图形区域 */
.viewer { padding:17px 17px 14px; }
.viewer-head {
display:flex;
align-items:flex-start;
justify-content:space-between;
gap:14px;
flex-wrap:wrap;
}
.viewer-head h2 {
font-size:18px;
margin:0 0 5px;
}
.step-desc {
color:var(--muted);
font-size:13px;
line-height:1.55;
max-width:740px;
overflow-wrap:anywhere;
}
.state-tag {
background:#edf1ff;
color:#3048a1;
border-radius:99px;
padding:6px 10px;
font-size:12px;
font-weight:750;
white-space:nowrap;
}
.state-tag.final {
background:#e8f7ef;
color:#146c43;
}
.state-tag.animating {
background:#fff0cb;
color:#854d0e;
}
.metrics {
display:flex;
flex-wrap:wrap;
gap:8px;
margin:14px 0 12px;
}
.metric {
border:1px solid var(--line);
background:#fbfcff;
border-radius:10px;
padding:8px 11px;
min-width:84px;
}
.metric .num {
font-size:19px;
font-weight:800;
}
.metric .cap {
margin-top:2px;
color:var(--muted);
font-size:11px;
}
.graph-wrap {
width:100%;
border:1px solid #e3e8f0;
border-radius:12px;
overflow:hidden;
background:#fff;
}
#graph {
display:block;
width:100%;
height:auto;
}
.graph-note {
margin:9px 2px 0;
font-size:12px;
color:var(--muted);
line-height:1.65;
}
.legend {
display:flex;
flex-wrap:wrap;
gap:8px 14px;
margin:9px 2px 0;
}
.legend-item {
display:inline-flex;
align-items:center;
gap:6px;
font-size:12px;
color:#39465d;
}
.legend-swatch {
width:18px;
height:3px;
border-radius:99px;
display:inline-block;
}
.bottom-grid {
display:grid;
grid-template-columns:minmax(0,1fr) minmax(0,1fr);
gap:16px;
margin-top:16px;
}
.info-card { padding:16px 17px; }
.info-card h3 {
font-size:14px;
margin:0 0 10px;
}
.normalized {
margin:0;
padding-left:20px;
color:#344158;
font:12px/1.7 ui-monospace,Consolas,monospace;
overflow-wrap:anywhere;
}
.normalized li { margin:3px 0; }
.history {
list-style:none;
margin:0;
padding:0;
max-height:260px;
overflow:auto;
}
.history button {
width:100%;
text-align:left;
display:flex;
gap:9px;
align-items:flex-start;
border:0;
border-radius:8px;
padding:7px 8px;
background:transparent;
font-weight:500;
line-height:1.4;
}
.history button:hover { background:#f2f5fb; }
.history button.active {
background:#edf1ff;
color:#263f9b;
}
.history .step-no {
font-size:11px;
font-weight:800;
min-width:22px;
color:var(--muted);
padding-top:2px;
}
.history .step-text {
font-size:12px;
overflow-wrap:anywhere;
}
.footnote {
color:var(--muted);
font-size:12px;
line-height:1.7;
margin:17px 2px 0;
}
.edge-path {
fill:none;
stroke-width:2.2;
stroke-linecap:round;
stroke-linejoin:round;
}
.edge-label {
font:800 15px system-ui,sans-serif;
text-anchor:middle;
dominant-baseline:middle;
paint-order:stroke;
stroke:#fff;
stroke-width:5px;
stroke-linejoin:round;
}
.node {
fill:#fff;
stroke:#49627f;
stroke-width:1.8;
}
.node.base {
fill:#fff5e6;
stroke:var(--orange);
stroke-width:2.2;
}
.node.highlight {
fill:#fff0cb;
stroke:#e28a11;
stroke-width:2.8;
}
.node-label {
text-anchor:middle;
dominant-baseline:middle;
font:800 12px system-ui,sans-serif;
fill:#25344b;
pointer-events:none;
}
.empty-text {
text-anchor:middle;
dominant-baseline:middle;
font:14px system-ui,sans-serif;
fill:#8a96a9;
}
@media(max-width:900px) {
.layout { grid-template-columns:1fr; }
.bottom-grid { grid-template-columns:1fr; }
}
@media(max-width:520px) {
main { padding:16px 10px 28px; }
.viewer { padding:11px; }
.sidebar { padding:14px; }
.metric { min-width:72px; }
}
</style>
</head>
<body>
<main>
<header>
<div class="header-top">
<h1 id="page-title">Stallings folding 可视化</h1>
<button id="lang-toggle" type="button" aria-label="Switch language">English</button>
</div>
<p id="page-subtitle" class="subtitle">
观察自由群子群的初始玫瑰图、每次 folding 的局部变化,
以及最终得到的子群图 Δ(H)。
</p>
</header>
<div class="layout">
<aside class="panel sidebar">
<h2 id="input-section-title" class="section-title">输入子群数据</h2>
<label id="rank-label" for="rank">自由群的秩 n</label>
<input id="rank" type="number" value="2" min="1" max="1000">
<p id="rank-hint" class="hint">
前 26 个生成元记为 a、b、…、z;也可以使用 x1、x2、…。
</p>
<label id="words-label" for="words">H 的生成元(每行一个词)</label>
<textarea id="words" spellcheck="false">abab^{-1}
abb
bab
baaab^{-1}</textarea>
<p id="words-hint" class="hint">
支持 ab、ab^{-1}、a^-1b、x1x2^-1;
大写字母表示逆元,例如 aB。
</p>
<div class="examples">
<button class="example" data-example="basic">⟨ab, ab⁻¹⟩</button>
<button class="example" data-example="rank2">⟨a², b², ab⟩</button>
<button class="example" data-example="overlap">复杂折叠示例</button>
<button class="example" data-example="trivial">平凡子群</button>
</div>
<button id="build" class="primary" disabled>构造图并计算 folding</button>
<p id="pyodide-status" class="hint" role="status" aria-live="polite">正在加载 Python 算法环境……</p>
<div id="notice" class="notice" role="alert"></div>
<div class="step-picker">
<div class="step-picker-head">
<label id="step-range-label" for="step-range">折叠步骤</label>
<span id="step-count" class="step-count">0 / 0</span>
</div>
<input id="step-range" type="range" min="0" max="0" value="0" disabled>
<div class="step-extremes">
<span id="initial-step-label">初始图</span>
<span id="final-step-label">最终图</span>
</div>
<div class="step-buttons">
<button id="prev" disabled>← 上一步</button>
<button id="next" disabled>下一步 →</button>
<button id="play-fold" class="play" disabled>▶ 播放本步折叠</button>
</div>
<div id="fold-info" class="step-info">
选择一个步骤后,可以播放该步的 folding 动画。
</div>
</div>
</aside>
<section class="content">
<div class="panel viewer">
<div class="viewer-head">
<div>
<h2 id="graph-title">初始图 Γ₀</h2>
<div id="step-desc" class="step-desc">准备构造图。</div>
</div>
<div id="state-tag" class="state-tag">等待输入</div>
</div>
<div class="metrics">
<div class="metric">
<div id="v-count" class="num">—</div>
<div id="v-cap" class="cap">顶点数 |V|</div>
</div>
<div class="metric">
<div id="e-count" class="num">—</div>
<div id="e-cap" class="cap">边数 |E|</div>
</div>
<div class="metric">
<div id="rank-count" class="num">—</div>
<div id="rank-cap" class="cap">图的秩 |E|−|V|+1</div>
</div>
<div class="metric">
<div id="fold-count" class="num">—</div>
<div id="fold-cap" class="cap">已执行 folding</div>
</div>
</div>
<div class="graph-wrap">
<svg id="graph" viewBox="0 0 760 760"
role="img" aria-label="Stallings 折叠图"></svg>
</div>
<p id="graph-note" class="graph-note">
播放时,橙色边是本步正在折叠的边。顶点合并时,整张图会轻微调整以减少节点、边和交叉处的重叠;
所有位置与路径都平滑过渡,重复边淡出后画面停留在折叠后的图。
</p>
<div id="legend" class="legend" aria-label="生成元颜色图例"></div>
</div>
<div class="bottom-grid">
<section class="panel info-card">
<h3 id="normalized-title">输入词的自由约化</h3>
<ol id="normalized" class="normalized">
<li>等待输入。</li>
</ol>
</section>
<section class="panel info-card">
<h3 id="history-title">折叠历史(点击步骤可跳转)</h3>
<ol id="history" class="history">
<li class="hint">计算后将在这里列出每一步。</li>
</ol>
</section>
</div>
</section>
</div>
<p id="footnote" class="footnote">
每条边沿正生成元定向,反向读取对应逆元。
一次 folding 识别两条具有相同标签、相同出发顶点和相同读取方向的边,
并识别它们的终点;若终点已经相同,则只合并重复边。
</p>
</main>
<script>
(() => {
"use strict";
const $ = id => document.getElementById(id);
const VIEW = 760;
const CENTER = {x:380, y:380};
const ANIMATION_DURATION = 1050;
const HIGHLIGHT_DELAY = 350;
const PALETTE = [
"#3558bb", "#ed3438", "#159b71", "#9448cc",
"#df8c17", "#078eab", "#c12879", "#65952c",
"#684bd1", "#bd5423", "#17867b", "#5c6c83"
];
function genColor(i) {
return i < PALETTE.length
? PALETTE[i]
: `hsl(${(i * 137.508) % 360} 68% 42%)`;
}
const rankInput = $("rank");
const wordsInput = $("words");
const notice = $("notice");
const svg = $("graph");
const I18N = {
zh: {
pageTitle:"Stallings folding 可视化",
pageSubtitle:"观察自由群子群的初始玫瑰图、每次 folding 的局部变化,以及最终得到的子群图 Δ(H)。",
inputSection:"输入子群数据",
rankLabel:"自由群的秩 n",
rankHint:"前 26 个生成元记为 a、b、…、z;也可以使用 x1、x2、… 。",
wordsLabel:"H 的生成元(每行一个词)",
wordsHint:"支持 ab、ab^{-1}、a^-1b、x1x2^-1;大写字母表示逆元,例如 aB。",
exampleOverlap:"复杂折叠示例",
exampleTrivial:"平凡子群",
build:"构造图并计算 folding",
pythonLoading:"正在加载 Python 算法环境……首次访问可能需要一些时间。",
pythonReady:"Python 算法已就绪。",
pythonComputing:"正在计算折叠步骤……",
pythonFailed:"Python 环境加载失败。请检查网络连接,并通过 GitHub Pages 或本地 HTTP 服务器打开页面。",
algorithmError:"算法运行失败:{{detail}}",
invalidInput:"输入格式无效。",
stepLabel:"折叠步骤",
initialGraph:"初始图",
finalGraph:"最终图",
prev:"← 上一步",
next:"下一步 →",
play:"▶ 播放本步折叠",
chooseStep:"选择一个步骤后,可以播放该步的 folding 动画。",
graphTitle:"初始图 Γ₀",
graphPreparing:"准备构造图。",
waiting:"等待输入",
vertices:"顶点数 |V|",
edges:"边数 |E|",
graphRank:"图的秩 |E|−|V|+1",
foldCount:"已执行 folding",
graphNote:"播放时,橙色边是本步正在折叠的边。顶点合并时,整张图会轻微调整以减少节点、边和交叉处的重叠;所有位置与路径都平滑过渡,重复边淡出后画面停留在折叠后的图。",
normalizedTitle:"输入词的自由约化",
normalizedWaiting:"等待输入。",
historyTitle:"折叠历史(点击步骤可跳转)",
historyWaiting:"计算后将在这里列出每一步。",
footnote:"每条边沿正生成元定向,反向读取对应逆元。一次 folding 识别两条具有相同标签、相同出发顶点和相同读取方向的边,并识别它们的终点;若终点已经相同,则只合并重复边。",
switchButton:"English",
switchAria:"切换到英文",
initialTitle:"初始图 Γ₀ = R_k",
initialDescription:"初始图是 R_k(k={{k}}),每个花瓣对应一个生成词。",
finalTitle:"最终图 Δ(H) = Γ₀",
finalTitleAfter:"最终图 Δ(H)(第 {{n}} 次 folding 后)",
foldTitle:"第 {{n}} 次 folding",
alreadyFolded:"初始图已经满足折叠条件。",
foldDescSame:"在顶点 {{v}} 处,两条沿 {{dir}} 读取的边 e{{e1}}、e{{e2}} 终点已经相同,因此只需识别重复边。",
foldDescDifferent:"在顶点 {{v}} 处,沿 {{dir}} 读取的边 e{{e1}}、e{{e2}} 可折叠;识别终点 {{t1}} 与 {{t2}}。",
tooManyFolds:"折叠步骤超过 20000 步,已中止。",
invalidGen:"词“{{raw}}”中的生成元编号无效。",
invalidChar:"无法解析词“{{raw}}”中的字符“{{ch}}”。",
generatorOutOfRange:"词“{{raw}}”使用了第 {{g}} 个生成元,但 n={{n}}。",
invalidRank:"自由群的秩 n 必须是 1 到 1000 之间的整数。",
finalInfoTitle:"最终图",
finalInfo:"已经没有可折叠的同标签同方向边。",
nextFoldTitle:"下一步折叠",
identifyEdges:"识别两条边 e{{e1}} 和 e{{e2}},标签为 {{label}}。",
sameEndpointInfo:"两个终点已经相同,只需消除重复边。",
mergeVertices:"将顶点 {{t1}} 与 {{t2}} 合并。",
foldTargetTitle:"本步折叠对象",
sameEndpointAnim:"终点已经相同:重复边将被合并。",
mergeAnim:"顶点 {{t1}} 与 {{t2}} 将合并。",
preparingFold:"准备折叠",
animating:"正在折叠",
completed:"完成",
finalState:"最终折叠图",
initialState:"初始图",
stepState:"步骤 {{n}}",
noGenerators:"未输入生成元:H = {1}。",
emptyGraph:"无边:Δ(H) 只有基点",
edgeTitle:"边 e{{id}},标签 {{label}}",
languageLabel:"语言切换"
},
en: {
pageTitle:"Stallings Folding Visualizer",
pageSubtitle:"Explore the initial rose graph of a subgroup of a free group, each local folding move, and the resulting subgroup graph Δ(H).",
inputSection:"Subgroup Input",
rankLabel:"Rank n of the free group",
rankHint:"The first 26 generators are a, b, …, z; you can also use x1, x2, … .",
wordsLabel:"Generators of H (one word per line)",
wordsHint:"Supports ab, ab^{-1}, a^-1b, x1x2^-1. Uppercase letters denote inverses, e.g. aB.",
exampleOverlap:"Complex folding example",
exampleTrivial:"Trivial subgroup",
build:"Build graph and compute folding",
pythonLoading:"Loading the Python algorithm environment… This may take a moment on first visit.",
pythonReady:"Python algorithm is ready.",
pythonComputing:"Computing folding steps…",
pythonFailed:"Failed to load the Python environment. Check your network and open the page through GitHub Pages or a local HTTP server.",
algorithmError:"Algorithm failed: {{detail}}",
invalidInput:"Invalid input format.",
stepLabel:"Folding steps",
initialGraph:"Initial graph",
finalGraph:"Final graph",
prev:"← Previous",
next:"Next →",
play:"▶ Play this fold",
chooseStep:"Choose a step to play its folding animation.",
graphTitle:"Initial graph Γ₀",
graphPreparing:"Ready to build the graph.",
waiting:"Waiting for input",
vertices:"Vertices |V|",
edges:"Edges |E|",
graphRank:"Graph rank |E|−|V|+1",
foldCount:"Folds performed",
graphNote:"During playback, the orange edges are the ones being folded. As vertices merge, the layout adjusts slightly to reduce overlaps among vertices, edges, and crossings. Positions and paths transition smoothly; the duplicate edge fades out, leaving the folded graph.",
normalizedTitle:"Free reduction of input words",
normalizedWaiting:"Waiting for input.",
historyTitle:"Folding history (click a step to jump)",
historyWaiting:"Each step will be listed here after computation.",
footnote:"Each edge is oriented by the positive generator; traversing it backward reads the inverse. A folding identifies two edges with the same label, initial vertex, and reading direction, and identifies their terminal vertices. If their terminal vertices already agree, only the duplicate edges are merged.",
switchButton:"中文",
switchAria:"Switch to Chinese",
initialTitle:"Initial graph Γ₀ = R_k",
initialDescription:"The initial graph is R_k (k={{k}}), with one petal for each generator word.",
finalTitle:"Final graph Δ(H) = Γ₀",
finalTitleAfter:"Final graph Δ(H) (after {{n}} folds)",
foldTitle:"Fold {{n}}",
alreadyFolded:"The initial graph already satisfies the folding condition.",
foldDescSame:"At vertex {{v}}, the edges e{{e1}} and e{{e2}} read {{dir}} and already have the same terminal vertex, so only the duplicate edges are identified.",
foldDescDifferent:"At vertex {{v}}, the edges e{{e1}} and e{{e2}} can be folded along {{dir}}, identifying terminal vertices {{t1}} and {{t2}}.",
tooManyFolds:"More than 20,000 folding steps; computation was stopped.",
invalidGen:"Invalid generator index in word “{{raw}}”.",
invalidChar:"Cannot parse character “{{ch}}” in word “{{raw}}”.",
generatorOutOfRange:"Word “{{raw}}” uses generator {{g}}, but n={{n}}.",
invalidRank:"The free-group rank n must be an integer between 1 and 1000.",
finalInfoTitle:"Final graph",
finalInfo:"No more pairs of equally labelled edges with the same reading direction can be folded.",
nextFoldTitle:"Next fold",
identifyEdges:"Identify edges e{{e1}} and e{{e2}}, labelled {{label}}.",
sameEndpointInfo:"The terminal vertices already agree; only the duplicate edge needs to be removed.",
mergeVertices:"Merge vertices {{t1}} and {{t2}}.",
foldTargetTitle:"Edges being folded",
sameEndpointAnim:"The terminal vertices already agree; the duplicate edge will be merged.",
mergeAnim:"Vertices {{t1}} and {{t2}} will merge.",
preparingFold:"Preparing fold",
animating:"Folding…",
completed:"Complete",
finalState:"Final folded graph",
initialState:"Initial graph",
stepState:"Step {{n}}",
noGenerators:"No generators entered: H = {1}.",
emptyGraph:"No edges: Δ(H) consists only of the basepoint",
edgeTitle:"Edge e{{id}}, label {{label}}",
languageLabel:"Language switch"
}
};
let currentLang = "en";
let pythonStatusKey = "pythonLoading";
function tr(key, vars={}) {
let text = (I18N[currentLang] && I18N[currentLang][key]) || I18N.zh[key] || key;
for (const [name,value] of Object.entries(vars)) {
text = text.replaceAll(`{{${name}}}`,String(value));
}
return text;
}
function vertexName(v, base) {
return v === base ? "*" : `v${v}`;
}
function getStepTitle(index) {
if (!steps.length) return tr("graphTitle");
if (steps.length===1) return tr("finalTitle");
if (index===0) return tr("initialTitle");
if (index===steps.length-1) return tr("finalTitleAfter",{n:index});
return tr("foldTitle",{n:index});
}
function getStepDescription(index) {
if (!steps.length) return tr("graphPreparing");
if (steps.length===1) return tr("alreadyFolded");
if (index===0) {
return tr("initialDescription",{k:steps[0].graph.petals.length});
}
return foldDescription(steps[index].foldFromPrevious,steps[index-1].graph);
}
function applyStaticLanguage() {
document.documentElement.lang=currentLang==="zh"?"zh-CN":"en";
document.title=tr("pageTitle");
const set=(id,key)=>{const el=$(id);if(el)el.textContent=tr(key);};
set("page-title","pageTitle");
set("page-subtitle","pageSubtitle");
set("input-section-title","inputSection");
set("rank-label","rankLabel");
set("rank-hint","rankHint");
set("words-label","wordsLabel");
set("words-hint","wordsHint");
set("build","build");
set("step-range-label","stepLabel");
set("initial-step-label","initialGraph");
set("final-step-label","finalGraph");
set("prev","prev");
set("next","next");
set("play-fold","play");
set("v-cap","vertices");
set("e-cap","edges");
set("rank-cap","graphRank");
set("fold-cap","foldCount");
set("graph-note","graphNote");
set("normalized-title","normalizedTitle");
set("history-title","historyTitle");
set("footnote","footnote");
const examples=document.querySelectorAll("[data-example]");
for(const btn of examples){
if(btn.dataset.example==="overlap") btn.textContent=tr("exampleOverlap");
if(btn.dataset.example==="trivial") btn.textContent=tr("exampleTrivial");
}
const langButton=$("lang-toggle");
langButton.textContent=tr("switchButton");
langButton.setAttribute("aria-label",tr("switchAria"));
langButton.title=tr("languageLabel");
svg.setAttribute("aria-label",currentLang==="zh"?"Stallings 折叠图":"Stallings folding graph");
$("legend").setAttribute("aria-label",currentLang==="zh"?"生成元颜色图例":"Generator color legend");
setPythonStatus(pythonStatusKey);
}
let steps = [];
let currentStep = 0;
let noGeneratorsEntered = false;
let isAnimating = false;
let animationToken = 0;
function genName(i) {
return i < 26 ? String.fromCharCode(97 + i) : `x${i + 1}`;
}
function wordText(word) {
return word.length
? word.map(t => genName(t.g) + (t.s < 0 ? "⁻¹" : "")).join("")
: "1";
}
function foldDescription(f, graph) {
const dir = f.sign > 0
? genName(f.label)
: `${genName(f.label)}⁻¹`;
if (f.t1 === f.t2) {
return tr("foldDescSame",{
v:vertexName(f.vertex,graph.base),dir,e1:f.e1,e2:f.e2
});
}
return tr("foldDescDifferent",{
v:vertexName(f.vertex,graph.base),dir,e1:f.e1,e2:f.e2,
t1:vertexName(f.t1,graph.base),t2:vertexName(f.t2,graph.base)
});
}
/* =========================================================
3. Bézier 几何
所有边统一转换成三次 Bézier 表示:
p0, c1, c2, p3。
这样动画时可以直接逐点插值路径控制点;
直线、普通弯曲边、自环之间也可以平滑过渡。
========================================================= */
function lerp(a, b, t) {
return a + (b - a) * t;
}
function lerpPoint(a, b, t) {
return {
x:lerp(a.x, b.x, t),
y:lerp(a.y, b.y, t)
};
}
function cubicPoint(p0, c1, c2, p3, t) {
const s = 1 - t;
return {
x:s*s*s*p0.x + 3*s*s*t*c1.x +
3*s*t*t*c2.x + t*t*t*p3.x,
y:s*s*s*p0.y + 3*s*s*t*c1.y +
3*s*t*t*c2.y + t*t*t*p3.y
};
}
function cubicDerivative(p0, c1, c2, p3, t) {
const s = 1 - t;
return {
x:3*s*s*(c1.x-p0.x) +
6*s*t*(c2.x-c1.x) +
3*t*t*(p3.x-c2.x),
y:3*s*s*(c1.y-p0.y) +
6*s*t*(c2.y-c1.y) +
3*t*t*(p3.y-c2.y)
};
}
function cubicGeometry(p0, c1, c2, p3) {
// De Casteljau:将曲线精确分成两段,使中点可放置箭头。
const a = lerpPoint(p0, c1, 0.5);
const b = lerpPoint(c1, c2, 0.5);
const c = lerpPoint(c2, p3, 0.5);
const d = lerpPoint(a, b, 0.5);
const e = lerpPoint(b, c, 0.5);
const mid = lerpPoint(d, e, 0.5);
return {
p0,c1,c2,p3,
mid,
tangent:{x:e.x-d.x, y:e.y-d.y},
controlCenter:lerpPoint(c1,c2,0.5),
path: