0

您将如何使用二维来处理一维内存?(例如获取将值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
    }

}
4

3 回答 3

7

m[row * 4 + col],或相反。

于 2012-08-21T07:46:25.057 回答
0

让编译器弄清楚:

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];
    } 
}
于 2012-08-21T07:55:37.820 回答
0

一般来说:

矩阵(i,j) = m[i * 列 + j]

于 2012-08-21T08:10:08.017 回答