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;
  }
bknight
  • 85
  • 7

2 Answers2

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;
}
Andrew Alderson
  • 893
  • 12
  • 18
  • thanks man, it worked like a charm! any tips on upgrading this to a non square matrix? – bknight Oct 16 '14 at 23:40
  • 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. – Andrew Alderson Oct 16 '14 at 23:41
  • yeah I've done it that way, now just have to solve something wrong with the loops, currenty doing it this way: http://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. – bknight Oct 16 '14 at 23:57
  • `mirror[i][j] = pixel[j][w-i-1];` should be `mirror[i][j] = pixel[j][h-i-1];` – Andrew Alderson Oct 17 '14 at 03:56
  • yeah it's rotating fine now! last problem is my getRGB algorithm which is messing up when h != w, http://pastebin.com/JVHAe55y , cant really find why. thx again for all the help. – bknight Oct 17 '14 at 12:07
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.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268