-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.js
76 lines (63 loc) · 2.36 KB
/
worker.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
// x_dots, y_dots, standardDeviation, divStack, obstacles
onmessage = (e) => {
const x_dots = e.data[0];
const y_dots = e.data[1];
const standardDeviation = e.data[2];
const divStack = e.data[3];
const obstacles = e.data[4];
const gaussianRandom = (mean=0, stdev=1) => {
let u = 1 - Math.random();
let v = Math.random();
let z = Math.sqrt( -2.0 * Math.log( u ) ) * Math.cos( 2.0 * Math.PI * v );
return z * stdev + mean;
}
const probabilitiesArray = [];
for (let i = 0; i < x_dots.length; i++) {
const probabilitiesLocalArray = [];
let hit = 0;
let j = -1;
while (true) {
j += 1;
let miss = false;
const x_dot = gaussianRandom(x_dots[i], standardDeviation);
const y_dot = gaussianRandom(y_dots[i], standardDeviation);
if (obstacles !== [] || obstacles !== undefined) {
for (let obstacle of obstacles) {
if (x_dot > obstacle.left
&& x_dot < (obstacle.left + obstacle.width)
&& y_dot > obstacle.top
&& y_dot < (obstacle.top + obstacle.height)
) {
miss = true;
break
}
}
}
if (miss !== true) {
for (let armor of divStack) {
if (x_dot > armor.left
&& x_dot < (armor.left + armor.width)
&& y_dot > armor.top
&& y_dot < (armor.top + armor.height)
) {
if (armor.hit === 1) {
hit += 1;
break
}
}
}
}
probabilitiesLocalArray.push(hit / (j + 1));
if (probabilitiesLocalArray.length > 500) {
if (Math.abs(probabilitiesLocalArray[probabilitiesLocalArray.length - 1] - probabilitiesLocalArray[probabilitiesLocalArray.length - 2]) < 0.0001) {
break
}
}
// if (j >= 500) {
// break
// }
}
probabilitiesArray.push(hit / probabilitiesLocalArray.length);
}
postMessage(probabilitiesArray)
}