代码之家  ›  专栏  ›  技术社区  ›  user3178756

从展平的三维阵列计算三维坐标

  •  1
  • user3178756  · 技术社区  · 7 年前

    我有一个线性索引,按列的主要顺序展开,我喜欢得到三维坐标[x,y,z]。我为Row少校找到这个 https://math.stackexchange.com/questions/19765/calculating-coordinates-from-a-flattened-3d-array-when-you-know-the-size-index ,但无法计算出列的主调?

    2 回复  |  直到 7 年前
        1
  •  0
  •   SJHowe    7 年前

    鉴于

    sometype array[XSIZE][YSIZE][ZSIZE];
    

    然后作为一维数组,如果 x>=0和x<xsize、y>=0和y<ysize和z>=0和z<zsize,然后

    Index = ((x * YSIZE + y) * ZSIZE) + z;      // Row major order, C/C++
    Index = ((z * YSIZE + y) * XSIZE) + x;      // Col major order
    

    对于计算指数,给定x,y,z:

    // For Row major order
    z = Index % ZSIZE;
    y = (Index / ZSIZE) % YSIZE;
    x = Index / (ZSIZE * YSIZE);
    
    // For Col major order
    x = Index % XSIZE;
    y = (Index / XSIZE) % YSIZE;
    z = Index / (XSIZE * YSIZE);
    
        2
  •  0
  •   stark    7 年前

    如果

    index = x + (y + (z * ZSIZE)) * YSIZE 
    

    然后:

    x = index % YSIZE
    Y = ((index - x) / YSIZE) % ZSIZE
    Z = (index - x - y*YSIZE) / (ZSIZE * YSIZE)
    
    推荐文章