-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.js
138 lines (119 loc) · 2.87 KB
/
graph.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
function Graph(ctx, num) {
this.ctx = ctx;
this.nodes = [];
this.edges = [];
this.createNodes(num);
}
Graph.prototype.createNodes = function(amount) {
var ctx = this.ctx;
for(var i = 0; i < amount; ++i) {
this.nodes.push(new Node( Math.random() * ctx.canvas.width,
Math.random() * ctx.canvas.height));
}
}
Graph.prototype.clear = function() {
var ctx = this.ctx;
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
}
Graph.prototype.draw = function() {
this.clear();
this.drawEdges();
this.drawTourEdges("#FF0000", 2);
this.drawNodes();
}
Graph.prototype.drawFullGraph = function() {
this.clear();
for(var i = 0; i < this.nodes.length; i++) {
for(var j = i + 1; j < this.nodes.length; j++) {
this.drawEdge(this.nodes[i], this.nodes[j]);
}
}
this.drawNodes();
}
Graph.prototype.drawNodes = function(color) {
for(var i = 0; i < this.nodes.length; ++i) {
var node = this.nodes[i];
this.drawNode(node, color);
}
}
Graph.prototype.drawNode = function(node, color, size) {
if(!node)
return;
color = color || "#000";
size = size || 2
var ctx = this.ctx;
ctx.fillStyle = color;
ctx.beginPath();
ctx.arc(node.x, node.y, size, 0, size * Math.PI, false);
ctx.fill();
}
Graph.prototype.drawEdge = function(node1, node2, color, thickness) {
color = color || "#000";
thickness = thickness || 1;
var ctx = this. ctx;
if(!node1 || !node2)
return;
ctx.strokeStyle = color;
ctx.lineWidth = thickness;
ctx.beginPath();
ctx.moveTo(node1.x, node1.y);
ctx.lineTo(node2.x, node2.y);
ctx.stroke();
}
Graph.prototype.drawEdges = function(color, thickness) {
for(var i = 0; i < this.nodes.length; ++i) {
var node = this.nodes[i];
var parent = node.parent;
if(parent) {
this.drawEdge(node, parent, color, thickness);
}
}
}
Graph.prototype.drawTourEdges = function(color, thickness) {
for(var i = 0; i < this.nodes.length; ++i) {
var node = this.nodes[i];
var parent = node.tourParent;
if(parent) {
this.drawEdge(node, parent, color, thickness);
}
}
}
Graph.prototype.getRoot = function() {
for(var i = 0; i < this.nodes.length; i++) {
var node = this.nodes[i];
if(!node.parent)
return node;
}
return null;
}
//Populate nodes children list for future tour
Graph.prototype.populateChildrenFromParents = function() {
for(var i = 0; i < this.nodes.length; i++)
this.nodes[i].children = [];
for(var i = 0; i < this.nodes.length; i++) {
var node = this.nodes[i];
if(node.parent) {
node.parent.children.push(node);
}
}
}
//Reset graph so it can be re-used
Graph.prototype.again = function() {
for(var i = 0; i < this.nodes.length; i++) {
var node = this.nodes[i];
node.key = null;
node.parent = null;
node.children = [];
node.angle = 0;
node.tourParent = null;
}
}
function Node(x, y) {
this.x = x;
this.y = y;
this.key = null;
this.parent = null;
this.children = [];
this.angle = 0;
this.tourParent = null;
}