views:

239

answers:

3

Hi, how can i rotate 2d rectangular array of integers that has odd number of rows by 45 degrees

so something like

int[] myArray = new int[,]
{
{1, 0 ,1},
{0, 1 ,0},
{0, 0 ,0},
}

into

int[] rotatedArray = new int[,]
{
{0, 1 ,0},
{0, 1 ,1},
{0, 0 ,0},
}

for any dimension (3x3, 5x5, 7x7, etc.)

by this "formula" http://yfrog.com/n6matrix45p

5x5

0 0 0 0 0
2 0 0 0 0
1 1 1 1 1
0 0 0 0 0
0 0 0 0 0

into

1 2 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1

5x5

0 0 0 3 0
0 0 0 3 0
0 0 0 3 0
0 0 0 3 0
0 0 0 3 0

into

0 0 0 0 0
0 0 0 0 3
0 0 0 3 0
0 0 3 3 0
0 3 0 0 0

A: 

You can try this library:

Math.NET Project for matrices operations... http://numerics.mathdotnet.com/

This code appears to be useful too:

http://www.drunkenhyena.com/cgi-bin/view_net_article.pl?chapter=2;article=28#Rotation

Don't forget the DirectX managed and unmanaged namespaces and classes. Lots and lots of good stuff there to check.

For example:

Matrix..::.Rotate Method (Single, MatrixOrder)

Leniel Macaferi
those matrices are only 4x4 or 3x3, i'll try math.net, but i fear this rotation is too specific
Kikaimaru
These are rotation matrices for transformations. A completely different thing.
Cloudanger
A: 

I think we have these rules:

  1. Imagine the matrix as a set of "frames or boxes without centers" within each other like "Russian dolls".

  2. Elements at the center of a side (top/left/right/bottom) move towards the nearest corner clockwise.

  3. Corners move towards the next center clockwise.

  4. Elements that are not corners nor centers move to the next spot (clockwise) that is the same distance from a corner as they currently are.

I've started writing some code but I don't think it's trivial and I haven't had time to test.

Graphain
+1  A: 
Kikaimaru