-
Notifications
You must be signed in to change notification settings - Fork 0
/
paths.js
146 lines (88 loc) · 2.58 KB
/
paths.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
var PathSet = function(options) {
var defaults = {
nodes: [],
edges: Object.create(null), // indexed by "from" node
nodeTypes: Object.create(null),
nodeRefs: Object.create(null),
nodeMatchEpsilon: .5,
};
var e = $.extend({}, defaults, options);
for(x in e) this[x] = e[x];
// shorthand
this.c = this.components;
this.init();
};
PathSet.prototype.init = function() {
};
// returns the new node id
PathSet.prototype.addNode = function(pos, type, ref) {
var e = this.nodeMatchEpsilon;
type = type || null;
ref = ref || null;
// look for an existing node that's close enough
for(var i = 0; i < this.nodes.length; i++) {
var n = this.nodes[i];
if(pos.x + e >= n.x
&& pos.x - e < n.x
&& pos.y + e >= n.y
&& pos.y - e < n.y) {
// update the metadata
if(null !== type) this.nodeTypes[i] = type;
if(null !== ref) this.nodeRefs[i] = ref;
return i;
}
}
// no match has been found, insert a new one
var ind = this.nodes.length;
this.nodes.push(ptc(pos));
this.edges[ind] = [];
this.nodeTypes[ind] = type;
this.nodeRefs[ind] = ref;
return ind;
};
PathSet.prototype.addNodeIntoEdge = function(pos, type, ref) {
var n = this.addNode(pos, type, ref);
// check all paths
}
// adds an edge between two nodes
PathSet.prototype.addEdge = function(from, to, bidirectional) {
if(-1 === this.edges[from].indexOf(to))
this.edges[from].push(to);
if(bidirectional && -1 === this.edges[to].indexOf(from))
this.edges[to].push(from);
};
// adds a path between two points
PathSet.prototype.addPath = function(from, to, bidirectional) {
var f = this.addNode(from);
var t = this.addNode(to);
return this.addEdge(f, t, bidirectional);
};
PathSet.prototype.directConnected = function(from, to) {
return this.edges[from].indexOf(to) !== -1;
}
PathSet.prototype.getNodeNear = function(pos, e) {
e = e || this.nodeMatchEpsilon;
// look for an existing node that's close enough
for(var i = 0; i < this.nodes.length; i++) {
var n = this.nodes[i];
if(pos.x + e >= n.x
&& pos.x - e < n.x
&& pos.y + e >= n.y
&& pos.y - e < n.y) {
return i;
}
}
return null;
};
PathSet.prototype.followRandomEdge = function(node) {
var edges = this.edges[node];
if(!edges) return null; // bad node or no exits
var i = rand(0, edges.length - 1);
return ptc(this.nodes[edges[i]]);
};
PathSet.prototype.followTowardsDrink = function(node) {
var edges = this.edges[node];
if(!edges) return null; // bad node or no exits
var i = rand(0, edges.length - 1);
return ptc(this.nodes[edges[i]]);
};