I want to rotate a 2 x 2 2d array clockwise 90 degrees.
+------+
| 0 1 |
| 3 2 |
+------+
for example, the above table is represented in an array [0, 1, 2, 3]
I want to rotate it clockwise to
+------+
| 3 0 |
| 2 1 |
+------+
I used the algorithm shared here Rotate a one-dimensional array matrix
My code can rotate 0, 2, 3 but not 1.
int myArray = [0, 1, 2, 3];
int[] myArrayCopy = new int[myArray.length];
System.arraycopy(myArray, 0, myArrayCopy, 0, myArray.length);
int length = myArray.length / 2;
for(int x = 0; x < length; x++) {
for(int y = 0; y < length; y++) {
pixel[(x * length) + y] = myArrayCopy[(length - y - 1) * (length + x)];
System.out.println("x" + x + " y" + y + " " + " move " + (length - y - 1) * (length + x) + " to " + (x * length + y));
}
}
This prints the result
x0 y0 move 2 to 0 (this means moving index 2 to index 0)
x0 y1 move 0 to 1
x1 y0 move 3 to 2
x1 y1 move 0 to 3
it missed moving index 1 to index 3 and moving index 0 shows twice. How to fix the algorithm to make it work ?