Hi All,
Firstly apologies for the title, I don't know if it describes what I am trying to achieve but its the best I've got.
Basically I have an array describing the intensity over a 2D space. I want to then distribute this intensity to neighbors over a given set of iterations, i.e. Lets say I have the following array:
intensity = [ 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 100, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0 ]
I then do one pass over my distributeIntensity algorithm (distributing 50% of intensity to neighbours). I then would have:
[ 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 50, 50, 50, 0,
0, 50, 100, 50, 0,
0, 50, 50, 50, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0 ]
If I do 2 passes over the original array my resulting array would be:
[ 0, 0, 0, 0, 0,
25, 50, 75, 50, 25,
50, 150, 200, 150, 50,
75, 200, 300, 200, 75,
50, 150, 200, 150, 50,
25, 50, 75, 50, 25,
0, 0, 0, 0, 0 ]
My current code is:
this.distributeIntensities = function(passes, shareRatio) {
for (var i = 0; i < passes; i++) { this.distributeIntensity(shareRatio); }
}
this.distributeIntensity = function(shareRatio) {
var tmp = hm.intensity.slice(0); // copy array
for (var i = 0; i < tmp.length; i++) {
if (hm.intensity[i] <= 0) { continue; }
var current = hm.intensity[i];
var shareAmount = current * shareRatio;
this.shareIntensityWithNeighbours(tmp, shareAmount, i);
}
hm.intensity = tmp;
}
this.shareIntensityWithNeighbours = function(arr, heat, i) {
// This should be var x = Math.floor(...) however
// this is slower and without gives satisfactory results
var x = i % hm.columnCount;
var y = i / hm.columnCount;
if (x > 0) {
if (y > 0) arr[i - hm.columnCount - 1] += heat;
arr[i - 1] += heat;
if (y < (hm.rowCount - 1)) arr[i + hm.columnCount - 1] += heat;
}
if (y > 0) arr[i - hm.columnCount] += heat;
if (y < (hm.rowCount - 1)) arr[i + hm.columnCount] += heat;
if (x < (hm.columnCount - 1)) {
if (y > 0) arr[i - hm.columnCount + 1] += heat;
arr[i + 1] += heat;
if (y < (hm.rowCount - 1)) arr[i + hm.columnCount + 1] += heat;
}
}
Now, this works however it is very slow (I am working with a huge array and 8 passes). I know there is a faster/better/cleaner way of doing this but it is beyond my abilities so I put it out there in the hope that someone can point me in the right direction (Note: I do not speak fluent mathematics, in fact I'm pretty mathematically illiterate).
Thanks in advance
Guido