|
|
|||||
Поворот матрицы
Имеется матрица n * m.
Требуется, взависимости от полученного значения, в диапозоне [0 ; 4) развернуть матрицу. то есть: 0 1 2 3 Решений в данном случае достаточно много, но все, найденые мной, либо громоздкие, либо не производительные. Хотелось бы какое нибудь комплексное, элегантное решение. З.Ы. матрица может в любой n-ный момент быть любого из перечисленныхь видов. Размерность матрицы неограничена и не обязательно является равносторонней (квадратом).
__________________
ЛОДКИ |
|
|||||
Modus ponens
|
А как задается матрица изначально? Вложенные массивы? Один массив упакованный каким-нибудь интересным способом например:
или И их есть еще. В зависимости от подхода, естесственно, могут быть разные решения. http://stackoverflow.com/questions/4...ensional-array кстати, тут есть интересное решение (в третьем ответе), где задается класс матрицы а при вращении меняется коеффициент в зависимости от которог индексы через которые идет обращение к элементам матрицы преобразуются к нужному виду, что делает вращение O(1) операцией вообще
__________________
Hell is the possibility of sanity Последний раз редактировалось wvxvw; 21.07.2012 в 14:27. |
|
|||||
Modus ponens
|
package { public class Matrix { public static const DEGREES_0:int = 0; public static const DEGREES_90:int = 1; public static const DEGREES_180:int = 2; public static const DEGREES_270:int = 3; private const _source:Array = []; private var _direction:int; private var _width:int; private var _height:int; public function Matrix(width:int, height:int) { super(); this._width = width; this._height = height; this._source.length = width * height; } public function get(x:int, y:int):Object { var result:Object; this.assertOutOfBounds(x, y); switch (this._direction) { case 0: result = this._source[y * this._width + x]; break; case 1: result = this._source[(this._width - x - 1) * this._width + (this._height - y - 1)]; break; case 2: result = this._source[(this._height - y - 1) * this._width + (this._width - x - 1)]; break; case 3: result = this._source[x * this._width + y]; break; } return result; } public function set(x:int, y:int, value:Object):Object { var result:Object; this.assertOutOfBounds(x, y); switch (this._direction) { case 0: result = this._source[y * this._width + x] = value; break; case 1: result = this._source[(this._width - x - 1) * this._width + (this._height - y - 1)] = value; break; case 2: result = this._source[(this._height - y - 1) * this._width + (this._width - x - 1)] = value; break; case 3: result = this._source[x * this._width + y] = value; break; } return value; } public function rotate(rotation:int):void { if (rotation < 0 || rotation > 3) throw new ArgumentError("Invalid rotation"); this._direction = rotation; if (rotation & 1) { this._width = this._height ^ this._width; this._height = this._height ^ this._width; this._width = this._height ^ this._width; } } public function toString():String { var result:String = "["; for (var i:int; i < this._height; i++) { result += "["; for (var j:int = 0; j < this._width; j++) result += this.get(j, i) + ", "; if (this._width) result = result.substr(0, result.length - 2) + "],\n"; } if (this._height) result = result.substr(0, result.length - 2); return result + "]"; } private function assertOutOfBounds(x:int, y:int):void { if (this._width < x || this._height < y) throw new ArgumentError("Index out of bounds"); } } } package { import flash.display.Sprite; public class TestMatrix extends Sprite { public function TestMatrix() { super(); this.test(); } private function test():void { var matrix:Matrix = new Matrix(3, 3); matrix.set(0, 0, 4); matrix.set(1, 0, 0); matrix.set(2, 0, 4); matrix.set(0, 1, 4); matrix.set(1, 1, 5); matrix.set(2, 1, 6); matrix.set(0, 2, 0); matrix.set(1, 2, 4); matrix.set(2, 2, 0); trace(matrix); matrix.rotate(Matrix.DEGREES_90); trace(matrix); matrix.rotate(Matrix.DEGREES_180); trace(matrix); matrix.rotate(Matrix.DEGREES_270); trace(matrix); matrix.rotate(Matrix.DEGREES_0); trace(matrix); } } }
__________________
Hell is the possibility of sanity |
Часовой пояс GMT +4, время: 11:01. |
|
« Предыдущая тема | Следующая тема » |
Опции темы | |
Опции просмотра | |
|
|