Say you have a 3-dimensional array in c#
int space[width, height, depth];
And you would like to implement the method
public int[,] GetCrossSection(int position, int dimension)
Where 'position' is the point along the 'dimension' specified where you would like to extract your slice. It is important to not use the fact that we are only dealing with 3 dimensions, in the examples below you could fix them by adding if statements and assume the matrix will not grow beyond 3 dimensions.
My first attempt (commented problem areas):
public int[,] GetCrossSection(int position, int dimension)
{
int[] dimensionIterationInterval = new int[] { width, height, depth };
var dims = new List<int>(dimensionIterationInterval);
dims.RemoveAt(dimension);
dimensionIterationInterval = dims.ToArray();
int[,] crossSection = new int[dimensionIterationInterval[0], dimensionIterationInterval[1]];
int[] itr = new int[2];
for (itr[0] = 0; itr[0] < dimensionIterationInterval[0]; itr[0]++)
{
for (itr[1] = 0; itr[1] < dimensionIterationInterval[1]; itr[1]++)
{
crossSection[itr[0], itr[1]] = space[?,?,?]; //Problem
}
}
}
And my second attempt, equally futile:
public int[,] GetCrossSection(int position, int dimension)
{
int[,] dimensionIterationInterval = new int[,] { { 0, width }, { 0, height }, { 0, depth } };
dimensionIterationInterval[dimension, 0] = position;
dimensionIterationInterval[dimension, 1] = position + 1;
int[,] crossSection = new int[?,?]; //Problem
for (int x = dimensionIterationInterval[0, 0]; x < dimensionIterationInterval[0, 1]; x++)
{
for (int y = dimensionIterationInterval[1, 0]; y< dimensionIterationInterval[1, 1]; y++)
{
for (int z = dimensionIterationInterval[2, 0]; z < dimensionIterationInterval[2, 1]; z++)
{
crossSection[?, ?] = space[x, y, z]; // Problem
}
}
}
}
Both those attemps run into dead ends. How would you solve it? It's ok to have the fixed iteration loops for the number of dimensions of space[,,]. If the number of dimensions grow that is somewhat managable. Clever/limited if statments could work, but not excessive ifs for each dimension.