c++ - N维网格顶点计算

标签 c++ algorithm math

问题:给定一个单元格索引(红色)计算单元格索引周围的数组索引(黑色)。

bool CalculateCellVerticesFromIndex(size_t index, size_t* vertices)
{
    size_t gridSize[2] = {6, 5};
    return true;  // if the index was valid
    return false; // if the index was invalid
}

计算已知大小 (m X n X ... ) 的 N 维网格中单元格周围的顶点。

示例图:

6 x 5 Grid Typo fixed

int vertices[4] = {0, 0, 0, 0}

在上图中,CalculateCellVerticesFromIndex(12, vertices);应该用 {14, 15, 20, 21} 填充顶点;

最佳答案

Width = 6
Row = Index div (Width - 1)
if Row > 5 - 2 then OutOfGrid
Column = Index mod (Width - 1)
LeftBottom = Row * Width + Column
LeftTop = LeftBottom + Width
RightBottom and RightTop - elaborate

关于c++ - N维网格顶点计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10051426/

相关文章:

c++ - 和有什么区别! ( x < y ) 和 x >= y 在 C++ 中?

java - 如何实现 Luhn 算法?

algorithm - Haskell "as patterns"算法

math - 在 Mathematica 中生成拓扑空间图

c++ - 使用 Libnoise 生成高度图

c++ - 从文件中读取不同数量的变量

algorithm - 大O算法效率对比

algorithm - 邀请人们参加聚会的最佳算法

c++ - 使用模板参数的 typedef 不适用于 g++

javascript - 用元素创建形状的算法