Jump to content

Rotating Matrix - Java

bobhays

Hey everyone, looking some help with this method in a program.

I have a global square matrix called grid, and int constant GRID_SiZE (length of 1 side, 4 in this case.)

 

public void rotate(boolean rotateClockwise) {
        int[][] turned = new int[GRID_SIZE][GRID_SIZE];
        for(int i = 0; i < GRID_SIZE; i++)
        {
                for(int j = 0; j < GRID_SIZE; j++)
                {
                        if(rotateClockwise)
                        {
                                int k = (GRID_SIZE - 1) - i;
                                turned[j][k] = grid[i][j];
                        }
                        else
                        {
                                int k = (GRID_SIZE - 1) - j;
                                turned[k][i] = grid[i][j];
                        }
                }
        }
        grid = turned;
    }

 

 

If rotateClockwise is false I'm supposed to rotate counter-clockwise.

I'm not getting what i'm supposed to. Can anyone help me figure out what's wrong?

The issue seems pretty similar if i rotate clockwise or counter, so I think the same problem applies to both.

Link to comment
Share on other sites

Link to post
Share on other sites

Can you give a sample input, expected output, and actual output.

I ran your method with this input:

 1  2  3  4 
 5  6  7  8 
 9 10 11 12 
13 14 15 16 

And got this output:

 4  8 12 16 
 3  7 11 15 
 2  6 10 14 
 1  5  9 13 

 

Is this not correct?

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, PlutoNZL said:

Can you give a sample input, expected output, and actual output.

I ran your method with this input:

 1  2  3  4 
 5  6  7  8 
 9 10 11 12 
13 14 15 16 

And got this output:

 4  8 12 16 
 3  7 11 15 
 2  6 10 14 
 1  5  9 13 

 

Is this not correct?

I realized there was an error in another part of my code that is affecting the output. I'm still not sure what the error is or how it's changing my output, but that is the issue not this method. Thank you.

Link to comment
Share on other sites

Link to post
Share on other sites

47 minutes ago, PlutoNZL said:

snip

Now what I'm trying to do is ready a text file with the following format.

4
3456789
0 0 0 2
0 0 0 1
0 0 3 3
0 2 0 1

I have to set the first line to the GRID_SIZE, the second line to the score, and then add the rest to a matrix. How would I go about doing this? I'm trying to use buffered reader but I dont know where it leaves off since I cant make the matrix and set the values in one loop. I was thinking of using scanner instead but not sure how to do it.

 

EDIT: got it.

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, prolemur said:

Not useful here.

 

A rotation matrix is used to rotate points in Euclidean space around an axis. To perform this rotation, you multiply the matrix with the column vector of each point. Something like this :

JKTrdIl.png  (rotation around the x axis)

Which is an ok way of handling rotations in Euclidean space, but it also introduces Gimbal lock when rotating along multiple axis(first x then y for example). A matrix can be generated to rotate around an arbitrary axis to prevent most Gimbal lock. For the arbitrary rotation axis {Rx,Ry,Rz}, this matrix would be :

bGqLijR.png

It doesn't prevent Gimbal lock entirely though; to do that we could use quaternions, which are a whole other topic.

 

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Nineshadow said:

Not useful here.

 

A rotation matrix is used to rotate points in Euclidean space around an axis. To perform this rotation, you multiply the matrix with the column vector of each point. Something like this :

JKTrdIl.png  (rotation around the x axis)

Which is an ok way of handling rotations in Euclidean space, but it also introduces Gimbal lock when rotating along multiple axis(first x then y for example). A matrix can be generated to rotate around an arbitrary axis to prevent most Gimbal lock. For the arbitrary rotation axis {Rx,Ry,Rz}, this matrix would be :

bGqLijR.png

It doesn't prevent Gimbal lock entirely though; to do that we could use quaternions, which are a whole other topic.

 

My reply was kind of a joke, but damn. You out here dropping them knowledge bombs :D

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Nineshadow said:

to do that we could use quaternions, which are a whole other topic.

A topic which can be summed up in 1 word. Magic.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

20 hours ago, fizzlesticks said:

A topic which can be summed up in 1 word. Magic.

4-dimensional magic.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×