2013-11-22 42 views
-1

現在已經停留在這幾天了,我正在走出我的想法。本質上,我已經將二維數組數組(一個圖像,我知道有更簡單的方法來實現這一點,但我有明確的要求)轉換爲一維數組。我可以輕鬆地旋轉二維數組。我在旋轉數組的1D版本時遇到了麻煩,我認爲這只是一行算法不正確。 我使用的陣列旋轉的代碼是:順時針旋轉2D轉換1D陣列90度

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

樣品的輸入和輸出將是有益的。描述你認爲是錯誤的(超出'我認爲某條線是錯誤的'),或者你不瞭解會改善這個問題。 SO不是調試服務。 – Caleb

回答

1

這應該順時針旋轉:

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

非常感謝您:-) – shineSparkz

+0

如果它解決了您的問題,請將答案設置爲已接受。謝謝 – ghembo