Results 1 to 5 of 5
Thread: Rotate 2D array 90 degrees ?
-
October 15th, 2007, 11:50 AM #123Registered User
postsRotate 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?
-
October 15th, 2007, 12:30 PM #2
Instead of reading the array the same way and rotating the array, I recommend just change the way you look at the array. ^^
http://darylteo.com/blog
-
October 15th, 2007, 02:48 PM #3
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.
-
October 15th, 2007, 04:55 PM #4
^ 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 for..in that was SLOW. While a regular for loop can get through 5 million objects in a couple seconds, it took over a minute with for..in.I love this place, my Slurpee is so Green. - FIF
-
October 15th, 2007, 06:17 PM #5
This should work if you want to rotate a non-jagged, rectangular array 90 degrees clockwise:
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.Code:var myArray:Array = [[ 1 , 2 , 3 , 4 , 5 ], [ 6 , 7 , 8 , 9 ,'a'], ['b','c','d','e','f']]; trace2DArray(rotate2DArray(myArray)); /* [[b,6,1], [c,7,2], [d,8,3], [e,9,4], [f,a,5]] */ // 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 + ']'); }AS4 expert.

Reply With Quote


Bookmarks