【问题标题】:N-Dimensional Grid Vertices CalculationN维网格顶点计算
【发布时间】:2012-04-07 03:14:37
【问题描述】:

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

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 维网格中围绕单元格的顶点。

示例图:

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

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

【问题讨论】:

  • 看来你需要了解整数除法和取余数(模)的操作
  • 它不是用来做作业的。这是为了我的工作。
  • 嗯,我认为你可能是对的,对不起,我的错。显然我的大脑现在已经糊涂了(或其他什么)。我将删除我之前的 cmets...
  • 我们这个 C oor C++ 方法签名出现 C for C++ 我希望 std::vector 或 std::array
  • 函数签名是C-ish;但我试图不使用 std::vector 来向后兼容必须与之交互的遗留代码(即 C)。如果您有一个适用于 std::vector 或 std::array 的解决方案,我也很乐意看到它。我以后可以自己翻译。

标签: c++ algorithm math


【解决方案1】:
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

【讨论】:

  • 我喜欢这段代码的去向,但是如果这是一个 3D 网格,甚至是 4D 网格会发生什么?
  • 它将利用与整数除法相同的原理。例如,要获得 3d 中的层数,需要将 Index 除以二维的乘积
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-26
  • 2018-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多