You're in the Java TA ...

Solved

Posted on 2007-08-07

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

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

10 Comments

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

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??

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

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

private static int[][] rotateLeft(int[][] matrix){

int[][] result=new int[matrix[0].length][matr

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][matr

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;

}

By clicking you are agreeing to Experts Exchange's Terms of Use.

Title | # Comments | Views | Activity |
---|---|---|---|

the registry refers to a nonexistent java runtime environment | 6 | 61 | |

Tools or ways to handle development of complex web applications | 4 | 49 | |

How Complex Is This Java Course ? | 9 | 51 | |

mapAB Challlenge | 35 | 46 |

This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**11** Experts available now in Live!