-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
482 lines (394 loc) · 11.9 KB
/
main.ts
File metadata and controls
482 lines (394 loc) · 11.9 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
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
import _ = require("lodash");
import anime = require("animejs");
// The distance want to be away from the wall
const CLOSE_WALL_DISTANCE = 20;
// The distance want to be away from other boids
const CLOSE_BOID_DISTANCE = 80;
const MOVE_FACTOR = 10;
let WALLX = 500;
let WALLY = 280;
type Coordinate = {
x: number;
y: number;
};
type Vector = {
x: number;
y: number;
};
// Boid
class Boid {
public drads: Array<number>;
constructor(
public id: number,
public coordinate: Coordinate,
public rad: number,
public distance?: number // Speed
) {
this.id = id;
this.coordinate = coordinate;
this.rad = rad;
this.distance = distance === undefined ? 1 : distance;
this.drads = []; // Stores the direction of each force to calculate the moving direction
}
public x() {
return this.coordinate.x;
}
public y() {
return this.coordinate.y;
}
public dx() {
return this.distance * Math.cos(this.rad);
}
public dy() {
return this.distance * Math.sin(this.rad);
}
public deg() {
return (this.rad * 180) / Math.PI;
}
public dump() {
console.log("id: " + this.id);
console.log("coordinate: " + this.coordinate);
console.log("rad: " + this.rad);
console.log("distance: " + this.distance);
}
}
class Flock {
constructor(private boids?: Array<Boid>) {
this.boids = boids;
}
public static createBoids(_num?: number): Array<Boid> {
let num = _num === undefined ? 1 : _num;
let boids = [];
for (let i = 0; i < num; i++) {
let x = Math.random() * 500;
let y = Math.random() * -300;
let rad = Math.random() * 2 * Math.PI;
boids.push(new Boid(i, { x: x, y: y }, rad, 1));
}
return boids;
}
public calcDRads(boid: Boid) {
this.radToDrads(boid);
this.turnFlockCenter(boid);
this.matchVelocity(boid);
this.avoidOtherBoids(boid);
this.avoidWall(boid);
}
// Update the Boid coordinates using the accumulated drads
public updateCoordinateAndRad(boid: Boid) {
let aveRad = this.calcAverageRads(boid.drads);
let aveDradsUpteaed = this.calcAverageRad(aveRad, boid.rad);
let updatedCoordinate = this.addCordinateToRad(
boid.coordinate,
aveDradsUpteaed
);
boid.drads = [];
boid.rad = aveDradsUpteaed;
boid.coordinate = updatedCoordinate;
}
private addCordinateToRad(c: Coordinate, rad: number): Coordinate {
return {
x: c.x + Math.cos(rad) * MOVE_FACTOR,
y: c.y + Math.sin(rad) * MOVE_FACTOR,
};
}
private avoidWall(boid: Boid): Boid {
if (
(this.closeRight(boid) && this.faceRight(boid)) ||
(boid.coordinate.x > WALLX && this.faceRight(boid))
) {
this.inversionX(boid);
} else if (
(this.closeLeft(boid) && this.faceLeft(boid)) ||
(boid.coordinate.x < 0 && this.faceLeft(boid))
) {
this.inversionX(boid);
} else if (
(this.closeTop(boid) && this.faceTop(boid)) ||
(boid.coordinate.y > 0 && this.faceTop(boid))
) {
this.inversionY(boid);
} else if (
(this.closeButtom(boid) && this.faceDown(boid)) ||
(boid.y() * -1 > WALLY && this.faceDown(boid))
) {
this.inversionY(boid);
}
return boid;
}
private avoidOtherBoids(boid: Boid) {
let boids = this.extractBoidFromBoids(boid);
let rads: Array<number> = [];
for (let otherBoid of boids) {
let closeDistance: boolean = this.closeDistanceCoordinate(
boid.coordinate,
otherBoid.coordinate,
CLOSE_BOID_DISTANCE
);
if (closeDistance) {
let vector: Vector = this.coordinatesToVector(
boid.coordinate,
otherBoid.coordinate
);
let _vector = this.inversionVector(vector);
rads.push(this.vectorToRadian(_vector));
}
}
let aveRad: number = this.calcAverageRads(rads);
boid.drads.push(aveRad);
}
private calcAverageCoordinate(boids: Array<Boid>): Coordinate {
var sumX = 0;
var sumY = 0;
for (let boid of boids) {
sumX += boid.x();
sumY += boid.y();
}
return { x: sumX / boids.length, y: sumY / boids.length };
}
private calcAverageRad(r1: number, r2: number): number {
let dx1 = Math.cos(r1);
let dx2 = Math.cos(r2);
let dy1 = Math.sin(r1);
let dy2 = Math.sin(r2);
let dx = (dx1 + dx2) / 2;
let dy = (dy1 + dy2) / 2;
return Math.atan2(dy, dx);
}
private calcAverageRadianBoids(boids: Array<Boid>): number {
let sumDx = 0;
let sumDy = 0;
for (let boid of boids) {
let dx = Math.cos(boid.rad);
let dy = Math.sin(boid.rad);
sumDx += dx;
sumDy += dy;
}
let aveDx = sumDx / boids.length;
let aveDy = sumDy / boids.length;
let averad = Math.atan2(aveDy, aveDx);
return averad;
}
private calcAverageRads(rads: Array<number>): number {
let sumX: number = 0;
let sumY: number = 0;
for (let rad of rads) {
sumX += Math.cos(rad);
sumY += Math.sin(rad);
}
return this.vectorToRadian({ x: sumX, y: sumY });
}
// Whether the distance between the coordinates of c1 and c2 is closer than standardDinstance
private closeDistanceCoordinate(
c1: Coordinate,
c2: Coordinate,
standardDistance: number
): boolean {
let distance: number = Math.sqrt(
Math.pow(c1.x - c2.x, 2) + Math.pow(c1.y - c2.y, 2)
);
return distance < standardDistance ? true : false;
}
private closeButtom(boid: Boid): boolean {
let distanceY = Math.abs(WALLY + boid.y());
return distanceY < CLOSE_WALL_DISTANCE ? true : false;
}
private closeLeft(boid: Boid): boolean {
let distanceX = Math.abs(boid.x());
return distanceX < CLOSE_WALL_DISTANCE ? true : false;
}
// Is it close to the top wall?
private closeTop(boid: Boid): boolean {
let distanceY = Math.abs(boid.y() * -1);
return distanceY < CLOSE_WALL_DISTANCE ? true : false;
}
private closeRight(boid: Boid): boolean {
let distanceX = Math.abs(WALLX - boid.x());
return distanceX < CLOSE_WALL_DISTANCE ? true : false;
}
private coordinatesToVector(c1: Coordinate, c2: Coordinate): Vector {
return { x: c2.x - c1.x, y: c2.y - c1.y };
}
// Creates and returns a new Boids that excludes the argument Boid from the instance variable Boids.
// (No change is made to the instance variable Boids)
private extractBoidFromBoids(boid: Boid): Array<Boid> {
let except_boids = this.boids.filter(function (boid_) {
return boid != boid_;
});
return except_boids;
}
private faceDown(boid: Boid): boolean {
let rad = this.calcAverageRads(boid.drads);
if (
(rad < 6.28319 && rad > 3.14159) ||
(rad > -3.14159 && rad < -0.0174533)
) {
return true;
} else {
return false;
}
}
private faceLeft(boid: Boid): boolean {
let rad = this.calcAverageRads(boid.drads);
return (rad < 4.71239 && rad > 1.5708) || (rad < -1.5708 && rad > -4.71239)
? true
: false;
}
private faceRight(boid: Boid): boolean {
let rad = this.calcAverageRads(boid.drads);
return (rad > -1.5708 && rad < 1.5708) || rad > 4.71239 ? true : false;
}
private faceTop(boid: Boid): boolean {
let rad = this.calcAverageRads(boid.drads);
return rad < 3.14159 && rad > 0 ? true : false;
}
private inversionX(boid: Boid): Boid {
let aveRad = this.calcAverageRads(boid.drads);
let dx = Math.cos(aveRad);
let dxNew = dx * -1;
let newRad = this.vectorToRadian({ x: dxNew, y: Math.sin(aveRad) });
boid.drads = [newRad];
return boid;
}
private inversionY(boid: Boid): Boid {
let aveRad = this.calcAverageRads(boid.drads);
let dy = Math.sin(aveRad);
let dyNew = dy * -1;
let newRad = this.vectorToRadian({ x: Math.cos(aveRad), y: dyNew });
boid.drads = [newRad];
return boid;
}
// ex: { 1, 1} -> { -1, -1}
private inversionVector(vector: Vector): Vector {
let resultVector: Vector = { x: vector.x * -1, y: vector.y * -1 };
return resultVector;
}
private matchVelocity(boid: Boid) {
let boids = this.extractBoidFromBoids(boid);
if (boids.length == 0) return;
let aveRad = this.calcAverageRadianBoids(boids);
boid.drads.push(aveRad);
}
private turnFlockCenter(boid: Boid) {
let boids = this.extractBoidFromBoids(boid);
if (boids.length == 0) return;
let averageCoordinate = this.calcAverageCoordinate(boids);
let vector = this.coordinatesToVector(boid.coordinate, averageCoordinate);
let drad = this.vectorToRadian(vector);
boid.drads.push(drad);
}
private radToDrads(boid: Boid): Boid {
boid.drads.push(boid.rad);
return boid;
}
private vectorToRadian(vector: Vector): number {
return Math.atan2(vector.y, vector.x);
}
}
// for debug method
function dumpBoid(boid: Boid) {
console.log(boid.id);
console.log("x: " + boid.coordinate.x);
console.log("y: " + boid.coordinate.y);
console.log("rad: " + boid.rad);
console.log("drads: " + boid.drads);
console.log("degree: " + boid.rad * (180 / Math.PI));
}
// for debug method
function dumpBoids(boids: Array<Boid>) {
for (let boid of boids) {
boid.dump();
}
}
function viewBoids(boids: Array<Boid>) {
let border = document.querySelector("#border");
boids.forEach(function (boid, i) {
var div = document.createElement("div");
div.className = "square";
div.id = "boid" + boid.id;
div.style.top = -1 * boid.coordinate.y + "px";
div.style.left = boid.coordinate.x + "px";
div.style.position = "absolute";
div.style.transform = "rotate(" + (90 - boid.deg()) + "deg)";
border.appendChild(div);
});
}
function degreeToRad(degree: number){
return degree * ( Math.PI / 180 );
}
//let boids: Array<Boid> = Flock.createBoids(30);
let boid1 = new Boid(0, {x: 10, y: -50 }, degreeToRad(0), 1);
let boid2 = new Boid(1, {x: 300, y: -200 }, degreeToRad(45), 1);
let boid3 = new Boid(2, {x: 10, y: -100 }, degreeToRad(0), 1);
let boid4 = new Boid(3, {x: 200, y: -200 }, degreeToRad(45), 1);
let boids: Array<Boid> = [boid1, boid2, boid3, boid4];
let flock = new Flock(boids);
viewBoids(boids);
// Move Boid by clicking
let btn = document.getElementById("move");
if (btn !== null){
btn.addEventListener("click", function () {
for (let boid of boids) {
flock.calcDRads(boid);
flock.updateCoordinateAndRad(boid);
let div = document.getElementById("boid" + boid.id);
if (div !== null) {
div.style.transform = "rotate(" + (90 - boid.deg()) + "deg)";
anime({
targets: "#boid" + boid.id,
left: boid.coordinate.x + "px",
top: -1 * boid.coordinate.y + "px",
easing: "linear",
});
}
}
});
}
// Boid moves automatically
let auto_btn = document.getElementById("auto_move");
function move_loop() {
for (let boid of boids) {
flock.calcDRads(boid);
flock.updateCoordinateAndRad(boid);
let div = document.getElementById("boid" + boid.id);
if (div !== null) {
div.style.transform = "rotate(" + (90 - boid.deg()) + "deg)";
anime({
targets: "#boid" + boid.id,
left: boid.coordinate.x + "px",
top: -1 * boid.coordinate.y + "px",
easing: "linear",
});
}
}
}
if (auto_btn !== null){
auto_btn.addEventListener("click", function () {
setInterval(move_loop, 50);
});
}
// Boid moves automatically Async
let auto_btn_async = document.getElementById("auto_move_async");
function move_loop_async() {
for (let boid of boids) {
flock.calcDRads(boid);
}
for (let boid of boids) {
flock.updateCoordinateAndRad(boid);
let div = document.getElementById("boid" + boid.id);
if (div !== null) {
div.style.transform = "rotate(" + (90 - boid.deg()) + "deg)";
anime({
targets: "#boid" + boid.id,
left: boid.coordinate.x + "px",
top: -1 * boid.coordinate.y + "px",
easing: "linear",
});
}
}
}
if (auto_btn_async !== null){
auto_btn_async.addEventListener("click", function () {
setInterval(move_loop_async, 50);
});
}