Rotating around the centre point on Z axis
Hello,
I am currently trying to write a Tetris clone but I am having trouble rotating a 4x4 grid around the centre point.
I am using the standard formula for rotating around the z axis using a 5x5 grid
x' = cosine(90)*(x-2) - sine(90)*(y-2) + 0
y' = sine(90)*(x-2) + cosine(90)*(y-2) + 0
The -2 is applied so that it refers to the centre block (2,2).
The grid is stored in a two dimensional int array.
The formula above does not work for a 4x4 grid because there is no centre block.
What formula do I need for a 4x4 grid.
Thanks
[591 byte] By [
ANDREWP7a] at [2007-11-27 2:49:44]

# 1
There are 7 tetrominos. It will probably be quicker to hard-code the rotation than to write a generic rotation algorithm for them.However, in direct answer to your question: you can't just rotate the 4x4 grid around its centre, because the resulting grid won't line up properly.
# 2
> However, in direct answer to your question: you can't
> just rotate the 4x4 grid around its centre, because
> the resulting grid won't line up properly.
I beg to differ
If your (x,y) coordinates are in fact the non-negative valued indexes into a 2D square array, you will find that the transformation
x' = -y + n;
y' = x;
where n is the maximum dimension of the array (so for a 4x4 array n = 3)
will rotate the 2D array.
in other words, here is code that performs a 90 degree rotation on a 2D square matrix of size n.
int n = 4;
int[][] oldA = new int[n][n];
int[][] newA = new int[n][n];
...
for(int x = 0; x<n; x++){
for (int y = 0; y><n; y++){
newA[n-1-y][x] = oldA[x][y];
}
}
you are just rotating about the origin, which moves points in the first quadrant (both coordinates positive) to the second quadrant. Then you translate the coordinates back to the first quadrant.
As always, I don't test code, I just write it and post it. Feel free to check that it actually works as advertised. :)>