-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
1874 lines (1704 loc) · 70.4 KB
/
index.html
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
<html>
<head>
<title>CSE 150 Demo</title>
<style>
td{
text-align: center;
width:50px;
}
ul
{
list-style-type: none;
}
</style>
<link href="css/bootstrap.css" rel="stylesheet" media="screen">
<link href="css/hover.css" rel="stylesheet" media="all">
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script src="js/bootstrap.js"></script>
<script src="lib/arbor.js"></script>
<script src="_/graphics.js"></script>
<!-- <script src="main.js"></script>-->
<script>
var image_clicked = false;
var Graph_Number = 0;
//Declare all the variables
var nodeA;
var nodeB;
var nodeC;
var nodeD;
var nodeE;
var nodeF;
var nodeG;
var nodeH;
var NodeList = [];
var ExampleNodeList = [];
var EdgeList = [];
var ExampleEdgeList = [];
var edgeAC;
var edgeCF;
var edgeBF;
var edgeCG;
var edgeAD;
var edgeDG;
var edgeDH;
var edgeEH;
var print_path_result = "";
var PositionsNodeList = [];
PositionsNodeList[0] = {X: 210, Y: 20}; //A
PositionsNodeList[1] = {X: 30, Y: 90}; //B
PositionsNodeList[2] = {X: 150, Y: 90}; //C
PositionsNodeList[3] = {X: 270, Y: 90}; //D
PositionsNodeList[4] = {X: 390, Y: 90}; //E
PositionsNodeList[5] = {X: 90, Y: 160}; //F
PositionsNodeList[6] = {X: 210, Y: 160}; //G
PositionsNodeList[7] = {X: 330, Y: 160}; //H
edgeAC = new Edge("AC", "A", "C", false, false);
edgeCF = new Edge("CF", "C", "F", false, false);
edgeBF = new Edge("BF", "B", "F", false, false);
edgeCG = new Edge("CG", "C", "G", false, false);
edgeAD = new Edge("AD", "A", "D", false, false);
edgeDG = new Edge("DG", "D" ,"G", false, false);
edgeDH = new Edge("DH", "D", "H", false, false);
edgeEH = new Edge("EH", "E", "H", false, false);
EdgeList = [edgeAC, edgeCF, edgeBF, edgeCG, edgeAD, edgeDG, edgeDH,edgeEH];
/**
* Class: Node
**/
function Node(name, x, y, mass, evidence, target, targetY, visited){
this.name = name;
this.x = x;
this.y = y;
this.mass = mass;
//Red for evidence set
this.evidence = evidence;
//Blue for x set
this.target = target;
//Green for y set
this.targetY = targetY;
this.visited = visited;
}
/**
* Class: Edge
**/
function Edge(name, from, to, highlighted, rule){
this.name = name;
this.from = from;
this.to = to;
//Variable to control whether the path will be grey or black
this.rule = rule;
this.highlighted = highlighted;
}
var particleSystem;
(function($){
var Renderer = function(canvas){
var canvas = $(canvas).get(0)
var ctx = canvas.getContext("2d");
var gfx = arbor.Graphics(canvas);
var that = {
init:function(system){
//
// the particle system will call the init function once, right before the
// first frame is to be drawn. it's a good place to set up the canvas and
// to pass the canvas size to the particle system
//
// save a reference to the particle system for use in the .redraw() loop
particleSystem = system;
// inform the system of the screen dimensions so it can map coords for us.
// if the canvas is ever resized, screenSize should be called again with
// the new dimensions
particleSystem.screenSize(canvas.width, canvas.height)
particleSystem.screenPadding(75) // leave an extra 80px of whitespace per side
// set up some event handlers to allow for node-dragging
that.initMouseHandling()
},
redraw:function(){
//
// redraw will be called repeatedly during the run whenever the node positions
// change. the new positions for the nodes can be accessed by looking at the
// .p attribute of a given node. however the p.x & p.y values are in the coordinates
// of the particle system rather than the screen. you can either map them to
// the screen yourself, or use the convenience iterators .eachNode (and .eachEdge)
// which allow you to step through the actual node objects but also pass an
// x,y point in the screen's coordinate system
//
/*for(var i = 0; i < NodeList.length; i++){
particleSystem.pruneNode(NodeList[i].name);
}*/
for (var i = 0; i < NodeList.length; i++){
//alert("e");
var tmp = NodeList[i];
particleSystem.addNode(tmp.name, {name: tmp.name, x: tmp.x, y: tmp.y, mass: 2, evidence: tmp.evidence, target: tmp.target, targetY: tmp.targetY});
}
for (var i = 0; i < EdgeList.length; i++){
var tmp = EdgeList[i];
//console.log(tmp.highlighted);
var from = particleSystem.getNode(tmp.from);
var to = particleSystem.getNode(tmp.to);
particleSystem.addEdge(from, to, {weight: 2, name: tmp.name, highlighted: tmp.highlighted, rule: tmp.rule});
}
for(var i = 0; i < ExampleNodeList.length; i++){
var tmp = ExampleNodeList[i];
particleSystem.addNode(tmp.name, {name: tmp.name, x: tmp.x, y: tmp.y, mass: 2, evidence: tmp.evidence, target: tmp.target, targetY: tmp.targetY});
}
for(var i = 0; i < ExampleEdgeList.length; i++){
var tmp = ExampleEdgeList[i];
var from = particleSystem.getNode(tmp.from);
var to = particleSystem.getNode(tmp.to);
particleSystem.addEdge(from, to, {weight: 2, name: tmp.name, highlighted: tmp.highlighted, rule: tmp.rule});
}
ctx.fillStyle = "white";
ctx.fillRect(0,0, canvas.width, canvas.height)
var nodeBoxes = {}
particleSystem.eachNode(function(node, pt){
// node: {mass:#, p:{x,y}, name:"", data:{}}
// pt: {x:#, y:#} node position in screen coords
// draw a rectangle centered at pt
var w = 10
if(node.data.evidence){
ctx.strokeStyle = "black";
ctx.fillStyle = "#cccccc";
}
else if(node.data.target){
ctx.strokeStyle = "blue";
ctx.fillStyle = "white";
}
else if(node.data.targetY){
ctx.strokeStyle = "green";
ctx.fillStyle = "white";
}
else{
ctx.strokeStyle = "black";
ctx.fillStyle = "white";
}
//ctx.fillStyle = (node.data.evidence) ? "red" : "black"
//ctx.fillRect(pt.x-w/2, pt.y-w/2, w,w)
ctx.beginPath()
ctx.arc(node.p.x, node.p.y, 10, 0, 2*Math.PI);
ctx.fill();
ctx.fillStyle ="black";
ctx.fillText(node.data.name, node.p.x+15, node.p.y-3);
ctx.stroke();
nodeBoxes[node.name] = [node.p.x, node.p.y, 22, 15]
})
particleSystem.eachEdge(function(edge, pt1, pt2){
// edge: {source:Node, target:Node, length:#, data:{}}
// pt1: {x:#, y:#} source position in screen coords
// pt2: {x:#, y:#} target position in screen coords
// draw a line from pt1 to pt2
var weight = edge.data.weight
var start = nodeBoxes[edge.data.name.charAt(0)]
var end = nodeBoxes[edge.data.name.charAt(1)]
ctx.save()
ctx.beginPath()
ctx.lineWidth = (!isNaN(weight)) ? parseFloat(weight) : 1;
var currentEdge = findEdgeByName(edge.data.name)
if(currentEdge != null){
if(highlight_path){
if(currentEdge.highlighted)
ctx.strokeStyle = "#FF6666";
else
ctx.strokeStyle = "#cccccc";
}
else{
ctx.strokeStyle = "black";
}
}
else{
ctx.strokeStyle = "black";
}
ctx.fillStyle = null
var startx = start[0];
var starty = start[1];
//The node is top right of the end node
if(start[0] > end[0] && start[1] < end[1]){
startx = start[0] - 6;
starty = start[1] + 6;
}
//Top left
else if(start[0] < end[0] && start[1] < end[1]){
startx = start[0] + 8;
starty = start[1] + 6;
}
//On the right side
else if(start[0] < end[0] && start[1] == end[1]){
startx = start[0] + 10;
//Bottom left
}else if(start[0] < end[0] && start[1] > end[1]){
startx = start[0] + 10;
starty = start[1] - 6;
}
//Bottom right
else if(start[0] > end[0] && start[1] > end[1]){
startx = start[0] - 10;
starty = start[1] - 6;
}
//Direct bottom
else if(start[0] == end[0] && start[1] > end[1]){
starty = start[1] - 10;
}
//Direct top
else if(start[0] == end[0] && start[1] < end[1]){
starty = start[1] + 10;
}
//On the left side
else if(start[0] > end[0] && start[1] == end[1]){
startx = start[0] - 10;
}
ctx.moveTo(startx, starty)//Tail
var endx = end[0];
var endy = end[1];
//If start node is at the right side of end node
if(start[0] > end[0] && start[1] == end[1]){
endx = end[0] + 12;
}
//If the end node is right above
else if(start[0] == end[0] && start[1] > end[1]){
//endx = end[0] + 2;
endy = end[1] + 10;
}
//If the end node is right below
else if(start[0] == end[0] && start[1] < end[1]){
//endx = end[0] + 2;
endy = end[1] - 10;
}
else if(start[1] > end[1] && start[0] > end[0]){
endy = end[1] + 10;
endx = end[0] + 5;
}
else if(start[0] > end[0]){//The node is on the left side
endx = end[0] + 10;
endy = end[1] - 10;
}
else if(start[1] == end[1] && start[0] < end[0]){
endx = end[0] - 12;
}
else if(start[1] > end[1] && start[0] < end[0]){
endy = end[1] + 10;
endx = end[0] - 7;
}
else{
endx = end[0] - 10;
endy = end[1] - 10;
}
//ctx.lineTo(end[0]-5, end[1]-5)//Head
ctx.lineTo(endx, endy)
ctx.stroke()
ctx.restore()
// draw an arrowhead if this is a -> style edge
ctx.save()
// move to the head position of the edge we just drew
var wt = !isNaN(weight) ? parseFloat(weight) : 1
var arrowLength = 12 + wt;
var arrowWidth = 4 + wt;
if(currentEdge != null){
if(highlight_path){
if(currentEdge.highlighted)
ctx.fillStyle = "#FF6666";
else
ctx.fillStyle = "#cccccc";
}
else{
ctx.fillStyle = "black";
}
}
else{
ctx.fillStyle = "black";
}
ctx.translate(endx, endy);
ctx.rotate(Math.atan2(endy - start[1], endx - start[0]));
// delete some of the edge that's already there (so the point isn't hidden)
ctx.clearRect(-arrowLength/2,-wt/2, arrowLength/2,wt)
// draw the chevron
ctx.beginPath();
ctx.moveTo(-arrowLength, arrowWidth);
ctx.lineTo(0, 0);
ctx.lineTo(-arrowLength, -arrowWidth);
ctx.lineTo(-arrowLength * 0.8, -0);
ctx.closePath();
ctx.fill();
ctx.restore()
})
},
initMouseHandling:function(){
// no-nonsense drag and drop (thanks springy.js)
var dragged = null;
// set up a handler object that will initially listen for mousedowns then
// for moves and mouseups while dragging
var handler = {
clicked:function(e){
var pos = $(canvas).offset();
_mouseP = arbor.Point(e.pageX-pos.left, e.pageY-pos.top)
dragged = particleSystem.nearest(_mouseP);
if (dragged && dragged.node !== null){
// while we're dragging, don't let physics move the node
dragged.node.fixed = true
}
$(canvas).bind('mousemove', handler.dragged)
$(window).bind('mouseup', handler.dropped)
return false
},
dragged:function(e){
var pos = $(canvas).offset();
var s = arbor.Point(e.pageX-pos.left, e.pageY-pos.top)
if (dragged && dragged.node !== null){
var p = particleSystem.fromScreen(s)
dragged.node.p = p
}
return false
},
dropped:function(e){
if (dragged===null || dragged.node===undefined) return
if (dragged.node !== null) dragged.node.fixed = false
dragged.node.tempMass = 1000
dragged = null
$(canvas).unbind('mousemove', handler.dragged)
$(window).unbind('mouseup', handler.dropped)
_mouseP = null
return false
}
}
// start listening
$(canvas).mousedown(handler.clicked);
},
}
return that
}
// helpers for figuring out where to draw arrows (thanks springy.js)
var intersect_line_line = function(p1, p2, p3, p4)
{
var denom = ((p4.y - p3.y)*(p2.x - p1.x) - (p4.x - p3.x)*(p2.y - p1.y));
if (denom === 0) return false // lines are parallel
var ua = ((p4.x - p3.x)*(p1.y - p3.y) - (p4.y - p3.y)*(p1.x - p3.x)) / denom;
var ub = ((p2.x - p1.x)*(p1.y - p3.y) - (p2.y - p1.y)*(p1.x - p3.x)) / denom;
if (ua < 0 || ua > 1 || ub < 0 || ub > 1) return false
return arbor.Point(p1.x + ua * (p2.x - p1.x), p1.y + ua * (p2.y - p1.y));
}
var intersect_line_box = function(p1, p2, boxTuple)
{
var p3 = {x:boxTuple[0], y:boxTuple[1]},
w = boxTuple[2],
h = boxTuple[3]
var tl = {x: p3.x, y: p3.y};
var tr = {x: p3.x + w, y: p3.y};
var bl = {x: p3.x, y: p3.y + h};
var br = {x: p3.x + w, y: p3.y + h};
return intersect_line_line(p1, p2, tl, tr) ||
intersect_line_line(p1, p2, tr, br) ||
intersect_line_line(p1, p2, br, bl) ||
intersect_line_line(p1, p2, bl, tl) ||
false
}
$(document).ready(function(){
var sys = arbor.ParticleSystem(0, 0, 0.5) // create the system with sensible repulsion/stiffness/friction
sys.parameters({gravity:true}) // use center-gravity to make the graph settle nicely (ymmv)
sys.renderer = Renderer("#viewport") // our newly created renderer will have its .init() method called shortly by sys...
})
})(this.jQuery)
//Init condition, set up edges and nodes
function init(){
for(var i = 0; i < PositionsNodeList.length; i++){
console.log(PositionsNodeList[i].X + " " + PositionsNodeList[i].Y);
}
//name, x, y, mass, evidence, target, targetY){
nodeA = new Node("A", PositionsNodeList[0].X, PositionsNodeList[0].Y, 2, false, false, false, false);
nodeB = new Node("B", PositionsNodeList[1].X, PositionsNodeList[1].Y, 2, false, false, false, false);
nodeC = new Node("C", PositionsNodeList[2].X, PositionsNodeList[2].Y, 2, false, false, false, false);
nodeD = new Node("D", PositionsNodeList[3].X, PositionsNodeList[3].Y, 2, false, false, false, false);
nodeE = new Node("E", PositionsNodeList[4].X, PositionsNodeList[4].Y, 2, false, false, false, false);
nodeF = new Node("F", PositionsNodeList[5].X, PositionsNodeList[5].Y, 2, false, false, false, false);
nodeG = new Node("G", PositionsNodeList[6].X, PositionsNodeList[6].Y, 2, false, false, false, false);
nodeH = new Node("H", PositionsNodeList[7].X, PositionsNodeList[7].Y, 2, false, false, false, false);
/*edgeAC = new Edge("AC", "A", "C", false, false);
edgeCF = new Edge("CF", "C", "F", false, false);
edgeBF = new Edge("BF", "B", "F", false, false);
edgeCG = new Edge("CG", "C", "G", false, false);
edgeAD = new Edge("AD", "A", "D", false, false);
edgeDG = new Edge("DG", "D" ,"G", false, false);
edgeDH = new Edge("DH", "D", "H", false, false);
edgeEH = new Edge("EH", "E", "H", false, false);
EdgeList = [edgeAC, edgeCF, edgeBF, edgeCG, edgeAD, edgeDG, edgeDH,edgeEH]; */
//Here's for the three possible blocking path
/*//Rule #1, intervening cause
node1 = new Node("1", 720, 50, 2, false, false, false);
node2 = new Node("2", 795, 50, 2, true, false, false);
node3 = new Node("3", 870, 50, 2, false, false, false);
edge12 = new Edge("12", "1", "2", false, true);
edge23 = new Edge("23", "2", "3", false, true);
//Rule #2, comman cause
node4 = new Node("4", 720, 100, 2, false, false, false);
node5 = new Node("5", 795, 100, 2, true, false, false);
node6 = new Node("6", 870, 100, 2, false, false, false);
edge54 = new Edge("54", "5", "4", false, true);
edge56 = new Edge("56", "5", "6", false, true);
//Rule #3, no observed common effect
node7 = new Node("7", 720, 150, 2, false, false, false);
node8 = new Node("8", 795, 150, 2, false, false, false);
node9 = new Node("9", 870, 150, 2, false, false, false);
node10 = new Node("W", 760, 200, 2, false, false, false);
node11 = new Node("K", 835, 200, 2, false, false, false);
edge78 = new Edge("78", "7", "8", false, true);
edge98 = new Edge("98", "9", "8", false, true);
edge8W = new Edge("8W", "8", "W", false, true);
edge8K = new Edge("8K", "8", "K", false, true);
ExampleNodeList = [node1, node2, node3, node4, node5, node6, node7, node8, node9, node10, node11];
ExampleEdgeList = [edge12, edge23, edge54, edge56, edge78, edge98, edge8K, edge8W];
*/
NodeList = [nodeA, nodeB, nodeC, nodeD, nodeE, nodeF, nodeG, nodeH]
//alert("init");
}
function changeGraph(number){
highlight_path = false;
if(!image_clicked){
image_clicked = true;
}
if(Graph_Number == 0){
}else{
canvas = document.getElementById('viewport');
ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
for(var i = 0; i < NodeList.length; i++){
NodeList.pop();
}
for(var i = 0; i < EdgeList.length; i++){
EdgeList.pop();
}
particleSystem.pruneNode("A");
particleSystem.pruneNode("B");
particleSystem.pruneNode("C");
particleSystem.pruneNode("D");
particleSystem.pruneNode("E");
particleSystem.pruneNode("F");
particleSystem.pruneNode("G");
particleSystem.pruneNode("H");
//alert("Clear");
}
/*if(Graph_Number < 5){
Graph_Number += 1;
}
else{
Graph_Number = 1;
}*/
Graph_Number = number;
if(Graph_Number == 1){
//Here's for the three possible blocking path
for(var i = 0; i < PositionsNodeList.length; i++){
PositionsNodeList.pop();
}
PositionsNodeList[0] = {X: 210, Y: 20}; //A
PositionsNodeList[1] = {X: 30, Y: 90}; //B
PositionsNodeList[2] = {X: 150, Y: 90}; //C
PositionsNodeList[3] = {X: 270, Y: 90}; //D
PositionsNodeList[4] = {X: 390, Y: 90}; //E
PositionsNodeList[5] = {X: 90, Y: 160}; //F
PositionsNodeList[6] = {X: 210, Y: 160}; //G
PositionsNodeList[7] = {X: 330, Y: 160}; //H
nodeA = new Node("A", PositionsNodeList[0].X, PositionsNodeList[0].Y, 2, false, false, false, false);
nodeB = new Node("B", PositionsNodeList[1].X, PositionsNodeList[1].Y, 2, false, false, false, false);
nodeC = new Node("C", PositionsNodeList[2].X, PositionsNodeList[2].Y, 2, false, false, false, false);
nodeD = new Node("D", PositionsNodeList[3].X, PositionsNodeList[3].Y, 2, false, false, false, false);
nodeE = new Node("E", PositionsNodeList[4].X, PositionsNodeList[4].Y, 2, false, false, false, false);
nodeF = new Node("F", PositionsNodeList[5].X, PositionsNodeList[5].Y, 2, false, false, false, false);
nodeG = new Node("G", PositionsNodeList[6].X, PositionsNodeList[6].Y, 2, false, false, false, false);
nodeH = new Node("H", PositionsNodeList[7].X, PositionsNodeList[7].Y, 2, false, false, false, false);
NodeList = [nodeA, nodeB, nodeC, nodeD, nodeE, nodeF, nodeG, nodeH];
edgeAC = new Edge("AC", "A", "C", false, false);
edgeCF = new Edge("CF", "C", "F", false, false);
edgeBF = new Edge("BF", "B", "F", false, false);
edgeCG = new Edge("CG", "C", "G", false, false);
edgeAD = new Edge("AD", "A", "D", false, false);
edgeDG = new Edge("DG", "D" ,"G", false, false);
edgeDH = new Edge("DH", "D", "H", false, false);
edgeEH = new Edge("EH", "E", "H", false, false);
EdgeList = [edgeAC, edgeCF, edgeBF, edgeCG, edgeAD, edgeDG, edgeDH,edgeEH];
}else if (Graph_Number == 2){
var gap_x = 70;
PositionsNodeList[0] = {X: 40, Y: 20}; //A
PositionsNodeList[1] = {X: 180, Y: 20}; //B
PositionsNodeList[2] = {X: 320, Y: 20}; //C
PositionsNodeList[3] = {X: 110, Y: 90}; //D
PositionsNodeList[4] = {X: 250, Y: 90}; //E
PositionsNodeList[5] = {X: 40, Y: 160}; //F
PositionsNodeList[6] = {X: 180, Y: 160}; //G
PositionsNodeList[7] = {X: 320, Y: 160}; //H
nodeA = new Node("A", PositionsNodeList[0].X, PositionsNodeList[0].Y, 2, false, false, false, false);
nodeB = new Node("B", PositionsNodeList[1].X, PositionsNodeList[1].Y, 2, false, false, false, false);
nodeC = new Node("C", PositionsNodeList[2].X, PositionsNodeList[2].Y, 2, false, false, false, false);
nodeD = new Node("D", PositionsNodeList[3].X, PositionsNodeList[3].Y, 2, false, false, false, false);
nodeE = new Node("E", PositionsNodeList[4].X, PositionsNodeList[4].Y, 2, false, false, false, false);
nodeF = new Node("F", PositionsNodeList[5].X, PositionsNodeList[5].Y, 2, false, false, false, false);
nodeG = new Node("G", PositionsNodeList[6].X, PositionsNodeList[6].Y, 2, false, false, false, false);
nodeH = new Node("H", PositionsNodeList[7].X, PositionsNodeList[7].Y, 2, false, false, false, false);
NodeList = [nodeA, nodeB, nodeC, nodeD, nodeE, nodeF, nodeG, nodeH];
edgeAD = new Edge("AD", "A", "D", false, false);
edgeBD = new Edge("BD", "B", "D", false, false);
edgeBE = new Edge("BE", "B", "E", false, false);
edgeCE = new Edge("CE", "C", "E", false, false);
edgeFD = new Edge("FD", "F", "D", false, false);
edgeDG = new Edge("DG", "D" ,"G", false, false);
edgeEG = new Edge("EG", "E", "G", false, false);
edgeHE = new Edge("HE", "H", "E", false, false);
EdgeList = [edgeAD, edgeBD, edgeBE, edgeCE, edgeFD, edgeDG, edgeEG,edgeHE];
//alert("Changing Graph");
}
else if(Graph_Number == 3){
var gap = 90
PositionsNodeList[0] = {X: 40, Y: 50}; //A
PositionsNodeList[1] = {X: 130, Y: 50}; //B
PositionsNodeList[2] = {X: 210, Y: 50}; //C
PositionsNodeList[3] = {X: 290, Y: 50}; //D
PositionsNodeList[4] = {X: 40, Y: 150}; //E
PositionsNodeList[5] = {X: 130, Y: 150}; //F
PositionsNodeList[6] = {X: 210, Y: 150}; //G
PositionsNodeList[7] = {X: 290, Y: 150}; //H
nodeA = new Node("A", PositionsNodeList[0].X, PositionsNodeList[0].Y, 2, false, false, false, false);
nodeB = new Node("B", PositionsNodeList[1].X, PositionsNodeList[1].Y, 2, false, false, false, false);
nodeC = new Node("C", PositionsNodeList[2].X, PositionsNodeList[2].Y, 2, false, false, false, false);
nodeD = new Node("D", PositionsNodeList[3].X, PositionsNodeList[3].Y, 2, false, false, false, false);
nodeE = new Node("E", PositionsNodeList[4].X, PositionsNodeList[4].Y, 2, false, false, false, false);
nodeF = new Node("F", PositionsNodeList[5].X, PositionsNodeList[5].Y, 2, false, false, false, false);
nodeG = new Node("G", PositionsNodeList[6].X, PositionsNodeList[6].Y, 2, false, false, false, false);
nodeH = new Node("H", PositionsNodeList[7].X, PositionsNodeList[7].Y, 2, false, false, false, false);
NodeList = [nodeA, nodeB, nodeC, nodeD, nodeE, nodeF, nodeG, nodeH];
edgeEA = new Edge("EA", "E", "A", false, false);
edgeFB = new Edge("FB", "F", "B", false, false);
edgeGC = new Edge("GC", "G", "C", false, false);
edgeHD = new Edge("HD", "H", "D", false, false);
edgeEF = new Edge("EF", "E", "F", false, false);
edgeFG = new Edge("FG", "F" ,"G", false, false);
edgeGH = new Edge("GH", "G", "H", false, false);
EdgeList = [edgeEA, edgeFB, edgeGC, edgeHD, edgeEF, edgeFG, edgeGH];
}
else if(Graph_Number == 4){
PositionsNodeList[0] = {X: 220, Y: 20}; //A
PositionsNodeList[1] = {X: 40, Y: 80}; //B
PositionsNodeList[2] = {X: 400, Y: 80}; //C
PositionsNodeList[3] = {X: 130, Y: 150}; //D
PositionsNodeList[4] = {X: 310, Y: 150}; //E
PositionsNodeList[5] = {X: 40, Y: 200}; //F
PositionsNodeList[6] = {X: 220, Y: 200}; //G
PositionsNodeList[7] = {X: 400, Y: 200}; //H
nodeA = new Node("A", PositionsNodeList[0].X, PositionsNodeList[0].Y, 2, false, false, false, false);
nodeB = new Node("B", PositionsNodeList[1].X, PositionsNodeList[1].Y, 2, false, false, false, false);
nodeC = new Node("C", PositionsNodeList[2].X, PositionsNodeList[2].Y, 2, false, false, false, false);
nodeD = new Node("D", PositionsNodeList[3].X, PositionsNodeList[3].Y, 2, false, false, false, false);
nodeE = new Node("E", PositionsNodeList[4].X, PositionsNodeList[4].Y, 2, false, false, false, false);
nodeF = new Node("F", PositionsNodeList[5].X, PositionsNodeList[5].Y, 2, false, false, false, false);
nodeG = new Node("G", PositionsNodeList[6].X, PositionsNodeList[6].Y, 2, false, false, false, false);
nodeH = new Node("H", PositionsNodeList[7].X, PositionsNodeList[7].Y, 2, false, false, false, false);
NodeList = [nodeA, nodeB, nodeC, nodeD, nodeE, nodeF, nodeG, nodeH];
edgeAG = new Edge("AG", "A", "G", false, false);
edgeAB = new Edge("AB", "A", "B", false, false);
edgeBD = new Edge("BD", "B", "D", false, false);
edgeDG = new Edge("DG", "D", "G", false, false);
edgeDF = new Edge("DF", "D", "F", false, false);
edgeCE = new Edge("CE", "C" ,"E", false, false);
edgeEH = new Edge("EH", "E", "H", false, false);
edgeEG = new Edge("EG", "E", "G", false, false);
EdgeList = [edgeAG, edgeAB, edgeBD, edgeDG, edgeDF, edgeCE, edgeEH, edgeEG];
}
else if(Graph_Number == 5){
PositionsNodeList[0] = {X: 100, Y: 40}; //A
PositionsNodeList[1] = {X: 260, Y: 40}; //B
PositionsNodeList[2] = {X: 20, Y: 100}; //C
PositionsNodeList[3] = {X: 180, Y: 100}; //D
PositionsNodeList[4] = {X: 340, Y: 100}; //E
PositionsNodeList[5] = {X: 100, Y: 160}; //F
PositionsNodeList[6] = {X: 260, Y: 160}; //G
PositionsNodeList[7] = {X: 420, Y: 160}; //H
nodeA = new Node("A", PositionsNodeList[0].X, PositionsNodeList[0].Y, 2, false, false, false, false);
nodeB = new Node("B", PositionsNodeList[1].X, PositionsNodeList[1].Y, 2, false, false, false, false);
nodeC = new Node("C", PositionsNodeList[2].X, PositionsNodeList[2].Y, 2, false, false, false, false);
nodeD = new Node("D", PositionsNodeList[3].X, PositionsNodeList[3].Y, 2, false, false, false, false);
nodeE = new Node("E", PositionsNodeList[4].X, PositionsNodeList[4].Y, 2, false, false, false, false);
nodeF = new Node("F", PositionsNodeList[5].X, PositionsNodeList[5].Y, 2, false, false, false, false);
nodeG = new Node("G", PositionsNodeList[6].X, PositionsNodeList[6].Y, 2, false, false, false, false);
nodeH = new Node("H", PositionsNodeList[7].X, PositionsNodeList[7].Y, 2, false, false, false, false);
NodeList = [nodeA, nodeB, nodeC, nodeD, nodeE, nodeF, nodeG, nodeH];
edgeAF = new Edge("AF", "A", "F", false, false);
edgeAD = new Edge("AD", "A", "D", false, false);
edgeCF = new Edge("CF", "C", "F", false, false);
edgeDF = new Edge("DF", "D", "F", false, false);
edgeDG = new Edge("DG", "D", "G", false, false);
edgeBD = new Edge("BD", "B" ,"D", false, false);
edgeBG = new Edge("BG", "B", "G", false, false);
edgeEG = new Edge("EG", "E", "G", false, false);
edgeEH = new Edge("EH", "E", "H", false, false);
EdgeList = [edgeAF, edgeAD, edgeCF, edgeDF, edgeDG, edgeBD, edgeBG, edgeEG, edgeEH];
}
init();
}
//Function update listen to the radio button clicked and update
//the node as required.
function update(){
for(var i = 0; i < EdgeList.length; i++){
EdgeList[i].highlighted = false;
}
//Case for node A
var radios = document.getElementById('nodeA_X');
//Node A update
if(document.getElementById('nodeA_X').checked){
nodeA = new Node("A", PositionsNodeList[0].X, PositionsNodeList[0].Y, 2, false, true, false, false);
NodeList[0] = nodeA;
}
else if(document.getElementById('nodeA_Y').checked){
nodeA = new Node("A", PositionsNodeList[0].X, PositionsNodeList[0].Y, 2, false, false, true, false);
NodeList[0] = nodeA;
}
else if(document.getElementById('nodeA_E').checked){
nodeA = new Node("A", PositionsNodeList[0].X, PositionsNodeList[0].Y, 2, true, false, false, false);
NodeList[0] = nodeA;
}
else if(document.getElementById('nodeA_C').checked){
nodeA = new Node("A", PositionsNodeList[0].X, PositionsNodeList[0].Y, 2, false, false, false, false);
NodeList[0] = nodeA;
}
//Node B update
if(document.getElementById('nodeB_X').checked){
nodeB = new Node("B", PositionsNodeList[1].X, PositionsNodeList[1].Y, 2, false, true, false, false);
NodeList[1] = nodeB;
}
else if(document.getElementById('nodeB_Y').checked){
nodeB = new Node("B", PositionsNodeList[1].X, PositionsNodeList[1].Y, 2, false, false, true, false);
NodeList[1] = nodeB;
}
else if(document.getElementById('nodeB_E').checked){
nodeB = new Node("B", PositionsNodeList[1].X, PositionsNodeList[1].Y, 2, true, false, false, false);
NodeList[1] = nodeB;
}
else if(document.getElementById('nodeB_C').checked){
nodeB = new Node("B", PositionsNodeList[1].X, PositionsNodeList[1].Y, 2, false, false, false, false);
NodeList[1] = nodeB;
}
//Node C update
if(document.getElementById('nodeC_X').checked){
nodeC = new Node("C", PositionsNodeList[2].X, PositionsNodeList[2].Y, 2, false, true, false, false);
NodeList[2] = nodeC;
}
else if(document.getElementById('nodeC_Y').checked){
nodeC = new Node("C", PositionsNodeList[2].X, PositionsNodeList[2].Y, 2, false, false, true, false);
NodeList[2] = nodeC;
}
else if(document.getElementById('nodeC_E').checked){
nodeC = new Node("C", PositionsNodeList[2].X, PositionsNodeList[2].Y, 2, true, false, false, false);
NodeList[2] = nodeC;
}
else if(document.getElementById('nodeC_C').checked){
nodeC = new Node("C", PositionsNodeList[2].X, PositionsNodeList[2].Y, 2, false, false, false, false);
NodeList[2] = nodeC;
}
//Node D update
if(document.getElementById('nodeD_X').checked){
nodeD = new Node("D", PositionsNodeList[3].X, PositionsNodeList[3].Y, 2, false, true, false, false);
NodeList[3] = nodeD;
}
else if(document.getElementById('nodeD_Y').checked){
nodeD = new Node("D", PositionsNodeList[3].X, PositionsNodeList[3].Y, 2, false, false, true, false);
NodeList[3] = nodeD;
}
else if(document.getElementById('nodeD_E').checked){
nodeD = new Node("D", PositionsNodeList[3].X, PositionsNodeList[3].Y, 2, true, false, false, false);
NodeList[3] = nodeD;
}
else if(document.getElementById('nodeD_C').checked){
nodeD = new Node("D", PositionsNodeList[3].X, PositionsNodeList[3].Y, 2, false, false, false, false);
NodeList[3] = nodeD;
}
//Node E update
if(document.getElementById('nodeE_X').checked){
nodeE = new Node("E", PositionsNodeList[4].X, PositionsNodeList[4].Y, 2, false, true, false, false);
NodeList[4] = nodeE;
}
else if(document.getElementById('nodeE_Y').checked){
nodeE = new Node("E", PositionsNodeList[4].X, PositionsNodeList[4].Y, 2, false, false, true, false);
NodeList[4] = nodeE;
}
else if(document.getElementById('nodeE_E').checked){
nodeE = new Node("E", PositionsNodeList[4].X, PositionsNodeList[4].Y, 2, true, false, false, false);
NodeList[4] = nodeE;
}
else if(document.getElementById('nodeE_C').checked){
nodeE = new Node("E", PositionsNodeList[4].X, PositionsNodeList[4].Y, 2, false, false, false, false);
NodeList[4] = nodeE;
}
//Node F update
if(document.getElementById('nodeF_X').checked){
nodeF = new Node("F", PositionsNodeList[5].X, PositionsNodeList[5].Y, 2, false, true, false, false);
NodeList[5] = nodeF;
}
else if(document.getElementById('nodeF_Y').checked){
nodeF = new Node("F", PositionsNodeList[5].X, PositionsNodeList[5].Y, 2, false, false, true, false);
NodeList[5] = nodeF;
}
else if(document.getElementById('nodeF_E').checked){
nodeF = new Node("F", PositionsNodeList[5].X, PositionsNodeList[5].Y, 2, true, false, false, false);
NodeList[5] = nodeF;
}
else if(document.getElementById('nodeF_C').checked){
nodeF = new Node("F", PositionsNodeList[5].X, PositionsNodeList[5].Y, 2, false, false, false, false);
NodeList[5] = nodeF;
}
//Node G update
if(document.getElementById('nodeG_X').checked){
nodeG = new Node("G", PositionsNodeList[6].X, PositionsNodeList[6].Y, 2, false, true, false, false);
NodeList[6] = nodeG;
}
else if(document.getElementById('nodeG_Y').checked){
nodeG = new Node("G", PositionsNodeList[6].X, PositionsNodeList[6].Y, 2, false, false, true, false);
NodeList[6] = nodeG;
}
else if(document.getElementById('nodeG_E').checked){
nodeG = new Node("G", PositionsNodeList[6].X, PositionsNodeList[6].Y, 2, true, false, false, false);
NodeList[6] = nodeG;
}
else if(document.getElementById('nodeG_C').checked){
nodeG = new Node("G", PositionsNodeList[6].X, PositionsNodeList[6].Y, 2, false, false, false, false);
NodeList[6] = nodeG;
}
//Node H update
if(document.getElementById('nodeH_X').checked){
nodeH = new Node("H", PositionsNodeList[7].X, PositionsNodeList[7].Y, 2, false, true, false, false);
NodeList[7] = nodeH;
}
else if(document.getElementById('nodeH_Y').checked){
nodeH = new Node("H", PositionsNodeList[7].X, PositionsNodeList[7].Y, 2, false, false, true, false);
NodeList[7] = nodeH;
}
else if(document.getElementById('nodeH_E').checked){
nodeH = new Node("H", PositionsNodeList[7].X, PositionsNodeList[7].Y, 2, true, false, false, false);
NodeList[7] = nodeH;
}
else if(document.getElementById('nodeH_C').checked){
nodeH = new Node("H", PositionsNodeList[7].X, PositionsNodeList[7].Y, 2, false, false, false, false);
NodeList[7] = nodeH;
}
}
function clearNodeVisit(){
for(var i = 0; i < NodeList.length; i++){
NodeList[i].visited = false;
}
}
//Check if all the nodes are visited
function checkIfAllVisited(){
for(var i = 0; i < NodeList.length; i++){
if (NodeList[i].visited == false)
return false;
}
console.log("return true from check all visited");
return true;
}
function checkIsEdgeContainVisitedNode(edge){
for(var i = 0; i < NodeList.length; i++){
if(NodeList[i].visited == true &&
edge.name.indexOf(NodeList[i].name) != -1)
return true;
}
return false;
}
function FindCurrentNodesEdgeTo(current, to){
var done = false;
if(current.visited == true){
console.log("return here");
return;
}
var tmp = [];
var index = 0;
for(var i = 0; i < EdgeList.length; i++){
//Find all edges that contain the name
if(EdgeList[i].name.indexOf(current.name) != -1){
if(checkIsEdgeContainVisitedNode(EdgeList[i])){
}else{
//The edge contain the node we want to find
if(EdgeList[i].name.indexOf(to) != -1){
tmp[index] = EdgeList[i];
done = true;
return tmp;
}else{
tmp[index] = EdgeList[i];
index++;
}
}
}
}
console.log("node that set to visited is : " + current.name);
current.visited = true;
for(var i = 0; i < tmp.length; i++){
console.log(tmp[i].name);
}
var empty = [];
var result = empty.concat(tmp);
console.log("\n\n");
if(checkIfAllVisited() == false){
console.log("enter if node is not all visited");
for(var i = 0; i < tmp.length; i++){
var returnLst = FindCurrentNodesEdgeTo(findNodeByName(tmp[i].name.replace(current.name, "")), to);
if(returnLst == null || returnLst.length == 0){
result = result.concat(returnLst);
}
else {
for(var j = 0; j < returnLst.length; j++){
if(typeof returnLst[j] == "undefined"){
continue;
}
else if(returnLst[j].name.indexOf(to) != -1){
result = result.concat(returnLst);
return result;
}
}
result = result.concat(returnLst);
}
}
}
return result;
}
//Return a list of edge that is adjacent to current node
//Ex: A will return AC & AD
function currentNodesEdge(node){
if(node.visited == true){
console.log("return here");
return;
}
var tmp = [];
var index = 0;
for(var i = 0; i < EdgeList.length; i++){
//Find all edges that contain the name
if(EdgeList[i].name.indexOf(node.name) != -1){
if(checkIsEdgeContainVisitedNode(EdgeList[i])){
}else{
tmp[index] = EdgeList[i];
index++;
}
}
}
console.log("node that set to visited is : " + node.name);
node.visited = true;
for(var i = 0; i < tmp.length; i++){
console.log(tmp[i].name);
}
var empty = [];
var result = empty.concat(tmp);
console.log("\n\n");
if(checkIfAllVisited() == false){
console.log("enter if");
for(var i = 0; i < tmp.length; i++){
result = result.concat(currentNodesEdge(findNodeByName(tmp[i].name.replace(node.name, ""))));
}
}
return result;