2次元を使用して1次元メモリにどのように対処しますか?(値を1次元配列として格納する値を取得するなど(Matrix::ValueAt(row, col)
4x4マトリックスの場合)。Matrix
float m[16]
class Matrix4x4
{
private float m[16];
float getValueAt(int row, int col)
{
// I want this function
}
}
、m[row * 4 + col]
またはその逆。
一般に:
Matrix(i,j) = m[i * 列 + j]
コンパイラにそれを理解させます:
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];
}
}