1.Multi-Dimension
x axis: length; y axis: height; z axis: depth
2.Native Multi-Dimensional Arrays
int array2d[height][width];
int array3d[depth][height][width];
address(array[z][y][x]): (z * width * height) + (y * width) + x
3.Array2D Class
[Data]
template <class Datatype>
class Array2D
{
Datatype* m_array;
int m_width;
int m_height;
};
[Cons & DeCons]
Array2D(int p_width, int p_height)
{
m_array = new Datatype[p_width * p_height];
m_width = p_width;
m_height = p_height;
}
~Array2D()
{
if (m_array != 0)
delete[] m_array;
m_array = 0;
}
eg: Array2D<int> intarray(10, 10);
[Get Func]
Datatype& Get(int p_x, int p_y)
{
return m_array[p_y * m_width + p_x];
}
eg:
intarray.Get(4, 5) = 10;
int value = intarray.Get(4, 6);
[Resize Func]
void Resize(int p_width, int p_height)
{
Datatype* newarray = new Datatype[p_width * p_height];
if (newarray == 0)
return;
int x, y, t1, t2;
int minx = (p_width < m_width ? p_width : m_width);
int miny = (p_height < m_height ? p_height : m_height);
for (y = 0; y < miny; y++)
{
t1 = y * p_width;
t2 = y * m_width;
for (x = 0; x < minx; x++)
{
newarray[t1 + x] = m_array[t2 + x];
}
}
if (m_array != 0) delete[] m_array;
m_array = newarray;
m_width = p_width;
m_height = p_height;
}
[Size]
int Width()
{
return m_width;
}
int Height()
{
return m_height;
}
int Size()
{
return m_width * m_height;
}
4.Array3D Class
...