카테고리 없음

알고리즘/ 3차원 배열의 인덱스를 1차원으로 변환하기

cleitia 2020. 2. 27. 10:39

3차원 영상(공간?) 도메인에서 3차원 배열의 인덱스를 1차원으로 변환하는 식은 아래와 같음

public int to1D( int x, int y, int z ) {
    return (z * xMax * yMax) + (y * xMax) + x;
}

public int[] to3D( int idx ) {
    final int z = idx / (xMax * yMax);
    idx -= (z * xMax * yMax);
    final int y = idx / xMax;
    final int x = idx % xMax;
    return new int[]{ x, y, z };
}

https://stackoverflow.com/questions/7367770/how-to-flatten-or-index-3d-array-in-1d-array

 

How to "flatten" or "index" 3D-array in 1D array?

I am trying to flatten 3D array into 1D array for "chunk" system in my game. It's a 3D-block game and basically I want the chunk system to be almost identical to Minecraft's system (however, this i...

stackoverflow.com