【问题标题】:find closest two points of multiple array in javascript在javascript中找到多个数组的最近两个点
【发布时间】:2020-10-27 23:44:11
【问题描述】:

我有:

pathx=[15,12,25,39,7]
path1=[17,32,71,91]
path2=[52,63,9,29,49,37]
path3=[43,40,5]

实际上路径元素更多,路径数量也更多!

需要从 pathx 和 path1、path2、path3 之一中找到最接近的数字。
在此示例中,最接近的是来自 pathx 的 39 和来自 path3 的 40。 结果需要是路径号和元素位置(pathx position4和path3 position2)。

需要将 pathx 与 path3 连接后,结果将是 pathx=[15,12,25,39,40,5,43,7]。

并在相同模式下将新路径 x 与保持路径 1、路径 2 进行比较...直到完成 所有路径。不过这可以尽量让自己……没问题! 并且路径的值实际上是 [xx+yy] ...所以毕达哥拉距离是 易于计算...最后 pathx 将包含坐标数组 [x,y] 但是第一次会怎么做呢

提前致谢!

感谢您的第一反应是所有路径都有所有路径,我在第一次询问时出错了!

【问题讨论】:

    标签: javascript arrays find compare


    【解决方案1】:

    这样就可以了.. 有一些方法可以更有效地做到这一点,但如果这些是我们正在谈论的数字数量,它应该足够好..

      let path1=[17,32,71,91];
      let path2=[15,12,25,39,7];
      let path3=[52,63,9,29,49,37];
      let path4=[43,40,5];
      let paths=[path1,path2,path3,path4];
    
      let num1 = 0;
      let num2 = 0;
      let minDiff = Number.MAX_VALUE;
      for(let i = 0; i < paths.length - 1; i++){
        let arr=[];
        for(let j = i+1; j < paths.length; j++){
          arr=[...arr, ...paths[j]];
        }
       
        for(let m =0; m< paths[i].length; m++){
          for(let n = 0; n < arr.length; n++){
            let currDiff = Math.abs(paths[i][m] - arr[n]);
            if(currDiff < minDiff){
              minDiff = currDiff;
              num1 = paths[i][m];
              num2 = arr[n];
            }
          }
        }
      }
      
    

    在此之后minDiff 将是 1,num1 将是 39,num2 将是 40 :)

    【讨论】:

      【解决方案2】:

      解决这个问题的一种方法是创建一个包含所有点的数组,其中的数据指示每个点的路径索引、位置索引和位置。

      接下来,按位置对数组进行排序,并丢弃被同一路径上的点包围的项目,因为这些项目肯定不会构成最佳对。

      然后对于排序数组中的每个点,计算到具有较高位置的不同路径上最近点的距离(无需考虑较低的位置,因为相应的配对已经在外循环的较早时考虑过) .由于排序后的过滤,每个项目最多会与其他两个项目进行比较(可能一个来自同一路径,一个来自不同路径)。

      解决方案将O(n log n) 与点数(来自排序,然后是O(n) 过程)缩放,如果需要考虑很多点,这可能很有用。

      这是一个用 Javascript 实现的示例。

      const paths = [
          [17, 32, 71, 91],
          [15, 12, 25, 39, 7],
          [52, 63, 9, 29, 49, 37],
          [43, 40, 5]
      ]
      
      // Add [path_idx, position_idx, position] triples to an array.
      let array = [];
      for (let i = 0; i < paths.length; ++i) {
          const path = paths[i];
          for (let j = 0; j < path.length; ++j) {
              array.push({path_idx: i, position_idx: j, position: path[j]});
          }
      }
      
      // Sort the array by position values.
      array.sort(function(a, b) {
          if (a.position < b.position)
              return -1;
          else if (a.position > b.position)
              return 1;
          else
              return 0;
      });
      
      // Drop items that are surrounded by points on the same path, as these
      // items will definitely not comprise the optimal pair.
      array = array.filter(function(item, idx) {
          if (idx === 0 || idx === array.length - 1)
              return true;
          const left = array[idx - 1];
          const right = array[idx + 1];
          return item.path_idx !== left.path_idx || item.path_idx !== right.path_idx;
      
      });
      
      // Find the closest items from different paths.
      let min_distance = null;
      let closest = null;
      for (let i = 0; i < array.length; ++i) {
          let item_i = array[i];
          for (let j = i + 1; j < array.length; ++j) {
              let item_j = array[j];
              if (item_i.path_idx === item_j.path_idx)
                  continue;
              const distance = item_j.position - item_i.position;
              if (min_distance === null || distance < min_distance) {
                  min_distance = distance;
                  closest = [item_i, item_j];
              }
              break;
          }
      }
      
      // Output (zero-indexed):
      //   [
      //     { path_idx: 1, position_idx: 3, position: 39 },
      //     { path_idx: 3, position_idx: 1, position: 40 }
      //   ]
      console.log(closest);
      
      

      【讨论】:

        【解决方案3】:

        我修改了代码……结果就是这样:

              <script>
        
        
          let pathx=[15,12,25,39,7];
          let path0=[17,32,71,91];
          let path1=[52,63,9,29,49,37];
          let path2=[43,40,5];
          let paths=[path0,path1,path2];
        
          let num1 = 0;
          let num2 = 0;
          let num3 = 0;
          let num4 = 0;
          let num5 = 0;
          let minDiff = Number.MAX_VALUE;
          for(let i = 0; i < pathx.length; i++){
            for(let m =0; m< paths.length; m++){
              for(let n = 0; n < paths[m].length; n++){
                let currDiff = Math.abs(paths[m][n] - pathx[i]);
                if(currDiff < minDiff){
                  minDiff = currDiff;
                  num1 = paths[m][n];
                  num2 = pathx[i];
                  num3 = i;
                  num4 = m;
                  num5 = n;
                }
              }
            }
          }
          
                  console.log("minDiff="+minDiff+" num1="+num1+" num2="+num2);
                  console.log("pathx["+num3+"]="+num2+" paths["+num4+"]["+num5+"]="+num1)
        </script>
        

        结果:

        minDiff=1 num1=40 num2=39
        pathx[3]=39 paths[2][1]=40
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-01-16
          • 2016-12-28
          • 2020-05-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多