flash on 2011-5-17
...
@author ton
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Matrix;
/**
* ...
* @author ton
*/
public class Main extends Sprite {
public function Main():void {
if (stage)
init();
else
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
var mc:Sprite = new Sprite();
mc.graphics.beginFill(0xff0000);
mc.graphics.drawRect(0, 0, 100, 100);
mc.graphics.endFill();
mc.x = mc.y = 100;
addChild(mc);
var tx:Number = mc.x;
var ty:Number = mc.y;
var rotate:Number = Math.PI / 4;
var mat1:Matrix = new Matrix(1, 0, 0, 1, mc.x, mc.y);
var mat2:Matrix = new Matrix(1, 0, 0, 1, mc.x + mc.width, mc.y);
var mat3:Matrix = new Matrix(1, 0, 0, 1, mc.x, mc.y + mc.height);
var mat4:Matrix = new Matrix(1, 0, 0, 1, mc.x + mc.width, mc.y + mc.height);
mat1 = rotateMat(mat1, tx, ty, rotate);
trace(mat1);
mat2 = rotateMat(mat2, tx, ty, rotate);
trace(mat2);
mat3 = rotateMat(mat3, tx, ty, rotate);
trace(mat3);
mat4 = rotateMat(mat4, tx, ty, rotate);
trace(mat4);
mc.transform.matrix = mat1;
}
private function rotateMat(mat:Matrix, tx:Number, ty:Number, rotate:Number):Matrix {
var mat:Matrix = mat.clone();
mat = translatePost( -tx, -ty, mat);
mat = rotatePost(rotate, mat);
mat = translatePost(tx, ty, mat);
return mat;
}
private function translatePost(tx:Number, ty:Number, mat:Matrix):Matrix {
return new Matrix(mat.a, mat.b, mat.c, mat.d, mat.tx + tx, mat.ty + ty);
}
private function rotatePost(rotateRad:Number, mat:Matrix):Matrix {
var cos:Number = Math.cos(rotateRad);
var sin:Number = Math.sin(rotateRad);
return new Matrix(mat.a * cos + mat.b * -sin, mat.a * sin + mat.b * cos, mat.c * cos + mat.d * -sin,
mat.c * sin + mat.d * cos, mat.tx * cos + mat.ty * -sin, mat.tx * sin + mat.ty * cos);
}
}
}