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

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

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
Asked:
Lo-Tan
  • 2
  • 2
  • 2
  • +3
2 Solutions
 
CEHJCommented:
>>JavaScript

You're in the Java TA ...
0
 
gatorvipCommented:
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
 
Lo-TanAuthor 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
gatorvipCommented:
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
 
CEHJCommented:
>>
[CEHJ:  You're in the Java TA ...]
What's that?  Topic Announcement??
>>

Topic Area
0
 
Lo-TanAuthor 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
 
mr_egyptianCommented:
Singular Value Decomposition(SVD) sounds like something you might be interested in, though it's a bit beyond me.

0
 
mr_egyptianCommented:
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
 
IsisagateCommented:
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
 
Computer101Commented:
Forced accept.

Computer101
EE Admin
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now