首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >Java:将图像旋转90度

Java:将图像旋转90度
EN

Stack Overflow用户
提问于 2016-05-14 00:08:07
回答 2查看 1K关注 0票数 1

我正在尝试编写一个将图像向右旋转90度的方法。我已经查看了关于这个话题的其他帖子,但似乎没有一个能帮助我解决我的问题。我的代码似乎在纸面上可以工作,但我不知道为什么j-unit测试不能通过。为什么我的j-unit测试没有通过?

我的代码:

代码语言:javascript
代码运行次数:0
运行
复制
 /**
* intialize a picture by giving an image matrix
* @param imageMatrix two dimansionar RGBColor array
*/
public Picture(RGBColor imageMatrix[][]){
    this.width = imageMatrix.length;
    this.height = imageMatrix[0].length;
    this.imageMatrix = imageMatrix;
}

/**
 * turns this picture 90 degrees to the right
 *
 */
public void rot90DegRight(){
    int w = imageMatrix.length;
    int h = imageMatrix[0].length;
    RGBColor[][] rotatedMatrix = new RGBColor[h][w];
    for (int i = 0; i<h; i++){
        for(int j = 0; j<w; j++){
            rotatedMatrix[i][j] = imageMatrix[w-j-1][i];
        }
    }

}

下面是j-unit测试用例:

代码语言:javascript
代码运行次数:0
运行
复制
@Test(timeout=1000)
public void testRot90DegRight(){
    RGBColor[][] imageMatrix = new RGBColor[100][100];
    for (int w=0; w<100; w++){
        for (int h=0; h<100; h++){
            if ((w==20) & (h==20)){
                imageMatrix[w][h] = new RGBColor(255,255,255);
            } else {
                imageMatrix[w][h] = new RGBColor(0,0,0);
            }
        }
    }
    Picture p = new Picture(imageMatrix);
    p.rot90DegRight();
    assertTrue("The white pixel was not rotated", !(p.getImageMatrix()[20][20].isWhite()));
    assertTrue("The white pixel was not rotated", (p.getImageMatrix()[79][20].isWhite()));

}
EN

回答 2

Stack Overflow用户

发布于 2016-05-14 00:17:16

您在rot90DegRight()中创建了rotatedMatrix并在其中分配了一些值,但随后简单地丢弃了结果。你必须将旋转的结果存储在某个地方。

添加

代码语言:javascript
代码运行次数:0
运行
复制
this.imageMatrix = rotatedMatrix;

在外部for循环之后可能会使其工作。

请注意,这将使它不再引用在执行旋转后传递给构造函数的数组。

票数 2
EN

Stack Overflow用户

发布于 2016-05-14 00:23:32

米凯猫是对的。它就像这样(简单地说):

假设你正在尝试旋转这个双精度数组:

1 2 3 4

1 2 3 4

1 2 3 4

1 2 3 4

使用您的方法,在替换为之后,您最终会返回到您的循环中,并替换为。数组将在中途自行撤消,留下相同的结果。

希望这能有所帮助!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37214648

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档