我有以下代码以对角线反映图像:
public void mirrorDiagonal() {
Pixel[][] pixels = this.getPixels2D();
Pixel topRightPixel = null;
Pixel bottomLeftPixel = null;
int maxLength;
if (pixels.length < pixels[0].length) { maxLength = pixels.length; }
else {maxLength = pixels[0].length; }
for (int row = 0; row < maxLength; row++)
{
for (int col = row; col < maxLength; col++)
{
topRightPixel = pixels[row][col];
bottomLeftPixel = pixels[col][row];
bottomLeftPixel.setColor(topRightPixel.getColor());
}
}
}
但是我把某个地方搞砸了,它从图像的右上角反射到左下方。
我的问题:我还将如何反映其他方式?(更具体地说是从左上角到右下角(
看起来您认为是右上角的像素实际上是左上角。尝试以下内容:
for (int row = 0; row < maxLength; row++) {
for (int col = maxLength - row - 1; col >= 0; col--) {
topRightPixel = pixels[row][col];
bottomLeftPixel = pixels[col][row];
bottomLeftPixel.setColor(topRightPixel.getColor());
}
}
为什么要更改颜色?而是可以移动像素对象。另外,如果您定义了3个基本转换,则可以通过不同的方式组合前3个来构建所有其他转换。例如
static void mirrorHor(Pixel[][] pixels) {
for(int row = 0; row < pixels.length; row++) {
for(int col = 0; col + col < pixels[row].length - 1; col++) {
Pixel p = pixels[row][col];
pixels[row][col] = pixels[row][pixels[row].length - col - 1];
pixels[row][pixels[row].length - col - 1] = p;
}
}
}
static void mirrorVert(Pixel[][] pixels) {
for(int row = 0; row + row < pixels.length - 1; row++) {
Pixel[] r = pixels[row];
pixels[row] = pixels[pixels.length - row - 1];
pixels[pixels.length - row - 1] = r;
}
}
static void mirrorTopRightBottomLeft(Pixel[][] pixels) {
for(int row = 0; row < pixels.length - 1; row++) {
for(int col = row + 1; col < pixels.length; col++) {
Pixel p = pixels[row][col];
pixels[row][col] = pixels[col][row];
pixels[col][row] = p;
}
}
}
static void rotate180(Pixel[][] pixels) {
mirrorHor(pixels);
mirrorVert(pixels);
}
static void mirrorTopLeftBottomRight(Pixel[][] pixels) {
mirrorTopRightBottomLeft(pixels);
rotate180(pixels);
}
static void rotateLeft90(Pixel[][] pixels) {
mirrorTopRightBottomLeft(pixels);
mirrorVert(pixels);
}
static void rotateRight90(Pixel[][] pixels) {
mirrorTopRightBottomLeft(pixels);
mirrorHor(pixels);
}