1

I have this as a color array:

public RGBImage(int width, int height, RGBColor background) {
      pixel =  new RGBColor[width][height];
      this.w = width;
      this.h = height;
      this.b = background;

      for(x = 0; x < width; x++){
          for(y = 0; y < height; y++){
              pixel[x][y] = b;
          }
      }

and i want to rotate it, right, the code is already doing good regarding square matrix thanks to @Oblivion Creations, but I'm getting outofbounds errors when using it non squared matrixes

   public void rotateRight() {
      RGBColor[][] mirror = new RGBColor[h][w];
              for(int i = 0 ; i < h; i++){
                  for(int j = 0 ; j < w; j++){
                      mirror[i][j] = pixel[j][w-i-1];
                  }
              }
            pixel = mirror;
  }

2 Answers 2

2

I think the problem is mirror = pixel; sharing the same reference... So it is altering itself as it is rotating, causing it to do weird and wonderful things.

My suggestion would be to copy from pixel to the mirror, then assign the mirror to pixel after the for loop, like so:

public void rotateLeft() {
    RGBColor[][] mirror = new RGBColor[w][h];
    if(w == h){
        for(int i = 0 ; i < h; i++){
            for(int j = 0 ; j < h; j++){
                mirror[i][j] = pixel[h-j-1][i];
            }
        }
        pixel = mirror;
    }
}

Edit:

For your new RotateRight, you're using the width variable instead of the height on the unrotated pixel array. Try this instead:

public void rotateRight() {
   RGBColor[][] mirror = new RGBColor[h][w];
           for(int i = 0 ; i < h; i++){
               for(int j = 0 ; j < w; j++){
                   mirror[i][j] = pixel[j][h-i-1];
               }
           }
         pixel = mirror;
}
Sign up to request clarification or add additional context in comments.

5 Comments

thanks man, it worked like a charm! any tips on upgrading this to a non square matrix?
No worries! Actually yeah, I was thinking about that after I answered... Change the creation of mirror to RGBColor[][] mirror = new RGBColor[h][w]; (reversing the width and height), remove the if check and it should be right to go.
yeah I've done it that way, now just have to solve something wrong with the loops, currenty doing it this way: pastebin.com/pXKxH73p but having out of bounds, which makes sense, just quite haven't figure it out to stop it on time. thx again.
mirror[i][j] = pixel[j][w-i-1]; should be mirror[i][j] = pixel[j][h-i-1];
yeah it's rotating fine now! last problem is my getRGB algorithm which is messing up when h != w, pastebin.com/JVHAe55y , cant really find why. thx again for all the help.
0

The problem is that mirror = pixel assigns mirror to refer to the same array as pixel. It does not copy the array. So then in your loop, pixel[i][j] = mirror[h-j-1][i] copies a pixel from one cell of the array to another cell of the same array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.