Solved

2d array rotation

Posted on 2011-03-23
3
3,831 Views
Last Modified: 2012-05-11
how to rotate a 2d array (matrix) 90 degrees anticlockwise so:

X =
    1    2    3
    4    5    6
    7    8    9

becomes

Y =
    3    6    9
    2    5    8
    1    4    7
0
Comment
Question by:chudyksg
3 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 35198684
create another empty array of 3x3 dimension, and follow these steps

pick value at 1,1 and put it at 3,1
pick value at 1,2 and put it at 2,1
pick value at 1,3 and put it at 1,1
pick value at 2,1 and put it at 3,2
pick value at 2,3 and put it at 1,2
pick value at 3,1 and put it at 3,3
pick value at 3,2 and put it at 2,3
pick value at 3,3 and put it at 1,3
0
 
LVL 27

Accepted Solution

by:
rrz earned 500 total points
ID: 35200226
from
http://stackoverflow.com/questions/42519/how-do-you-rotate-a-two-dimensional-array
public int[][] rotateMatrixLeft(int[][] matrix)
{
    /* W and H are already swapped */
    int w = matrix.length;
    int h = matrix[0].length;   
    int[][] ret = new int[h][w];
    for (int i = 0; i < h; ++i) {
        for (int j = 0; j < w; ++j) {
            ret[i][j] = matrix[j][h - i - 1];
        }
    }
    return ret;
}

Open in new window

0
 

Expert Comment

by:sinsach
ID: 35205990
This will rotate any matrix 90 degrees anti-clock.

/**
* To rotate the array 90 degrees anti-clock wise. This will rotate square
* and non square matrix.
* 
* @param sourceArray
* @return
*/
public int[][] antiClock90Rotation(int[][] sourceArray) {
	int[][] rotatedArray = new int[sourceArray[0].length][sourceArray.length];
	int positionFactor = rotatedArray.length - 1;
	for (int i = 0; i < rotatedArray.length; i++) {
		for (int j = 0; j < rotatedArray[i].length; j++) {
			int a = i - positionFactor;
			a = (a < 0) ? -a : a;
			rotatedArray[i][j] = sourceArray[j][a];
		}
	}
	return rotatedArray;
}

Open in new window

0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Windows 10 IE Certificate Issue 10 45
web application structure 18 76
SHA2 certs for IIS AND Java? 2 80
how to install java on RHEL image on EC2 4 26
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

786 members asked questions and received personalized solutions in the past 7 days.

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

Join & Ask a Question