-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtour.js
More file actions
73 lines (62 loc) · 1.84 KB
/
tour.js
File metadata and controls
73 lines (62 loc) · 1.84 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
function Tour(graph, time) {
this.graph = graph;
this.visitedNodes = null;
this.currentNode = null;
this.first = null;
this.done = false;
this.time = time || 100;
//Run tour
this.init();
this.graph.draw();
this.graph.drawNode(this.currentNode, "#00FF00", 5);
this.tour();
}
Tour.prototype.init = function() {
this.visitedNodes = [];
this.currentNode = this.graph.getRoot();
this.first = this.currentNode;
this.done = false;
//Populate node's children array to make tour easier
this.graph.populateChildrenFromParents();
//Calculate angle between parent and child
//Used to pick min node
for(var i = 0; i < this.graph.nodes.length; i++) {
var node = this.graph.nodes[i];
if(node.parent)
node.angle = getAngle(node.parent, node);
else
node.angle = 0;
}
}
Tour.prototype.tour = function() {
var that = this;
//Interval used to slow down algorithm to make rendering slower
this.interval = setInterval(function() {
if(!that.done) {
that.tourInside();
} else {
clearInterval(that.interval);
that.interval = null;
}
}, this.time);
}
Tour.prototype.tourInside = function() {
if(this.visitedNodes.indexOf(this.currentNode) < 0)
this.visitedNodes.push(this.currentNode);
if(this.currentNode.children.length > 0) {
var node = getNodeWithSmallestAngle(this.currentNode.children);
removeFromArray(this.currentNode.children, node);
this.currentNode = node;
this.currentNode.tourParent = this.visitedNodes[this.visitedNodes.length-1];
} else {
this.currentNode = this.currentNode.parent;
}
if(this.visitedNodes.length == this.graph.nodes.length || this.currentNode == null) {
this.first.tourParent = this.visitedNodes[this.visitedNodes.length-1];
this.done = true;
this.graph.draw();
return;
}
this.graph.draw();
this.graph.drawNode(this.currentNode, "#00FF00", 5);
}