javascript - 如何在 Javascript 中获取特定坐标 (x, y) 周围的坐标

标签 javascript

如标题所示,我正在努力获取特定坐标周围的坐标。在这个例子中我会说 24, 7。

enter image description here

最佳答案

在这种情况下我通常使用两个循环:

 const results = [];

  for(const dx of [-1, 0, 1]) {
   for(const dy of [-1, 0, 1]) {
     if(dx === 0 && dy === 0) continue;
     results.push({ x: x + dx, y: y + dy });
  }
}

或者只是:

 const neighbours = [
    [-1, -1], [-1,  0], [-1,  1],
    [ 0, -1], /*0, 0*/  [ 0,  1], 
    [ 1, -1], [ 1,  0], [ 1,  1]
 ];

 const results = neighbours.map(([dx, dy]) => ({ x: x + dx, y: y + dy }));

关于javascript - 如何在 Javascript 中获取特定坐标 (x, y) 周围的坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53507608/

相关文章:

javascript - 背景图像没有显示在 JSX 中?

javascript - 使用 Jest 测试 NUXT.js 和 Vue.js 应用程序。获取 '[vuex] module namespace not found in mapState()' 和 '[vuex] unknown action type'

javascript - 如何在 Ember JS 中将逗号分隔的列表显示到表格中?

javascript - 类型错误 : invalid 'in' operand a error when calling jQuery each inside RequireJS module

javascript - 有没有办法在没有 javascript 的情况下在移动应用程序上有一个完整的站点按钮?

javascript - facebook JavaScript SDK - 当您只想将 SDK 用于 facebook 'your app id' 按钮时, 'Like' 是什么?

javascript - 铯地球聚类

Javascript减少速记不返回对象

javascript - jquery addClass 添加子类不起作用

javascript - 将图像拖放到购物篮中