简体   繁体   English

在冯·诺伊曼邻居中找到最小值的位置

[英]Finding the position of the lowest value in a Von Neumann Neighborhood

I have a 2 dimensonal array in Javascript with floats inside. 我在Javascript中有一个2维数组,里面有浮点数。 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" :DI hope you understand my problem, english is not my first language. 我认为我有一个“程序员的障碍”:DI希望您理解我的问题,英语不是我的母语。

Background I tried implementing a hydraulic erosion algorithm to my terrain generator, which I wrote in JavaScript and WebGL. 背景技术我尝试对我的地形生成器实施水力侵蚀算法,该算法是用JavaScript和WebGL编写的。 The algorithm I'm trying to use is the optimized hydraulic erosion algorithm described in " Realtime Procedural Terrain Generation " from Jacob Olsen, 2004 我要使用的算法是Jacob Olsen(2004年)在“ 实时过程地形生成 ”中描述的优化水力侵蚀算法

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM