2016-03-23 155 views
0

我有代碼翻轉32 x 32像素的數組,但我不知道如何水平翻轉它。水平翻轉像素陣列

這裏是垂直翻轉的代碼。

for (int i = 0; i < pixels.length; i++) { 
     newPixels[(i/32) * 32 
       + (i % 32)] = pixels[(32 - (i/32) - 1) * Grid.SIZE + (i % 32)]; 
} 

由於32是寬度高度,這是不言而喻在所有這些地方

+1

這很奇怪。 '(i/32)* 32 +(i%32)'只是混淆的說'i'的方式。你能否更詳細地解釋這個問題? –

回答

1
int imageWidth = 32; 
for (int i = 0; i < pixels.length; i++) { 
    newPixels[i] = pixels[i - 2 * (i % imageWidth) + imageWidth - 1]; 
} 

使用的指數是(i/imageWidth) * imageWidth = i - (i % imageWidth)(該行的偏移)和imageWidth - (i % imageWidth) - 1總和( x位置鏡像在中心)