我需要反转存储在double [] img中的给定长度和宽度的图像; 这是我第一次使用数组。指令是嵌套for循环,y(行)上的外循环和x(列)上的内循环,并反转每个horizantal数组。 这就是我所拥有的,它不起作用。
width = ImageLibrary.getImageWidth();
height = ImageLibrary.getImageHeight();
for(i = 0; i < width ; i++){
for(j = 0; j < height ; j++){
for(int k = 0; k < img.length/2; k++){
double temp = img[k];
img[i] = img[img.length - k - 1];
img[img.length - k - 1] = temp;
}
}
}
我真的不确定该怎么办?当它说要反转水平阵列时,我这样做是否正确? 谢谢
答案 0 :(得分:3)
我认为你所寻找的更像是
width = ImageLibrary.getImageWidth();
height = ImageLibrary.getImageHeight();
// Loop from the top of the image to the bottom
for (y = 0; y < height ; y++) {
// Loop halfway across each row because going all the way will result
// in all the numbers being put back where they were to start with
for (x = 0; x < width / 2 ; x++) {
// Here, `y * width` gets the row, and `+ x` gets position in that row
double temp = img[y * width + x];
// Here, `width - x - 1` gets x positions in from the end of the row
// Subtracting 1 because of 0-based index
img[y * width + x] = img[y * width + (width - x - 1)];
img[y * width + (width - x - 1)] = temp;
}
}
这将镜像图像所以现在左侧是右侧,右侧是左侧