现在被困在这几天,我出去了。基本上我已经将2D数组值(一个图像,我知道有更简单的方法来实现这个,但我有明确的要求)转换为一维数组。我可以轻松旋转2D阵列。我在旋转阵列的1D版本时遇到了麻烦,我认为这是由于单行算法不正确。 我用来旋转数组的代码是:
cout << "\nTransfer from 2D to dynamic 1D array and print details\n\n";
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 array\n\n";
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";
}
答案 0 :(得分:1)
这应该顺时针旋转:
p_myImage2[offset].pixel = p_myImage[width * (height - 1 - y) + x].pixel;