Код AS3:
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");
}
}
}
Код AS3:
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);
}
}
}
Вроде бы правильно, на первый взгляд :) Но, смотрите, зависит от того, что нужно чаще делать, читать из матрицы или поворачивать. Но если существует возможность, что нужно будет несколько раз поворачивать подряд, не читая, то это как бы вообще крутое решение (опять же, заслуга не моя, а постера с СО).
|