麻烦将 2dlogic 转换为 1d 逻辑......矩阵作为访问邻居元素的数组

问题描述

我在使用此功能时遇到问题

我有一个元类,它作为 x 和 y 属性。 我将单元格网格存储在单个数组中。

我的策略是获取数组中的位置,并用 % arr.lenght-1 包裹元素...但是我得到的结果毫无意义;因此我认为我错过了一个关键步骤。

span = 1920;

     populateN(arr,span){  
          
          const arrPos = this.y*span+this.x; //position in arr
          this.neighbors.push(arr[arrPos+1]);//targeting [x-1][y]
          this.neighbors.push(arr[arrPos-1]);//targeting [x+1][y]


          this.neighbors.push(arr[(arrPos+1-span)%(arr.length-1)]);//targeting [x+1][y-1]
          this.neighbors.push(arr[(arrPos-1-span)%(arr.length-1)]);//targeting [x-1][y-1]
          this.neighbors.push(arr[(arrPos+0-span)%(arr.length-1)]);//targeting [x][y-1]

          
          this.neighbors.push(arr[(arrPos+1+span)%(arr.length-1)]);//targeting [x+1][y+1]
          this.neighbors.push(arr[(arrPos-1+span)%(arr.length-1)]);//targeting [x-1][y+1]
          this.neighbors.push(arr[(arrPos+0+span)%(arr.length-1)]);//targeting [x][y+1]


          

     }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)