[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 321

# JavaScript, C++, Generic

I need some type of standard way of rotating weird sized matrices, like 2x4 (or 4x2, im never quite certain if m = rows or columns when dealing with an mXn, and this is even after Linear Algebra..sigh)

Example:
[  1  1  1  1  ]
[  1  0  0  0  ]

Rotating this 90 degrees, I would like to have an output of
[  1  1  ]
[  0  1  ]
[  0  1  ]
[  0  1  ]

Another example,

[  1  1  ]
[  1  0  ]

rotated -90

[  1  0  ]
[  1  1  ]

This application is being done in Java, and I'm using a double[][] array.

Many thanks
0
Lo-Tan
• 2
• 2
• 2
• +3
2 Solutions

Commented:
>>JavaScript

You're in the Java TA ...
0

Commented:
If you have a m x n matrix (standard notation for m rows, n columns) by rotation you will get a new matrix of size n x m.

There are a few ways of approaching matrix rotation. One way is as follows:
- for rotation by -90, reverse the order of each element in each row ("mirror" the matrix) then transpose
- for rotation by +90, swap the order of the rows ("flip" the matrix) then transpose
0

Author Commented:
I figured Game programmers be the best to ask...

Is there not some form of cos/sin combination of a matrix I could use to multiple the current matrix by?

ie something like this works for rotating on the z axis
[  cos(x)  -sin(x)      0 ]
[  sin(x)   cos(x)      0  ]
[  0           0              1  ]

I just don't know how I can apply the theory behind this matrix to matrices of indefinite sizes.  I will try your solution, although I would truly like to have a Java Matrix class that has rotate(degrees) and doesn't do something arbitrary to -only- allow rotations of 90/-90 increments.

[CEHJ:  You're in the Java TA ...]
What's that?  Topic Announcement??
0

Commented:
That sort of matrix is used in image rotation, where the new coordinates are given in terms of the original ones rotated in a certain way. Unless I'm mistaken, you want to rotate the actual matrix (based on the examples you provided). I don't understand how your idea of "rotate(degrees)" would apply to the matrices you showed.

[ TA = Topic Area. Your title had JavaScript and C++ but the topic itself was posted in Java. ]
0

Commented:
>>
[CEHJ:  You're in the Java TA ...]
What's that?  Topic Announcement??
>>

Topic Area
0

Author Commented:
[That sort of matrix is used in image rotation]

Doesn't the same also apply to my matrix?  Isn't an image just thousands of pixels (RGB) located in an array, and rotation on those changes the indices of the pixels and their color values.

ie one pixel could be something like

pixels[0][0] = 255; // R
pixels[0][1] = 128; // G
pixels[0][2] = 200; // B
0

Commented:
Singular Value Decomposition(SVD) sounds like something you might be interested in, though it's a bit beyond me.

0

Commented:
From your basic examples, I scratched out these methods which, if I understand you correctly, seem to do what you want:

private static int[][] rotateLeft(int[][] matrix){
int[][] result=new int[matrix[0].length][matrix.length];

for(int i=matrix[0].length-1, j=0; i>=0; i--, j++){
for(int k=0; k<matrix.length; k++){
result[j][k]=matrix[k][i];
}
}

return result;
}

private static int[][] rotateRight(int[][] matrix){
int[][] result=new int[matrix[0].length][matrix.length];

for(int i=0; i<matrix[0].length; i++){
for(int j=0, k=matrix.length-1; k>=0; j++,k--){
result[i][j]=matrix[k][i];
}
}
return result;
}
0

Commented:
mmmm tetris... fun stuff.... I really don't have a whole lot to add to this conversation, other then if mr_egyptian's code doesn't work you will want to google for some tetris like game source code and see how they rotate the pieces because that's really the same action.
0

Commented:
Forced accept.

Computer101