#### Archived

This topic is now archived and is closed to further replies.

# Rotating a 2D Array...

This topic is 5933 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

If I have a 4x4 array: 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1 1 How do I rotate the data so that I end up with this: 0 0 0 0 1 0 0 0 1 0 0 0 1 1 0 0 Any help would be much appreciated, I've tried several things, but none seem to work. Jonoxon [edited by - Jonoxon on September 16, 2002 9:13:30 AM]

##### Share on other sites
If you read the last first, then the second last etc you''ll have it rotated as you wish:

0000
1000
1000
1100

##### Share on other sites
To understand how and why that works, look up matrix transposition.

"With my feet upon the ground I lose myself between the sounds and open wide to suck it in, I feel it move across my skin. I''m reaching up and reaching out. I''m reaching for the random or what ever will bewilder me, what ever will bewilder me. And following our will and wind we may just go where no one''s been. We''ll ride the spiral to the end and may just go where no one''s been." - Maynard James Keenan
[TheBlackJester ]
[Wildfire Studios ]

##### Share on other sites
OK, thanks. I''ll do that.

Jonoxon

##### Share on other sites
it''s almost the tranpose. kind of the mirror of the transpose... why do u want to do that transform?

##### Share on other sites
OK, here's what I did, in C++ (it works)

  int temp[4][4];int k=3;for(int i=0;i<4;i++){	for(int j=0;j<4;j++)	{			temp[i][j]=CurrentTet[j][k];	}	k--;}//Here I do collision checking, making sure that the rotation is valid etc.for(i=0;i<4;i++){	for(int j=0;j<4;j++)	{		CurrentTet[i][j] = temp[i][j];	}}

The reason I want to do this transform is for something that no programmer would be complete without, my first Tetris clone. This function rotates the tetraminoes 90 degrees.

Jonoxon

[edited by - Jonoxon on September 16, 2002 12:01:01 PM]

##### Share on other sites
It is not the tranpose of the matrix.
For example the (3,4) entry, a 1, should be at (4,3) in the tranpose. But it isn''t, there''s a zero there. The transpose would be:
0000
0001
0001
0011

##### Share on other sites
i could apply the 2d rotation matrix:

cos(t) -sin(t)
sin(t) cos(t)

where t is the rotation angle.

##### Share on other sites
The one I have works just fine. If you look you will see that I have a transposed matrix that has been mirrored(transposition causes the data to be mirrored anyway, so mirroring is necessary to maintain the original shape)

Mirror  | Transpose0 0 0 0 | 0 0 0 01 0 0 0 | 0 0 0 11 0 0 0 | 0 0 0 11 1 0 0 | 0 0 1 1

Jonoxon

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633703
• Total Posts
3013455
×