N维网格顶点计算

时间:2012-04-07 03:14:38

标签: 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
}

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

示例图:

6 x 5 Grid Typo fixed

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

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

1 个答案:

答案 0 :(得分: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