-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath.js
More file actions
167 lines (140 loc) · 5.17 KB
/
Copy pathpath.js
File metadata and controls
167 lines (140 loc) · 5.17 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
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
/*
*
* Path
*
*/
function Path (p5i, migrant, pos, dir, network){
this.migrant = migrant;
this.pos = pos;
this.dir = dir;
this.network = network
var initial = new Step(p5i,null,this.pos,this.dir,9);//10 9
this.steps = [];
this.steps.push(initial);
this.shortestStep = [];
var parent = this;
this.networks = {
"a":{
move:function(){parent.grow()},
stroke:p5i.color(255,100),
},
"b":{
move:function(){parent.grow()},
stroke:p5i.color(0,50),
}
};
this.update = function(){
this.networks[this.network].move();
};
this.show = function(){
p5i.push();
p5i.noFill();
p5i.stroke(this.networks[this.network].stroke);
p5i.strokeWeight(migrant.states[migrant.state].strokeWeight);
migrant.state == "brokered" ? p5i.beginShape(p5i.POINTS) : p5i.beginShape();
for(var i = 3; i < this.steps.length; i++){p5i.vertex(this.steps[i].pos.x,this.steps[i].pos.y);}
p5i.endShape();
p5i.pop();
};
this.grow = function(network,employed){
// look at each employer
for(var i = 0; i < p5i.employers.length; i++){
// if this employer is in the same network as this migrant
if(p5i.employers[i].network == this.network && migrant.employers[i] == false){
var closestStep = null;
var employer = p5i.employers[i];
var record = employer.dMax;
// does the employer interact with any steps in the migrant's path
// if so, only influence the closest step
for(var j = 0; j < this.steps.length;j++){
var step = this.steps[j]
var d = p5.Vector.dist(employer.pos,step.pos);
// if this step is within the employer's employment diameter
// the employer will employ the migrant
if(employer.dMin > d && employer.numOfEmployees < employer.maxNumOfEmployees){
migrant.employer = employer;
migrant.employers[i]=true;
migrant.employer.numOfEmployees++;
migrant.state = "transit";
closestStep = null;
break;
}else if(d < record){
// (A step in a migrant's path can be influenced by many employers each timestep)
// (An employer can only influence 1 step in a migrant's path each timestep)
// An employer influences the closest located step
// that is inside it's influence diameter
// and outside it's employment diameter
// identify this step as potentially influenced for this timestep
closestStep = step;
record = d;
}
}
// if this employer influences a step in the migrant's path
// sum its direction with the step's other influcencers directions
// then we can find the average direction for the path's next step
// when the loop is complete
if(closestStep != null){
closestStep.dir.add(p5.Vector.sub(employer.pos,closestStep.pos).normalize());
closestStep.count++;
}
}
}
// look at each intermediary
// need to refactor
for(var i = 0; i < p5i.intermediaries.length; i++){
if(p5i.intermediaries[i].network == this.network && migrant.intermediaries[i] == false){
var intermediary = p5i.intermediaries[i];
var record = intermediary.dMax;
var closestStep = null;
for(var j = 0; j < this.steps.length;j++){
var step = this.steps[j]
var d = p5.Vector.dist(intermediary.pos,step.pos);
if(intermediary.dMin > d){
migrant.intermediaries[i]=true;
closestStep = null;
}else if(d < record){
record = d;
closestStep = step;
}
}
if(closestStep != null){
closestStep.dir.add(p5.Vector.sub(intermediary.pos,closestStep.pos).normalize());
closestStep.count++;
}
}
}
//---------------------------------------------------------------------------
// from an influenced step in the migrant's path, add a new step
// whose direction is the normalized averaged of
// all of its influencer's direction
for(var i = this.steps.length - 1; i >= 0; i--){
var step = this.steps[i];
if(step.count > 0){
step.dir.div(step.count + 1);
this.steps.push(step.next());
step.reset();
}
}
//---------------------------------------------------------------------------
// if the migrant's path has reached an employer
// remove all erroneous steps
if(migrant.state == "transit"){
var eRecord = 100000;
var employerStep;
var shortestPath = [];
// find step that reached the employer
// >=
for(var i = this.steps.length -1; i > 0; i--){
var ed = p5.Vector.dist(migrant.employer.pos,this.steps[i].pos);
if(ed < eRecord){
eRecord = ed;
employerStep = this.steps[i];
}
};
// remove all steps that do not lead to employer
shortestPath.push(employerStep);
while(shortestPath[shortestPath.length-1].parent != null)shortestPath.push(shortestPath[shortestPath.length-1].parent);
this.steps = shortestPath;
}
};
}