-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzle.js
2518 lines (2271 loc) · 77.2 KB
/
puzzle.js
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
// general purpose functions
Object.prototype.clone = function() {
var newObj = (this instanceof Array) ? [] : {};
for (i in this) {
if (i == 'clone') continue;
if (this[i] && typeof this[i] == "object") {
newObj[i] = this[i].clone();
} else newObj[i] = this[i]
} return newObj;
};
function nestedArray(k, n, val) // k-nested arrays of length n
{
if (k==0) { if (val instanceof Object) return val.clone(); else return val; }
var tmp=new Array(n);
for (var i=0; i<n; i++) tmp[i]=nestedArray(k-1,n,val);
return tmp;
}
//
var sq3 = 1.73205080757;
var sq2 = 1.41421356237;
var icon; // little icon for current puzzle animation button
// webgl stuff
var gl;
var canvas,precanvas;
function initGL() // init openGL
{
try {
gl = canvas.getContext("webgl");
// gl = canvas.getContext("webgl", { premultipliedAlpha: false });
gl.viewport(0, 0, canvas.width, canvas.height);
}
catch(e) { alert("Could not initialise WebGL, sorry :-("); }
}
function getShader(gl, id)
{
var shaderScript = document.getElementById(id);
if (!shaderScript) {
return null;
}
var str = "";
var k = shaderScript.firstChild;
while (k) {
if (k.nodeType == 3) {
str += k.textContent;
}
k = k.nextSibling;
}
var shader;
if (shaderScript.type == "x-shader/x-fragment") {
shader = gl.createShader(gl.FRAGMENT_SHADER);
} else if (shaderScript.type == "x-shader/x-vertex") {
shader = gl.createShader(gl.VERTEX_SHADER);
} else {
return null;
}
gl.shaderSource(shader, str);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert(gl.getShaderInfoLog(shader));
return null;
}
return shader;
}
function makeShaderProgram(fs,vs)
{
var fragmentShader = getShader(gl, fs);
var vertexShader = getShader(gl, vs);
var sp = gl.createProgram();
gl.attachShader(sp, vertexShader);
gl.attachShader(sp, fragmentShader);
gl.linkProgram(sp);
if (!gl.getProgramParameter(sp, gl.LINK_STATUS))
alert("Could not initialise shaders");
return sp;
}
var shaderProgram;
var nshaders=2; // 2 programs (pairs of shaders)
function initShaders()
{
shaderProgram=new Array(nshaders);
for (var i=0; i<nshaders; i++)
{
shaderProgram[i]=makeShaderProgram('shader-fs'+i,'shader-vs'+i);
shaderProgram[i].vertexPositionAttribute = gl.getAttribLocation(shaderProgram[i], "VertexPosition");
gl.enableVertexAttribArray(shaderProgram[i].vertexPositionAttribute);
}
}
var currentProgram;
function useProgram(i)
{
currentProgram=i;
gl.useProgram(shaderProgram[i]);
}
function uniform(fun,str)
{
// extract value arguments
var args = Array.prototype.slice.call(arguments,2);
args.unshift(null);
if (args[0]=gl.getUniformLocation(shaderProgram[currentProgram], str)) // should we store them once and for all?
fun.apply(gl,args);
}
// graphical stuff
function addGraph(graph,type,indexbuf,flags,color,width,sampler)
{
if (!graph.hasOwnProperty('data')) graph.data=[];
var obj = new Object(); // could compress all that's below using {x:y, ... } syntax
obj.type=type; // line, triangle, line loop
obj.flags=flags;
if ((typeof(width)!=='undefined')&&(width!==null)) obj.width=width; // for lines and labels only
if ((typeof(color)!=='undefined')&&(color!==null)) obj.color=new Float32Array(color);
if ((typeof(sampler)!=='undefined')&&(sampler!==null)) obj.sampler=sampler;
obj.glbuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, obj.glbuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indexbuf), gl.STATIC_DRAW);
obj.n=indexbuf.length;
graph.data.push(obj);
}
function addCoords(graph,coords) // should make it a method of graph
{
graph.coordbuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, graph.coordbuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(coords), gl.STATIC_DRAW);
// probably shouldn't be there, but for now... necessary for proper POINTS drawing, and also LINES for mouse pointer
function cmp(a,b) { return b.type-a.type; }
graph.data.sort(cmp);
}
function deleteBuffers(graph) // make way for a new picture, possibly deleting GL buffers if we don't need them any more
{
if (graph instanceof Array)
{
for (var i=0; i<graph.length; i++)
deleteBuffers(graph[i]);
}
else
{
while (graph.data.length>0)
gl.deleteBuffer(graph.data.pop().glbuffer);
gl.deleteBuffer(graph.coordbuffer);
}
}
var mrot0=[
// standard view of puzzles (up to a rather arbitrary z coordinate)
[[-1/(2*sq2), sq3/sq2/2, -1/(2*sq2), sq3/sq2/2],
[-sq3/sq2/2, -1/(2*sq2), -(sq3/sq2/2), -1/(2*sq2)],
[1/2/sq2,-sq3/sq2/2,-1/2/sq2,sq3/sq2/2],
[sq3/sq2/2,1/2/sq2,-sq3/sq2/2,-1/2/sq2]],
// other views: square-triangle(-rhombus) tiling
[[1./4*(1 - sq3), 1./4*(1 + sq3), -(1./2), 1./2],
[1./4*(-1 - sq3), 1./4*(1 - sq3), -(1./2), -(1./2)],
[1./4*(-1 + sq3), 1./4*(-1 - sq3), -(1./2), 1./2],
[1./4*(1 + sq3), 1./4*(-1 + sq3), -(1./2), -(1./2)]],
// tableaux
[[0, 0, 0, 1], [0, 1, 0, 0], [-1, 0, 0, 0], [0, 0, -1, 0]],
//
[[0,0,-sq3/2,1./2],
[-sq3/2,1./2,0,0],
[0,0,-1./2,-sq3/2],
[1./2,sq3/2,0,0]],
// another funny one half-way between the previous 2
// actually it seems known in the lit
[[0,1/sq2,-sq3/sq2/2,1./2/sq2],
[-sq3/sq2/2,-1./2/sq2,-sq3/sq2/2,-1./2/sq2],
[1./2/sq2,-sq3/sq2/2,-1./2/sq2,sq3/sq2/2],
[1/sq2,0,-1./2/sq2,-sq3/sq2/2]],
// honeycomb
[[-1./2,sq3/2,0,0],[-sq3/2.,-1./2,0,0],[0,0,-sq3/2,1./2],[0,0,-1./2,-sq3/2]],
// dual honeycomb
[[0,0,-1./2,sq3/2],[0,0,-sq3/2,-1./2],[1./2,-sq3/2,0,0],[sq3/2,1./2,0,0]],
// regular hexagon -- ``dual puzzle''
[[0, 1/sq2, -(sq3/sq2/2), 1/(2*sq2)],
[-(1/sq2), 0, -(1/(2*sq2)), -(sq3/sq2/2)],
[0, -1/sq2, -(sq3/sq2/2), 1/(2*sq2)],
[(1/sq2), 0, -(1/(2*sq2)), -(sq3/sq2/2)]],
// ``dual square-triangle-rhombus tiling''
[[(-1 + sq3)/4, (1 + sq3)/4, (-1 - sq3)/4, (-1 + sq3)/4],
[(-1 - sq3)/4, (-1 + sq3)/4, (1 - sq3)/4, (-1 - sq3)/4],
[(1 - sq3)/4, (-1 - sq3)/4, (-1 - sq3)/4, (-1 + sq3)/4],
[(1 + sq3)/4, (1 - sq3)/4, (1 - sq3)/4, (-1 - sq3)/4]],
// standard view of puzzles rotated 180 degrees
[[1/(2*sq2), -sq3/sq2/2, 1/(2*sq2), -sq3/sq2/2],
[sq3/sq2/2, 1/(2*sq2), (sq3/sq2/2), 1/(2*sq2)],
[1/2/sq2,-sq3/sq2/2,-1/2/sq2,sq3/sq2/2],
[sq3/sq2/2,1/2/sq2,-sq3/sq2/2,-1/2/sq2]]
];
for (var i=0; i<mrot0.length; i++)
mrot0[i]=new Matrix(mrot0[i]);
var colors4d=[[0.7,1.0,0.87,1],[1.0,0.8,0.87,1],[1.0,0.87,0.7,1],[0.87,1.0,0.7,1],[0.82,0.82,1.0,1],[0.9,1.0,0.9,1],[1.0,0.9,0.9,1],[0.94,0.94,0.94,1],[1.0,1.0,0.65,1],[1,0.9,0.8,1],[0.9,1,0.8,1],[0,0,0,1]]; // t2, t1, s2, s1, s3, r2, r1, r3, k, nd1, nd2, black
var numedges=6;
var tritype=nestedArray(3,numedges+1,-1); // color assignment depending on edges around triangle
for (var i=1; i<=numedges; i++)
for (var j=1; j<=numedges; j++)
for (var k=1; k<=numedges; k++)
tritype[i][j][k]=11;
tritype[1][1][1]=0;
tritype[2][2][2]=1;
tritype[2][3][1]=2;
tritype[3][1][2]=3;
tritype[1][2][3]=4;
tritype[4][2][1]=5;
tritype[1][4][2]=6;
tritype[2][1][4]=7;
tritype[3][3][3]=8;
tritype[1][3][5]=9;//xxxxxxxxxxxx
tritype[3][2][6]=10;//xxxxxxxxxxxx
var linecolors4d=[[0,0.5,0,1],[0.5,0,0,1],[0.4,0.4,0,1],[0,0,0,1]]; // color of lines: green, red, yellow, black
var mrot=new Matrix(1); // rotation matrix
var m1rot=new Matrix(1); // first derivative wrt time
var m2rot=new Matrix(1);// second derivative (for random anim)
var morth=
[[1., 1./2., 0, 0], [0, sq3/2., 0, 0], [0, 0, 1., 1./2.], [0, 0, 0, sq3/2.]];
var intens=1; // color intensity
function conv4d2d(c4d) // convert from 4d to 2d: normally it's done by the gpu but just in case
{
var scale=document.getElementById('current').state.scale;
var aspect=canvas.width/canvas.height;
var i,j,k;
var c2d=[0,0];
for (i=0; i<2; i++)
for (j=0; j<4; j++)
for (k=0; k<4; k++)
c2d[i]+=mrot.e(i,j)*morth[j][k]*c4d[k]; // eww. rewrite better
if (aspect<1) c2d[1]=c2d[1]*aspect; else c2d[0]=c2d[0]/aspect;
return [(scale*c2d[0]+1)*0.5*canvas.width,(-scale*c2d[1]+1)*0.5*canvas.height];
}
// mask values
// 1 = green paths
// 2 = red paths
// 4 = loops
// 8 = frame
// 16 = arrows/labels
// 32 = points
// 64 = green numbering
// 128 = red numbering
// 256 = edge numbering
// 512 = dual edge numbering
// 1024 = traditional labelling
// 2048*(1/2,4/8,16/32,64/128): directions of arrows (complemented/not complemented)
var maxmask=1024; // doesn't include arrows -- just the actual toggles
function drawSceneInternal(graph,mask,scale,shift)
{
currentProgram=-1;
var newProgram;
// set the active position buffer
gl.bindBuffer(gl.ARRAY_BUFFER, graph.coordbuffer);
for (var j=0; j<graph.data.length; j++)
if ((graph.data[j].flags&mask) == graph.data[j].flags)
{
newProgram=graph.data[j].type==gl.POINTS?1:0; // points are treated specially
if (newProgram!=currentProgram) // overkill because graph entries should be sorted. still, better this way
{
useProgram(newProgram);
uniform(gl.uniformMatrix4fv,'rotationMatrix',false,mrot.elem);
uniform(gl.uniform1f,'aspect',canvas.width/canvas.height);
uniform(gl.uniform1f,'scale',scale);
uniform(gl.uniform4fv,'shift',shift);
gl.vertexAttribPointer(shaderProgram[newProgram].vertexPositionAttribute, 4, gl.FLOAT, false, 0, 0); // not sure how useful
gl.polygonOffset(1.,1.); // so edges are visible over polygons.
if (newProgram==0) gl.depthMask(true); else gl.depthMask(false);
currentProgram=newProgram;
}
// set various parameters
if (graph.data[j].color!==undefined)
uniform(gl.uniform4fv,'currentColor', graph.data[j].color);
else
uniform(gl.uniform4f,'currentColor', 0,0,0,1); // default = black
if (graph.data[j].type==gl.TRIANGLES)
uniform(gl.uniform1f,'intens',intens);
else
uniform(gl.uniform1f,'intens',1);
if (newProgram==1) // points are treated specially
{
uniform(gl.uniform1i,'sampler', graph.data[j].sampler);
uniform(gl.uniform1f,'pointSize', graph.data[j].width); // width is size of "point"
}
else
{
if (graph.data[j].width!==undefined) gl.lineWidth(graph.data[j].width); // ... or thickness of lines
}
// then draw
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, graph.data[j].glbuffer);
gl.drawElements(graph.data[j].type, graph.data[j].n, gl.UNSIGNED_SHORT, 0);
}
}
// display function
function drawScene()
{
if (!gl) return;
// only change the size of the canvas if the size it's being displayed
// has changed.
var width = precanvas.clientWidth;
var height = precanvas.clientHeight;
if (canvas.width != width-10 || canvas.height != height-10)
{
// Change the size of the canvas to match the size it's being displayed
// 10 seems the minimum value to not trigger grid auto resize leading to infinite loop of size expansion
canvas.width = width-10;
canvas.height = height-10;
}
gl.viewport(0, 0, canvas.width, canvas.height);
// gl clear screen
gl.depthMask(true); // !!! otherwise won't clear the depth buffer...
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
var current=document.getElementById("current");
if (current===null) return;
var graph=current.state;
if (graph===undefined) return;
intens=document.getElementById('intens').value;
var i,mask;
// recompute mask (could be done on the fly but useless optimization)
mask=0; i=1;
while (i<2048)
{
if (document.getElementById("check"+i).checked) mask+=i;
i=2*i;
}
// then the complement stuff
for (i=1; i<=4; i++)
if (document.getElementById("y"+i+"comp").checked) mask+=2048<<(2*i-2); else mask+=2048<<(2*i-1);
// set rotation matrix
mrot.orthogonalize();
var aspect=canvas.width/canvas.height;
var aspectx,aspecty; if (aspect<1) { aspectx=1; aspecty=1/aspect; } else { aspectx=aspect; aspecty=1; }
if (graph instanceof Array) // panorama
{
var n=graph.length;
// n1*n2=n, n1/n2=aspect
var n2=Math.round(Math.sqrt(n/aspect)); // number of rows
if (n2>n) n2=n;
if (n2>canvas.height/60) n2=Math.round(canvas.height/60); // arbitrary max
var n1=Math.ceil(n/n2); // number of columns
if (n1>canvas.width/60) n1=Math.round(canvas.width/60); // arbitrary max
// if (n>n1*n2) n=n1*n2;
if ((n>n1*n2)&&(!current.anim)) n=n1*n2; // might as well not draw those outside the viewing area
var sc=graph.scale*(1+aspect)/(n2*aspect+n1);
for (i=0; i<n; i++)
{
i1=i%n1; i2=Math.floor(i/n1);
if (current.anim)
{
if (i!=current.sel)
drawSceneInternal(graph[i],mask,sc*(1-current.t),[((2*i1+1)/n1-1)*aspectx*(1-current.t),(-(2*i2+1)/n2+0.95)*aspecty*(1-current.t),-current.t,0.]);
else
drawSceneInternal(graph[current.sel],mask,graph.scale*current.t+sc*(1-current.t),[((2*i1+1)/n1-1)*aspectx*(1-current.t),(-(2*i2+1)/n2+0.95)*aspecty*(1-current.t),0.,0.]);
}
else
drawSceneInternal(graph[i],mask,sc,[((2*i1+1)/n1-1)*aspectx,(-(2*i2+1)/n2+0.95)*aspecty,0.,0.]);// 1 -> 0.95 (in y) to avoid the very top
}
}
else // normal puzzle
{
drawSceneInternal(graph,mask,graph.scale,[0.,0.,0.,0.]);
// if manual mode
if (graph.manual)
{
for (i=0; i<tiles.length; i++)
drawSceneInternal(tiles[i],mask&(1+2+4+32),0.15,[((2*i+1)/tiles.length-1)*aspectx,-0.85*aspecty,0.,0.]);
// now try to create a tile-shaped pointer...
gl.depthFunc(gl.ALWAYS); // temporarily turn off depth -- may have weird effects with some rotations, but ultimately makes sense
if ((graph.select>=0)&&(mouseX!==null)&&(mouseY!==null))
drawSceneInternal(tiles[graph.select],mask&(1+2+4+32),graph.scale,[(2*mouseX/canvas.width-1)*aspectx,(-2*mouseY/canvas.height+1)*aspecty,0.,0.]);
gl.depthFunc(gl.LEQUAL); // and back on
}
}
}
var randomanim;
var animcount;
var timerdelay=50;
var timerdelay2=20;
var nsteps=32;
//anim function
function rotateAnimFunc()
{
if (randomanim)
{
if (animcount==0) // beginning of a new cycle
{
m1rot.orthogonalize(); // once in a while... doesn't hurt
m1target=new Matrix(); m1target.randomorthog(0.002*timerdelay);
m2rot.computerotation(m1rot,m1target,nsteps);
animcount=nsteps;
}
m1rot.leftmultiply(m2rot);
}
if (animcount) // only the active window can be animated
{
mrot.leftmultiply(m1rot); // do the animation
animcount--;
drawScene();
setTimeout(rotateAnimFunc, timerdelay);
}
}
var processAnimFlag=false;
var shortenAnimFlag; // only well-defined when processAnimFunc is running
function processDelay()
{
if (shortenAnimFlag) return 0;
var as=+document.getElementById("speed").value;
if (as>9) return 0; else return 1000*(1/(1+as)-0.095);
}
function processAnimFunc()
{
if (!processAnimFlag) return; // to avoid all kinds of nasty bugs...
var res;
var but=icon.parentElement; // yeah, a bit tricky...
var graph;
var para;
var pd=processDelay();
var submit=document.getElementById("submit");
if (pd==0) // full speed
{
// just in case
submit.innerHTML="Abort"; // pressing on the button again will abort
submit.onclick=endAnim;
destroyButton(but); but=null;
while ((res=makePuzzle())>=0) ; // 0 (backtrack) or 1 (add one more tile)
}
else // normal speed animation
{
// just in case
submit.innerHTML="Finish"; // pressing on the button again will shorten animation (like speed = 11)
submit.onclick=shortenAnim;
animateIcon();
if (but===null) // should rarely occur
{
// create icon button
graph={};
but=createButton("*",graph); // make a new button which is currently displayed with empty corresponding puzzle
}
else
{
graph=but.state;
deleteBuffers(graph);
}
res=makePuzzle();
if (res!=-2) // -2 means the end
{
buildScene(graph,res<=0); // reset coords in case we've backtracked
if (but.id=='current')
drawScene(); // redraw scene in case we're watching the animation
}
}
if (res!=-2)
{
setTimeout(processAnimFunc, pd);
if (res==-1) // new puzzle
{
npuzzles++;
createButton(npuzzles,buildScene({},true),but,buildUrl()).state.parts=parts; // insert before but if !==null. add "parts" to state... should be done in create button, of course
// debug
if (document.getElementById('debug').getAttribute('hidden')==null) dump();// careful, returns "" if hidden, null if not
// end debug
}
}
else endAnim();
}
function makeCurrent(but) // make a puzzle the currently viewed one
{
var old=document.getElementById('current');
if (old)
{
old.removeAttribute("id");
old.style.backgroundColor="";
}
if (but)
{
but.id='current'; // we don't really do anything, just tag it
but.style.backgroundColor="#D0E0FF"; // and color the background
var parts;
if (document.getElementById("updateparts").checked && ((parts=but.state.parts)!==undefined))
{ // ... and possibly update the partitions
document.getElementById("heightrange").value=parts[0];
document.getElementById("widthrange").value=parts[1];
document.getElementById("height").innerHTML=parts[0]; // sadly, onchange doesn't work.....
document.getElementById("width").innerHTML=parts[1];
updateparam('double',parts.length==6);
resetYoung(parts.slice(2));
for (var i=0; i<4; i++)
if (document.getElementById("y"+(i+1)+"comp").checked)
y[i].set(y[i].complement().get()); //ewww
}
if (but.anim) // unlikely event that we interrupted a panorama anim; or start of reverse anim
setTimeout(panoramaAnimFunc, timerdelay2);
}
drawScene();
}
function shortenAnim()
{
shortenAnimFlag=true;
processAnimFunc();
}
function endAnim() // end of process animation, either forcefully or because ran out of puzzles to make
{
processAnimFlag=false; // to avoid all kinds of nasty bugs...
var but=icon.parentElement; // yeah, a bit tricky...
var para=document.getElementById('currentpara');
// destroy icon button
if (para)
{
var current=document.getElementById('current');
var flag=(current==but)||(current==para.firstElementChild)||(processDelay()==0); // whether we're staring at nothing interesting, or full speed
destroyButton(but);
if (npuzzles>1) // create a panorama if more than one puzzle
{
var g=new Array();
var x=para.firstElementChild;
g.scale=x.state.scale;
while (x=x.nextElementSibling) // skip the first = template
g.push(x.state);
x=createButton("S",g);
if (flag) makeCurrent(x); // if we're staring at the template, might as well switch to panorama
}
else if (flag) makeCurrent(para.lastElementChild);
// remove currentpara
para.removeAttribute("id");
}
else destroyButton(but);
//
var submit=document.getElementById("submit");
submit.innerHTML="Process";
submit.onclick=process;
}
function openAssoc(e)
{
e.preventDefault();
window.open(this.url);
}
function createButton(text,graph,before,url="") // creates a button which secretly contains all the graphical info to draw a puzzle
{ // a bit risky because if somehow the buttons gets recreated, info will be lost
var para=document.getElementById('currentpara');
var but=document.createElement('button');
but.onclick=loadPuzzle;
but.url=url;
but.onauxclick=openAssoc;
but.state=graph; // !
if (text=='*')
but.appendChild(icon);
else
but.innerHTML=text;
if (before)
para.insertBefore(but,before);
else
para.appendChild(but);
return but;
}
function destroyButton(but)
{
if (but!==null)
{
var para=but.parentElement;
var flag=(but.id=='current');
deleteBuffers(but.state);
var e;
while ((e=but.firstElementChild)!==null) // what is this about again?
but.removeChild(e);
para.removeChild(but);
if (flag) makeCurrent(para.lastElementChild); // go back to last if it exists. can't have no current... drawScene() requires it
}
}
//controls
function toggleAnimation()
{
if (animcount)
animcount=randomanim=0;
else
{
animcount=0;
randomanim=1;
setTimeout(rotateAnimFunc, timerdelay); // start animation
}
}
var rot90=new Matrix([[1,0,0,0],[0,1,0,0],[0,0,0,1],[0,0,-1,0]]);
function rotate90()
{
mrot.leftmultiply(rot90);
drawScene();
}
function presetView(k)
{
m1rot.computerotation(mrot,mrot0[k],nsteps);
animcount=nsteps; randomanim=0;
setTimeout(rotateAnimFunc, timerdelay); // start animation
}
var shft=[ // increments of 4d coords corresponding to edge states
[[0,1,0,0],[0,0,0,1],[1,0,-1,1],[-1,1,1,0]], // ori = 0
[[1,0,0,0],[0,0,1,0],[1,-1,0,1],[0,1,1,-1]], // ori = 1
[[-1,1,0,0],[0,0,-1,1],[0,1,-1,0],[-1,0,0,1]] // ori = 2
];
var y=new Array(4); // partitions
// variables for puzzle calculation. they are set by process()
var size1; var size2;
var size; // size of young diagram/puzzle (the ones currently processed, not! currently displayed)
var edge; // triple array of edge states. edge[i=0...size+2,j=0..size+2,ori=0..2] (i+j<=size+2): 0=undefined, 1=-, 2=+, 3=0, 4=0bar, 5 & 6 = extra undegen
var doublePuzzle=false;
// or by initPuzzle()
var t;
var npuzzles;
var edgebak; // for undo in manual
var quadedge;
function initPuzzle() // start the creation of all puzzles with given constraints
{
// make a stack of edges that are not fixed yet
varedge=new Array();
for (i=1; i<=size+1; i++)
for (j=1; doublePuzzle ? j<=size+1 : j<=size+1-i; j++)
for (k=0; k<3; k++)
if ((k==1 || j<size+1) && (k==0 || i<size+1)) // only for doublePuzzle really
if (edge[i][j][k]===0)
varedge.push([i,j,k]);
t=0; // where in the stack of var edges we are
npuzzles=0; // number of created puzzles
// create the array of possible edges. first list options for up/down pointing triangles. usual ordering: SE,SW,E
// closely related to tritype structured differently
var uptriedge=[[1,1,1],[2,2,2],[1,2,3],[2,3,1],[3,1,2]];
var downtriedge=[[1,1,1],[2,2,2],[1,2,3],[2,3,1],[3,1,2]];
if (document.getElementById("nondeg").checked)
{
uptriedge.push([1,3,5]);
downtriedge.push([1,3,5]);
uptriedge.push([3,2,6]);
downtriedge.push([3,2,6]);
}
if (document.getElementById("equiv").checked)
{
uptriedge.push([2,1,4]);
downtriedge.push([2,1,4]);
}
if (document.getElementById("equiv2").checked)
{
uptriedge.push([4,2,1]);
downtriedge.push([4,2,1]);
}
if (document.getElementById("equiv3").checked)
{
uptriedge.push([1,4,2]);
downtriedge.push([1,4,2]);
}
if (document.getElementById("K").checked)
{
uptriedge.push([3,3,3]);
}
if (document.getElementById("Kinv").checked)
{
downtriedge.push([3,3,3]);
}
// need to combine the two triangles into a bigger array which depends on the orientation of the edge.......
quadedge=[nestedArray(5,numedges+1,0),nestedArray(5,numedges+1,0),nestedArray(5,numedges+1,0)];
// now use zero as a wild card
for (i=0; i<uptriedge.length; i++)
for (j=0; j<downtriedge.length; j++)
{
if (uptriedge[i][0]==downtriedge[j][0])
for (k=0; k<32; k++)
quadedge[0][(k&1)*uptriedge[i][0]][((k>>1)&1)*uptriedge[i][2]][((k>>2)&1)*uptriedge[i][1]][((k>>3)&1)*downtriedge[j][2]][((k>>4)&1)*downtriedge[j][1]]++;
if (uptriedge[i][1]==downtriedge[j][1])
for (k=0; k<32; k++)
quadedge[1][(k&1)*uptriedge[i][1]][((k>>1)&1)*uptriedge[i][0]][((k>>2)&1)*uptriedge[i][2]][((k>>3)&1)*downtriedge[j][0]][((k>>4)&1)*downtriedge[j][2]]++;
if (uptriedge[i][2]==downtriedge[j][2])
for (k=0; k<32; k++)
quadedge[2][(k&1)*uptriedge[i][2]][((k>>1)&1)*uptriedge[i][1]][((k>>2)&1)*uptriedge[i][0]][((k>>3)&1)*downtriedge[j][1]][((k>>4)&1)*downtriedge[j][0]]++;
}
}
var parts;
function makePuzzle() // one step in the making of a puzzle
{ // returns -2 if finished w/ all puzzles, -1 if found a new one, 0 if backtracked, 1 if added one tile
var i,j,k,l;
function findnext(i,j,k,l) // used internally by makePuzzle: sets a new edge if possible, or unset it if not
{
var lmax;
if (k==2)
{
if (doublePuzzle || i+j<size+1) lmax=numedges; else lmax=2; // whether boundary edge or not
while (l<=lmax && quadedge[2][l][edge[i][j][1]][edge[i][j][0]][edge[i][j+1][1]][edge[i+1][j][0]]==0) l++;
}
else if (k==1)
{
if (j>1 && j<size+1) lmax=numedges; else lmax=2;
while (l<=lmax && quadedge[1][l][edge[i][j][0]][edge[i][j][2]][edge[i+1][j-1][0]][edge[i][j-1][2]]==0) l++;
}
else if (k==0)
{
if (i>1 && i<size+1) lmax=numedges; else lmax=2;
while (l<=lmax && quadedge[0][l][edge[i][j][2]][edge[i][j][1]][edge[i-1][j][2]][edge[i-1][j+1][1]]==0) l++;
}
if (l<=lmax) { edge[i][j][k]=l; return true; } else { edge[i][j][k]=0; return false; }
}
// we want to fix a new edge: find which has fewest possibilities
var tmin; var c; var cmin=1000000;
for (tt=t; tt<varedge.length; tt++)
{
i=varedge[tt][0]; j=varedge[tt][1]; k=varedge[tt][2];
if (k==2)
c=quadedge[2][0][edge[i][j][1]][edge[i][j][0]][edge[i][j+1][1]][edge[i+1][j][0]]; // edge[i][j][2] SHOULD be zero!
else if (k==1)
c=quadedge[1][0][edge[i][j][0]][edge[i][j][2]][edge[i+1][j-1][0]][edge[i][j-1][2]]; // edge[i][j][1] SHOULD be zero
else if (k==0)
c=quadedge[0][0][edge[i][j][2]][edge[i][j][1]][edge[i-1][j][2]][edge[i-1][j+1][1]]; // edge[i][j][0] SHOULD be zero
if (c<cmin) { cmin=c; tmin=tt; if (c==0) break; }
}
if (t==varedge.length || cmin==0 || !findnext(i=varedge[tmin][0],j=varedge[tmin][1],k=varedge[tmin][2],1))
{
// backtracking
while (t>0)
{
i=varedge[t-1][0]; j=varedge[t-1][1]; k=varedge[t-1][2];
if (findnext(i,j,k,edge[i][j][k]+1))
break;
t--;
}
if (t==0) return -2; // the end: can't backtrack any further
else if (t<varedge.length) return 0; // successful backtrack
}
else
{ // mark the new edge as set
// permute tmin and t, increase t
varedge[tmin][0]=varedge[t][0]; varedge[tmin][1]=varedge[t][1]; varedge[tmin][2]=varedge[t][2];
varedge[t][0]=i; varedge[t][1]=j; varedge[t][2]=k; t++;
}
if (t==varedge.length)
{ // full puzzle: should compute the 3/4 partitions (and check they're in the right box)
var y1p,y2p,y3p,y4p;
y1p=[];
for (i=1; i<=size; i++)
if (edge[i][1][1]==1) y1p.push(size-i-size1+1+y1p.length);
if (y1p.length!=size1) return 1;
while (y1p.length>0 && y1p[y1p.length-1]==0) y1p.pop();
y2p=[];
for (i=size; i>=1; i--)
if (edge[1][i][0]==1) y2p.push(i-size1+y2p.length);
if (y2p.length!=size1) return 1;
while (y2p.length>0 && y2p[y2p.length-1]==0) y2p.pop();
if (doublePuzzle)
{
y3p=[];
for (i=size; i>=1; i--)
if (edge[i][size+1][1]==1) y3p.push(i-size1+y3p.length);
if (y3p.length!=size1) return 1;
while (y3p.length>0 && y3p[y3p.length-1]==0) y3p.pop();
y4p=[];
for (i=1; i<=size; i++)
if (edge[size+1][i][0]==1) y4p.push(size-i-size1+1+y4p.length);
if (y4p.length!=size1) return 1;
while (y4p.length>0 && y4p[y4p.length-1]==0) y4p.pop();
parts=[size1,size2,y1p,y2p,y3p,y4p];
return -1;
}
else
{
y3p=[];
for (i=1; i<=size; i++)
if (edge[size+1-i][i][2]==1) y3p.push(size-i-size1+1+y3p.length);
if (y3p.length!=size1) return 1;
while (y3p.length>0 && y3p[y3p.length-1]==0) y3p.pop();
parts=[size1,size2,y1p,y2p,y3p]; // this is the info "parts" that will be contained in button.state for "update partitions from puzzle"
return -1;
}
}
return 1; // added one tile, nothing special happened
}
function loadPuzzle() // load an existing puzzle. called by clicking on a button
{
makeCurrent(this);
}
var ncoords;
var coords;
var vind;
var midpt;
function initCoords(center)
{
var i,j,framelen;
// first make a big empty table of vertex indices
vind=new Array(size+2); for (i=0; i<=size+1; i++) vind[i]=new Array(size+2); // vind[i][j] === undefined. indices = matrix style 1..size+1
// arbitrarily set the coordinate of one vertex
if (center) // if coordinates are gonna be centered we don't worry too much
{
i=1; j=1; while ((i<=size)&&(edge[i][j][0]==0)&&(edge[i][j][1]==0)) { j++; if (j>size) {j=1; i++;} } // find a vertex adjacent to an occupied edge
vind[i][j]=0;
coords=[0,0,0,0]; ncoords=1; // ncoords=coords.length/4;
}
else
{ // if not, it's harder: we're gonna use the frame
if (doublePuzzle)
{
coords=[
size1/2,-size1/2,size2/2,-size2/2,
-size1/2,-size1/2,size2/2,-size2/2,
-size1/2,-size1/2,-size2/2,-size2/2,
-size1/2,size1/2,-size2/2,-size2/2,
-size1/2,size1/2,-size2/2,size2/2,
size1/2,size1/2,-size2/2,size2/2,
size1/2,size1/2,size2/2,size2/2,
size1/2,-size1/2,size2/2,size2/2
];
vind[1][1]=2;
vind[1][size+1]=4;
vind[size+1][size+1]=6;
vind[size+1][1]=0;
}
else
{
coords=[
2*size1/3,-size1/3,2*size2/3,-size2/3,
-size1/3,-size1/3,2*size2/3,-size2/3,
-size1/3,-size1/3,-size2/3,-size2/3,
-size1/3,2*size1/3,-size2/3,-size2/3,
-size1/3,2*size1/3,-size2/3,2*size2/3,
2*size1/3,-size1/3,-size2/3,2*size2/3
];
vind[1][1]=2;
vind[1][size+1]=4;
vind[size+1][1]=0;
}
// coords of arrows/labels too
framelen=ncoords=coords.length/4;
for (j=0; j<framelen/2; j++)
{
// arrow body
for (i=0; i<4; i++)
coords.push(coords[4*(j*2+1)+i]+0.1*coords[4*((j*2+2)%framelen)+i]);
// coords.push(0.65*coords[4*(j*2)+i]+0.5*coords[4*((j*2+2)%framelen)+i]);
for (i=0; i<4; i++)
coords.push(coords[4*(j*2+1)+i]+0.1*coords[4*(j*2)+i]);
// coords.push(0.5*coords[4*(j*2)+i]+0.65*coords[4*((j*2+2)%framelen)+i]);
// now determine the tip of the arrow: should depend on how one reads diagrams. reverse tip
for (i=0; i<4; i++)
coords.push(coords[4*(j*2+1)+i]+0.05*coords[4*(j*2)+i]);
//
for (i=0; i<4; i++)
coords.push(coords[4*(j*2+1)+i]+0.05*coords[4*((j*2+2)%framelen)+i]+0.1*coords[4*(j*2)+i]);
//
// default tip
for (i=0; i<4; i++)
coords.push(coords[4*(j*2+1)+i]+0.05*coords[4*((j*2+2)%framelen)+i]);
// coords.push(0.6*coords[4*(j*2)+i]+0.5*coords[4*((j*2+2)%framelen)+i]);
for (i=0; i<4; i++)
coords.push(coords[4*(j*2+1)+i]+0.05*coords[4*(j*2)+i]+0.1*coords[4*((j*2+2)%framelen)+i]);
// coords.push(0.65*coords[4*(j*2)+i]+0.55*coords[4*((j*2+2)%framelen)+i]);
// location of label
for (i=0; i<4; i++)
coords.push(coords[4*(j*2+1)+i]+0.15*coords[4*(j*2)+i]+0.15*coords[4*((j*2+2)%framelen)+i]);
// coords.push(0.5*coords[4*(j*2)+i]+0.5*coords[4*((j*2+2)%framelen)+i]+0.15*coords[4*(j*2)+i]+0.15*coords[4*((j*2+2)%framelen)+i]);
//
ncoords+=7;
}
}
// empty as well the index array for midpoints (and little arrows)
midpt={};
}
function updateCoords() // try to find new coordinates of vertices using known edges
{
var ncoords0; //var count=0;
do {
ncoords0=ncoords;
for (i=1; i<=size+1; i++)
for (j=1; j<=size+2; j++)
if (vind[i][j]===undefined)
{
vind[i][j]=ncoords; ncoords++; // just in case...
if (edge[i][j-1][0]>0 && (k=vind[i][j-1])!==undefined) { for (l=0; l<4; l++) coords.push(coords[k*4+l]+shft[0][edge[i][j-1][0]-1][l]); }
else if (edge[i-1][j][1]>0 && (k=vind[i-1][j])!==undefined) { for (l=0; l<4; l++) coords.push(coords[k*4+l]+shft[1][edge[i-1][j][1]-1][l]); }
else if (edge[i][j-1][2]>0 && (k=vind[i+1][j-1])!==undefined) { for (l=0; l<4; l++) coords.push(coords[k*4+l]+shft[2][edge[i][j-1][2]-1][l]); }
else if (edge[i-1][j][2]>0 && (k=vind[i-1][j+1])!==undefined) { for (l=0; l<4; l++) coords.push(coords[k*4+l]-shft[2][edge[i-1][j][2]-1][l]); }
else if (edge[i][j][0]>0 && (k=vind[i][j+1])!==undefined) { for (l=0; l<4; l++) coords.push(coords[k*4+l]-shft[0][edge[i][j][0]-1][l]); }
else if (edge[i][j][1]>0 && (k=vind[i+1][j])!==undefined) { for (l=0; l<4; l++) coords.push(coords[k*4+l]-shft[1][edge[i][j][1]-1][l]); }
else { ncoords--; vind[i][j]=undefined; } // actually, never mind...
}
}
while (ncoords>ncoords0);
}
function buildUrl() // build an url to call the associativity thing
{
var url="../assoc/?n="+size+"&hcol=";
for (i=1; i<=size+1; i++)
for (j=1; j<=size; j++)
if (i+j<=size+1)
url+=(edge[i][j][0])+","; else url+=((edge[i][j][0]+2)%4)+",";
url+="&vcol=";
for (i=1; i<=size; i++)
for (j=1; j<=size+1; j++)
if (i+j<=size+1)