Data.Structures.For.Game.Programmers.PART2.Basics.5.Multi_Dimensional_Arrays

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
  ...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值