2012-04-07 41 views
0

问题:给定一个单元索引(红色)计算围绕单元索引的数组索引(黑色)。N维网格顶点计算

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×n个X ...)的一个N维网格中的细胞的顶点。

实施例图:

6 x 5 Grid Typo fixed

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

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

+0

有什么问题吗? – twain249 2012-04-07 03:16:33

+1

看来你需要阅读有关整数除法的操作并取其余(模) – MBo 2012-04-07 04:27:16

+0

这不是作业。这是为了我的工作。 – Ryan 2012-04-07 15:33:55

回答

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 
+0

我喜欢这段代码去哪里,但如果这是一个3D网格,甚至是一个4D网格会发生什么? – Ryan 2012-04-07 17:21:24

+0

它将利用与整数除法相同的原则。例如,要以3d为单位获取图层编号,需要将索引除以两维的乘积 – MBo 2012-04-08 10:10:19