Ryan
Ryan

Reputation: 6642

N-Dimensional Grid Vertices Calculation

QUESTION: Given a cell index (red) compute the array index (black) that surround the cell index.

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
}

Calculate the vertices that surround a cell in a N-dimensional grid of known size (m X n X ... ).

Example diagram:

6 x 5 Grid Typo fixed

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

In the above diagram, CalculateCellVerticesFromIndex(12, vertices); should fill vertices up with {14, 15, 20, 21};

Upvotes: 0

Views: 570

Answers (1)

MBo
MBo

Reputation: 80187

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

Upvotes: 1

Related Questions