我需要反转存储在double[] img中的给定长度和宽度的图像;这是我第一次使用数组。指令将嵌套for循环、y(行)上的外循环和x(列)上的内循环,并反转每个水平数组。这就是我所拥有的,但它不工作。
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;
}
}
} 我真的不知道该怎么办?当它说要反转水平数组时,我做得对吗?谢谢
发布于 2013-03-28 03:06:21
我觉得你要找的更像这样
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;
}
}这将镜像图像,因此现在左侧是右侧,右侧是左侧
https://stackoverflow.com/questions/15667170
复制相似问题