The forums have permanently moved to This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.

Results 1 to 5 of 5

Thread: Rotate 2D array 90 degrees ?

  1. #1

    Rotate 2D array 90 degrees ?

    I am building tile based application which stores it's objects in 2D array. I would need to rotate it 90 degrees to change angle of looking at it.
    Any idea of how to do this?

  2. #2
    Instead of reading the array the same way and rotating the array, I recommend just change the way you look at the array. ^^

  3. #3
    Quote Originally Posted by Dazzer View Post
    Instead of reading the array the same way and rotating the array, I recommend just change the way you look at the array. ^^
    That'll only work with relatively small Arrays. If the Array is large, iterating in reverse (incrementing j, then i) increases the number of misses in the memory cache. See, programs working with lots of data will try to cache spatially associated data (so if you access a[i][0], Flash will probably cache a[i][1] somewhere). If you access data in the order that it's spatially associated, the cache will most often help. But if you access it the wrong way, the cache can slow you down. For instance, accessing a[0][i], then a[1][i], then a[2][i] will be slow if a[0], a[1] and a[2] are Objects that are larger than the cache.

  4. #4
    BradLee's Avatar
    Formerly TheSpork
    ^ I haven't been able to reproduce that. Actionscript's arrays aren't true arrays, they are some kinda associative array / hash table thing. I tested with accessing up to 10 million objects in an array. Iterating backwards was only a few milliseconds slower than forwards.

    Now using that was SLOW. While a regular for loop can get through 5 million objects in a couple seconds, it took over a minute with

  5. #5
    This should work if you want to rotate a non-jagged, rectangular array 90 degrees clockwise:
    var myArray:Array = [[ 1 , 2 , 3 , 4 , 5 ],
                         [ 6 , 7 , 8 , 9 ,'a'],
    // assumes a non-jagged array, not optimized
    function rotate2DArray(array:Array):Array {
    	var d1 = array.length;
    	var d2 = array[0].length;
    	var r:Array = new Array(d2);
    	for(var i:int = 0; i < d2; i++){
    		var t:Array = new Array(d1);
    		for(var j:int = 0; j < d1; j++){
    			t[j] = array[d1 - 1 - j][i];
    		r[i] = t;
    	return r;
    function trace2DArray(array:Array):void {
    	var r:String = '[';
    	for(var i:int = 0; i < array.length; i++){
    		if(i != 0){
    			r += ' ';
    		r += '[' + array[i].join(',') + ']';
    		if(i != array.length - 1){
    			r += ',\n';
    	trace(r + ']');
    The original array can't be modified in place when rotating a not-necessarily-square 2D array. The function should be similar if you want to rotate counterclockwise instead.
    “Who were you, Krilnon, and how did you know so much about AS4?”
    The historian sighed as she gazed up at the sky and saw… not stars. A story.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Home About Meet the Moderators Advertise

 Link to Us


Copyright 1999 - 2012