Reputation: 6988
I have a 3d array which is basically 9 chunks of 2d arrays defined as 100x100 where they would be lined up in a 3x3 grid. I want to transform this 3D array into just a 2D array.
array[9][100][100] needs to be turned to array[900][900]
Any ideas on how I'd go about this?
[EDIT]
So I'm going to lower the counts to make it more manageable. I'll use array[9][5][5];
Using the answer below if I init the array and print out to file I don't get what I would expect. Given how I init, what I expect, and want, is a file that looks like:
000001111122222
000001111122222
000001111122222
000001111122222
000001111122222
333334444455555
333334444455555
333334444455555
333334444455555
333334444455555
666667777788888
666667777788888
666667777788888
666667777788888
666667777788888
So I made the value's tie to the first dimension. It gives me 9 quadrants of 2D arrays. This is the layout I want. So the first dimension is really defining the quadrant that the other 2 dimensions should be placed. So the point of this is that each quadrant 2D array will hold the tile id to draw our map. I want to do this because the player will start in the center (quadrant 4) but as he moves to say quadrant 5, the server (it's an mmo) will see that it moved to the right out of center quadrant, and so it'll need to send the entire right side quadrants of more map data.
Basically it's send a new 2, 5, & 8 quadrant of map data. I'll them shift the 3D array accordingly (it's easy at this point right. just move around entire quadrants), then convert to the 2D array on the client so that I can easily find what sub part of this 2D map I need to draw since I can find out the exact tile the player is on in the 2D array with simple math. This gives me a streaming map that can be massive on the server, but only chunks sent to the client when they need it.
for (j = 0; j < 5; j++)
{
for (k = 0; k < 5; k++)
{
a[0][j][k] = 0;
a[1][j][k] = 1;
a[2][j][k] = 2;
a[3][j][k] = 3;
a[4][j][k] = 4;
a[5][j][k] = 5;
a[6][j][k] = 6;
a[7][j][k] = 7;
a[8][j][k] = 8;
}
}
Upvotes: 0
Views: 3711
Reputation: 14768
You can represent any n-dimensional array as an m-dimensional array, as long as you have m <= n; m > 0
.
The task would be simply to keep track of where is what, and how to reach it.
In your case, converting array[9][100][100]
into a two dimensional array could be done as follows:
d1 = 0;
d2 = 0;
a = array[9][100][100];
b = array[2][9 * 100 * 100 / 2];
for (unsigned int i(0); i < 9; i++) {
for (unsigned int j(0); j < 100; j++) {
for (unsigned int k(0); k < 100; k++) {
b[d1][d2] = a[i][j][k];
d2++;
d1 = (d2 == 9 * 100 * 100 / 2) ? 1 : 0;
d2 = (d2 == 9 * 100 * 100 / 2) ? 0 : d2;
}
}
}
Anything other than this would require a special mapping, which you need to provide in order to say where will the value a[i][j][k]
be stored in b[d1][d2]
.
Considering your answer, i guess this is what you're looking for:
int d1 = 0;
int d2 = 0;
int a[9][100][100];
int b[300][300];
unsigned int i(0), j(0), k(0);
for (i = 0; i < 9; i++) {
for (j = 0; j < 100; j++) {
for (k = 0; k < 100; k++) {
b[d1 + j][d2 + k] = a[i][j][k];
}
}
d1 = (d1 + j == 300) ? 0 : d1 + j;
d2 = (d2 + k == 300) ? 0 : d2 + k;
}
Regards!
Upvotes: 1