如何获取特定x,y周围的所有坐标

时间:2018-10-07 20:22:01

标签: javascript

我想知道如何围绕特定的x,y创建坐标数组。 例如:

    [Activity(
    Label = "Template", 
    MainLauncher = true, 
    NoHistory = true,
    Theme = "@style/AppTheme",
    Icon = "@drawable/icon")]
public class SplashScreen : MvxSplashScreenActivity
{
    public SplashScreen()
        :base(Resource.Layout.SplashScreen)
    {
    }
    protected override void RunAppStart(Bundle bundle)
    {
        StartActivity(typeof(Start));
        base.RunAppStart(bundle);
    }
}

在这种情况下,“ o”位于坐标3、2上 现在我要输出:

xxxxx
xxoxx
xxxxx

作为围绕“ o”(位于3、2)的坐标数组

2 个答案:

答案 0 :(得分:1)

我在Vue.js的扫雷实现中完全做到了:

getNeighbours (field) {
  let list = []
  let minX = Math.max(0, field.x - 1)
  let maxX = Math.min(this.getX - 1, field.x + 1)
  let minY = Math.max(0, field.y - 1)
  let maxY = Math.min(this.getY - 1, field.y + 1)
  for (let x = minX; x <= maxX; x++) {
    for (let y = minY; y <= maxY; y++) {
      if (x !== field.x || y !== field.y) {
        list.push(this.map[y][x])
      }
    }
  }
  return list
},

其中this是具有this.X列和this.Y行的运动场。整个运动场都保存在二维数组this.map[y][x]中。

您可以在此处查看:https://github.com/franktopel/vue-defuse

该方法可以在这里找到:https://github.com/franktopel/vue-defuse/blob/master/src/components/VueDefuse.vue#L477

答案 1 :(得分:0)

如果您输入的是边界上的xPosyPos,则下面的代码无法说明任何错误处理。

var fullArray = [['x','x','x','x','x'],['x','x','o','x','x'],['x','x','x','x','x']],
  xPos = 2,
  yPos = 1;
for (var y = yPos-1; y <= yPos + 1; y++) {
  console.log(fullArray[y][xPos-1],fullArray[y][xPos],fullArray[y][xPos+1]);
}