javascriptarraysalgorithmvon-neumann

Finding the position of the lowest value in a Von Neumann Neighborhood


I have a 2 dimensonal array in Javascript with floats inside. I have to iterate over every item in the array and find the position of the lowest float in a Von Neumann Neighborhood. The problem is I know how I find the lowest value, but not how to find the position of this value in the array.

I think there has to be a simple solution for that, but I just can't get my head around it. I think I have a "programmer's block" :D I hope you understand my problem, english is not my first language.

Background I tried implementing a hydraulic erosion algorithm to my terrain generator, which I wrote in JavaScript and WebGL. The algorithm I'm trying to use is the optimized hydraulic erosion algorithm described in "Realtime Procedural Terrain Generation" from Jacob Olsen, 2004


Solution

  • If you know how to know the right, you just to do a double loop

    function findStuff(array2D){
      for(var x = 0, max = array2D.length; x < max; x++){// parse each row
        var row = array2D[x];
        for(var y = 0, maxY = row.length; y < maxY; y++){// parse each col
           // do stuff here
           if(anyConditionFilled){
              return [x,y];// will return an array with x at index 0, and y at index 1
           }
        }
      }
    }
    var res = findStuff(myArray2D);
    console.log('res : ', res);