-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgridtools.js
144 lines (78 loc) · 4.36 KB
/
gridtools.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
function surroundingSample(gridObj, x, y, i) {
if (x != 0 && y != 0 && x != gridObj.length - 1 && y != gridObj[x].length - 1) {
var surroundPattern = new Array(gridObj[x - 1][y][i], gridObj[x][y - 1][i], gridObj[x - 1][y - 1][i], gridObj[x - 1][y + 1][i], gridObj[x + 1][y - 1][i], gridObj[x + 1][y][i], gridObj[x][y + 1][i], gridObj[x + 1][y + 1][i]);
}
if (x == 0 && y == 0) {
var surroundPattern = new Array(gridObj[gridObj.length - 1][y][i], gridObj[x][gridObj[x].length - 1][i], gridObj[gridObj.length - 1][gridObj[x].length - 1][i], gridObj[gridObj.length - 1][y + 1][i], gridObj[x + 1][gridObj[x].length - 1][i], gridObj[x + 1][y][i], gridObj[x][y + 1][i], gridObj[x + 1][y + 1][i]);
}
if (x == gridObj.length - 1 && y == gridObj[x].length - 1) {
var surroundPattern = new Array(gridObj[x - 1][y][i], gridObj[x][y - 1][i], gridObj[x - 1][y - 1][i], gridObj[x - 1][0][i], gridObj[0][y - 1][i], gridObj[0][y][i], gridObj[x][0][i], gridObj[0][0][i]);
}
if (x == 0 && y == gridObj[x].length - 1) {
var surroundPattern = new Array(0, 0, 0, 0, 0, 0, 0, 0); //(gridObj[gridObj.length - 1][y][i], gridObj[x][y - 1][i], gridObj[gridObj.length - 1][y - 1][i], gridObj[gridObj.legnth - 1][0][i], gridObj[x + 1][y - 1][i], gridObj[x + 1][y][i], gridObj[x][0][i], gridObj[x + 1][0][i]);
}
if (x == gridObj.length - 1 && y == 0) {
var surroundPattern = new Array(gridObj[0][y][i], gridObj[x][gridObj[x].length - 1][i], gridObj[x - 1][gridObj[x].length - 1][i], gridObj[x - 1][y + 1][i], gridObj[0][gridObj[x].length - 1][i], gridObj[0][y][i], gridObj[x][y + 1][i], gridObj[gridObj[x].length - 1][y + 1][i]);
}
if (y == 0 && x != 0 && x != gridObj.length - 1) {
var surroundPattern = new Array(gridObj[x - 1][y][i], gridObj[x][gridObj[x].length - 1][i], gridObj[x - 1][gridObj[x].length - 1][i], gridObj[x - 1][y + 1][i], gridObj[x + 1][gridObj[x].length - 1][i], gridObj[x + 1][y][i], gridObj[x][y + 1][i], gridObj[x + 1][y + 1][i]);
}
if (y == gridObj[x].length - 1 && x != 0 && x != gridObj.length - 1) {
var surroundPattern = new Array(gridObj[x - 1][y][i], gridObj[x][y - 1][i], gridObj[x - 1][y - 1][i], gridObj[x - 1][0][i], gridObj[x + 1][y - 1][i], gridObj[x + 1][y][i], gridObj[x][0][i], gridObj[x + 1][0][i]);
}
if (x == gridObj.length - 1 && y != 0 && y != gridObj.length - 1) {
var surroundPattern = new Array(gridObj[x - 1][y][i], gridObj[x][y - 1][i], gridObj[x - 1][y - 1][i], gridObj[x - 1][y + 1][i], gridObj[0][y - 1][i], gridObj[0][y][i], gridObj[x][y + 1][i], gridObj[0][y + 1][i]);
}
if (x == 0 && y != 0 && y != gridObj.length - 1) {
var surroundPattern = new Array(gridObj[gridObj.length - 1][y][i], gridObj[x][y - 1][i], gridObj[gridObj.length - 1][y - 1][i], gridObj[gridObj.length - 1][y + 1][i], gridObj[x + 1][y - 1][i], gridObj[x + 1][y][i], gridObj[x][y + 1][i], gridObj[x + 1][y + 1][i]);
}
return surroundPattern;
}
function gridDump(gridObj) {
var gridData = '';
for (x=0; x<gridObj.length; x++) {
if (x > 0) {
gridData = gridData + '-';
}
for (y=0; y<gridObj[x].length; y++) {
gridData = gridData + gridObj[x][y][2];
if (y < gridObj[x].length - 1) {
gridData = gridData + ',';
}
}
}
return gridData;
}
function difussion(gridObj, newGridObj, srcObj) {
var ii = 0;
for (x=0; x<gridObj.length; x++) {
for (y=0; y<gridObj[x].length; y++) {
for (i=1; i<srcObj.length; i++) {
ii = i;
if (dump) {
alert(gridObj[x][y][i]);
dump = 0;
}
currentDecay = srcObj[i].decay;
newGridObj[x][y][i] = sumArray(surroundingSample(gridObj, x, y, i)) / (8 + currentDecay);
if (currentSource > 0) {
document.getElementById('location' + x + '-' + y).style.opacity = newGridObj[x][y][currentSource] / 100;
document.getElementById('location' + x + '-' + y).style.filter = 'alpha(opacity=' + newGridObj[x][y][currentSource] + ')';
} else {
document.getElementById('location' + x + '-' + y).style.opacity = 0;
document.getElementById('location' + x + '-' + y).style.filter = 'alpha(opacity=0)';
}
i = ii;
}
}
}
}
function source(type, intensity, x, y, decay, angle, distance) {
this.decay = decay / 100;
this.type = type;
this.intensity = intensity;
this.x = x;
this.y = y;
this.angle = angle;
this.distance = distance;
}