您将如何使用二维来处理一维内存?(例如获取将值Matrix::ValueAt(row, col)
存储Matrix
为一维数组的值(float m[16]
对于 4x4 矩阵)。
class Matrix4x4
{
private float m[16];
float getValueAt(int row, int col)
{
// I want this function
}
}
与m[row * 4 + col]
,或相反。
让编译器弄清楚:
class Matrix4x4
{
private:
union
{
float m[16];
float m2[4][4];
};
public:
float getValueAt(int row, int col)
{
return m2[row][col];
}
float getValueAtLinear(int i)
{
return m[i];
}
}
一般来说:
矩阵(i,j) = m[i * 列 + j]