如何在菱形等距图中从后到前迭代

发布于 2024-12-29 06:27:51 字数 318 浏览 1 评论 0原文

想象一个菱形等轴测图,它基本上是一个具有 (x,y) 坐标和顶部单元格作为原点的 2D 数组,如单元格中标记的:

Diamond

我想按以下顺序从后到前迭代这些单元格:

 iteration订单

以这种方式循环遍历未知同边地图的算法是什么?

预期输出:[0,0]、[0,1]、[1,0]、[0,2]、[1,1]、[2,0]、[0,3]等

Imagine a diamond-shaped isometric map, which is basically a 2D array with (x,y) coordinates and the top cell as the origin, as marked in the cells:

Diamond

I want to iterate through these cells from back to front, in the following order:

 iteration order

What's the algorithm to loop in this way through an unknown same-sided map?

Expected output: [0,0], [0,1], [1,0], [0,2], [1,1], [2,0], [0,3], etc

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晌融 2025-01-05 06:27:51

python 伪代码

def iterate_cells(n):
    for i in range(n):
        for j in range(i+1):
            yield (j, i-j)
    for i in range(1, n+1):
        for j in range(n - i):
            yield(i+j, n-j-1)

输出:

In [119]: list(iterate_cells(5))
Out[119]: 
[(0, 0),
 (0, 1),
 (1, 0),
 (0, 2),
 (1, 1),
 (2, 0),
 (0, 3),
 (1, 2),
 (2, 1),
 (3, 0),
 (0, 4),
 (1, 3),
 (2, 2),
 (3, 1),
 (4, 0),
 (1, 4),
 (2, 3),
 (3, 2),
 (4, 1),
 (2, 4),
 (3, 3),
 (4, 2),
 (3, 4),
 (4, 3),
 (4, 4)]

python pseudocode:

def iterate_cells(n):
    for i in range(n):
        for j in range(i+1):
            yield (j, i-j)
    for i in range(1, n+1):
        for j in range(n - i):
            yield(i+j, n-j-1)

output:

In [119]: list(iterate_cells(5))
Out[119]: 
[(0, 0),
 (0, 1),
 (1, 0),
 (0, 2),
 (1, 1),
 (2, 0),
 (0, 3),
 (1, 2),
 (2, 1),
 (3, 0),
 (0, 4),
 (1, 3),
 (2, 2),
 (3, 1),
 (4, 0),
 (1, 4),
 (2, 3),
 (3, 2),
 (4, 1),
 (2, 4),
 (3, 3),
 (4, 2),
 (3, 4),
 (4, 3),
 (4, 4)]
无敌元气妹 2025-01-05 06:27:51

考虑到 map 包含在矩阵 M(n,n) 中:

// lateral loop above diagonal
for (int i=0; i<n; i++) {
  // diagonal loop
  for (int j=0; j<i; j++) {
    // the coords you are looking for are: row=(i-j), col=(i+j)
    int currentTileValue = M[i-j, i+j];
  }
}
// sub-diagonal lateral loop
for (int j=1; j<n; j++) {
  // diagonal loop
  for (int i=0; i<(n-j); i++) {
    // the coords you are looking for are: row=(j-i), col=(j+i)
    int currentTileValue = M[j-i, j+i];
  }
}

没有详细测试它,但我认为我认为它有效。无论如何,你明白了。

Considering map is contained in a matrix M(n,n):

// lateral loop above diagonal
for (int i=0; i<n; i++) {
  // diagonal loop
  for (int j=0; j<i; j++) {
    // the coords you are looking for are: row=(i-j), col=(i+j)
    int currentTileValue = M[i-j, i+j];
  }
}
// sub-diagonal lateral loop
for (int j=1; j<n; j++) {
  // diagonal loop
  for (int i=0; i<(n-j); i++) {
    // the coords you are looking for are: row=(j-i), col=(j+i)
    int currentTileValue = M[j-i, j+i];
  }
}

didn't test it in detail, but I think i think it works. Anyhow you get the idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文