| Refresh | Home EGTry.com

composition of a series matrix transformation


concat matrix concat matrix

ConcatMatrix.as

package {
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.geom.Point;
		
	//rotate and scale around a point other than the orgin
	//step 1. translate. move point pc at the origin.
	//step 2. rotate/scale.
	//step 3. move origin at the pc
	
	public class ConcatMatrix extends Sprite
	{

		public function ConcatMatrix()
		{
			
			
			var p1:Point=new Point(10, 50);
			var p2:Point=new Point(200,100);
			var pc:Point=new Point( (p1.x+p2.x)/2, (p1.y+p2.y)/2 );
			
			drawRect(p1, p2, 0.1);
			
			var shape:Shape=drawRect(p1, p2, 1);
			var m:Matrix=new Matrix;
			m.identity();
			
			m.translate(-pc.x, -pc.y);
			
			m.rotate(90*Math.PI/180); 
			
			m.scale(1,0.5);
			
			m.translate(pc.x,pc.y);
			trace("final matrix: "+m);//(a=0, b=0.5, c=-1, d=0, tx=180, ty=22.5)

			shape.transform.matrix=m;
			
		}
		
		public function drawRect(p1:Point, p2:Point, alpha:Number):Shape {
			var shape:Shape=new Shape();
			var g:Graphics=shape.graphics;
			g.lineStyle(1, 0x000000, alpha);
			g.drawRect(p1.x, p1.y, Math.abs(p2.x-p1.x), Math.abs(p2.y-p1.y));
			addChild(shape);
			return shape;
		}
		
	}
}