java - 交换和旋转二维数组

标签 java rotation swap multidimensional-array

我需要实现两种适用于二维数组的方法:

  • grayAndFlipLeftToRight - 此方法将图像转换为灰度值的二维数组,然后从左向右翻转。也就是说,原来面向左的对象现在将面向右。最左边一列的元素将与最右边一列的元素交换,依此类推。

  • grayAndRotate90 - 此方法将图像转换为灰度值的二维数组,然后将其顺时针旋转 90 度(将其放在右侧)。

    <

代码:

public class PictureUtil
{
    /**
     * Gets a version of the given Picture in gray scale and flipped left to right
     * @param pic the Picture to convert to gray scale and flip
     * @return a version of the original Picture in gray scale and flipped
     * left to right.
     */
    public static Picture grayAndFlipLeftToRight( Picture pic)
    {
        int[][] pixels = pic.getGrayLevels();

        for (int i = 0; i < pixels.length; i++) {             
            for (int fromStart = 0; fromStart < pixels[0].length; fromStart++) { 
                int fromEnd = pixels[0].length-1;

                while (fromEnd >= 0) {
                    int saved = pixels[i][fromStart];
                    pixels[i][fromStart] = pixels[i][fromEnd];
                    pixels[i][fromEnd] = saved; 

                    fromEnd--;
                }                                               
            }
        }

        return new Picture(pixels);
    }

    /**
     * Gets a version of the given Picture in gray scale and rotated 90 degrees clockwise
     * @param pic the Picture to convert to gray scale and rotate 90 degrees clockwise
     * @return a version of the original Picture in gray scale and rotated 90 degrees clockwise
     */
    public static Picture grayAndRotate90( Picture pic)
    {
        int[][] pixels = pic.getGrayLevels(); 

        int numberOfColums = pixels.length;
        int numberOfRows = pixels[0].length;        
        int[][] rotate = new int[numberOfRows][numberOfColums];

        for (int i = 0; i < pixels.length; i++) { 
            int seek = 0;
            for (int j = 0; j < pixels[0].length; j++) {            
                pixels[i][j] = rotate[seek][numberOfColums - 1];
                seek++;
            }
        }

        return new Picture(rotate); 
    }
}

我的实现对于这两种方法都无法正常工作。

  • 如何解决这个问题?

最佳答案

我认为你把循环弄乱了一点。这应该可以完成你的工作:

public static Picture grayAndFlipLeftToRight( Picture pic)
{
    int[][] pixels = pic.getGrayLevels();

    for (int i = 0; i < pixels.length; i++) {
        for (int curr = 0; curr < (pixels[0].length + 1) / 2; curr++) {

            int saved = pixels[i][curr];
            pixels[i][curr] = pixels[i][pixels[0].length - 1 - curr];
            pixels[i][pixels[0].length - 1 - curr] = saved;
        }
    }

    return new Picture(pixels);
}

public static Picture grayAndRotate90( Picture pic)
{
    int[][] pixels = pic.getGrayLevels(); 

    int[][] rotate = new int[pixels[0].length][pixels.length];

    for (int i = 0; i < pixels[0].length; i++) {
        for (int j = 0; j < pixels.length; j++) {
            rotate[i][pixels.length - 1 - j] = pixels[j][i];
        }
    }
    return new Picture(rotate); 
}

代码中的一个错误是该行(在旋转方法中):

pixels[i][j] = rotate[seek][numberOfColums - 1];

您正在修改输入数组本身的内容,并且对输出旋转没有执行任何操作。

关于java - 交换和旋转二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17944603/

相关文章:

java - Web Service wsse 安全实现

java - 设立新的服务领导者并通知领导者

android - 我旋转的 TextView 被截断了。我必须做什么?

Javascript:如何移动与其旋转相关的上下文对象

postgresql - Postgres 在 CentOS 上导致交换

c - 在 C 中交换 2 个数组时的困难

java - 与 Java 线程的简单通信

java - 模拟长时间运行的操作

javascript - jquery/CSS 旋转隐藏了一半的图像?

Python 数组旋转