将2D转换的1D阵列顺时针旋转90度



这几天我一直在纠结,我快要疯了。本质上,我已经将值的2D阵列(图像,我知道有更简单的方法可以实现这一点,但我有明确的要求)转换为1D阵列。我可以轻松地旋转2D阵列。我在旋转1D版本的数组时遇到了问题,我认为这是因为一行算法不正确。我用来旋转阵列的代码是:

cout << "nTransfer from 2D to dynamic 1D array and print detailsnn";
myImage * p_myImage = new myImage[total];
    for (int y = 0; y < height; y++)
{
    for (int x = 0; x < width; x++)
    {
        int offset = width * y + x;
        p_myImage[offset].pixel = array2D[y][x];
        cout << p_myImage[offset].pixel << " ";
    }
  cout << "n";
}
//new pointer to copy to
myImage * p_myImage2 = new myImage[total];
cout << "nRotate Matrix through 1D arraynn";
for (int x = 0; x < width; x++)
{
    for (int y = 0; y < height; y++)
    {
        int offset = height * x + y;
        //int offset = width * y + x ;
        p_myImage2[offset].pixel = p_myImage[height-1+x].pixel;
        cout << p_myImage2[offset].pixel << " ";
    }
  cout << "n";
}

这应该顺时针旋转:

p_myImage2[offset].pixel = p_myImage[width * (height - 1 - y) + x].pixel;

最新更新