# The Matrix Class

### The Matrix Class

The Matrix class allows you to perform a transformation by manipulating coordinates.

It is important when using this class to import it first at the beginning of your script, like this:

` import flash.geom.Matrix; `

To instantiate a new instance of the Matrix class, use this code as a template:

` var myMatrix:Matrix = new Matrix(a,b,c,d,tx,ty); `

• a Numerical value of the first row, first column.

• b Numerical value of the first row, second column.

• c Numerical value of the second row, first column.

• d Numerical value of the second row, second column.

• tx Numerical value of the third row, first column.

• ty Numerical value of the third row, second column.

Example:

This example will create an instance of the Matrix class and send it to the Output panel:

` import flash.geom.Matrix; var myMatrix:Matrix = new Matrix(1,2,3,4,5,6); trace(myMatrix); //output: (a=1, b=2, c=3, d=4, tx=5, ty=6) `

Because all the properties of this object match the parameters identically, they will be skipped.

#### Methods

##### clone

Availability: FP:8, AS:1.0

Generic Template: myMatrix.clone();

Returns: Matrix

This method will return an exact duplicate of the Matrix object it is being called on.

Description:

This method will create an exact copy of the Matrix object it is being called on.

Example:

This example will create a Matrix object, create a copy, and output that copy:

` import flash.geom.Matrix; var myMatrix:Matrix = new Matrix(1,2,3,4,5,6); var myMatrix2:Matrix = myMatrix.clone(); trace(myMatrix2); //output: (a=1, b=2, c=3, d=4, tx=5, ty=6) `

##### concat

Availability: FP:8, AS:1.0

Generic Template: myMatrix.concat(matrix);

Parameters:

• matrix Another Matrix object concatenated to the one calling the method.

Description:

This method will combine one matrix with another. This may seem like the values will be added, but instead a matrix multiplication will occur.

Example:

This example will create two Matrix objects and combine them:

` import flash.geom.Matrix; var myMatrix:Matrix = new Matrix(1,2,3,4,5,6); var myMatrix2:Matrix = myMatrix.clone(); myMatrix.concat(myMatrix2); trace(myMatrix); //output: (a=7, b=10, c=15, d=22, tx=28, ty=40) `

##### createBox

Availability: FP:8, AS:1.0

Generic Template: myMatrix.createBox(scaleX, scaleY, rot, x, y);

Parameters:

• scaleX Horizontal scaling value.

• scaleY Vertical scaling value.

• rot A numerical value to rotate the Matrix by, in radians. Default value of 0.

• x The amount in pixels of horizontal movement. Default value of 0.

• y The amount in pixels of vertical movement. Default value of 0.

Description:

This method will transform the matrix values based on given information such as scaling and rotation. This one method will accomplish in a single task what it would take several other of the Matrix methods to accomplish in a row.

Example:

This example will create a Matrix object, use the createBox method to transform it, and then output the results:

`[View full width] import flash.geom.Matrix; var myMatrix:Matrix = new Matrix(1,2,3,4,5,6); myMatrix.createBox(2, 2, 45, 10, 10); trace(myMatrix); //output: (a=1.05064397763546, b=1.70180704906824, c=-1.70180704906824, d=1.05064397763546 , tx=10, ty=10) `

Availability: FP:8, AS:1.0

Generic Template: myMatrix.createGradientMatrix(w, h, rot, x, y);

Parameters:

• w Width of the gradient box.

• h Height of the gradient box.

• rot A numerical value to rotate the Matrix by, in radians. Default value of 0.

• x The amount in pixels of horizontal movement. Default value of 0. It will be offset by half of w. Default value of 0.

• y The amount in pixels of vertical movement. Default value of 0. It will be offset by half of h. Default value of 0.

Description:

This method will create a very specific type of matrix, the gradient matrix.

Example:

This example will use the createGradientMatrix method to create the gradient for a square that will be drawn with the drawing API:

` import flash.geom.Matrix; //create the arrays we need var colors:Array = [0x397dce, 0x000033]; var alphas:Array = [75, 100]; var ratios:Array = [0, 255]; //create the matrix var myMatrix:Matrix = new Matrix(); //create the gradient myMatrix.createGradientBox(200, 200, 0, 50, 50); //draw the square this.beginGradientFill("linear", colors, alphas, ratios, myMatrix); this.lineTo(0, 300); this.lineTo(300, 300); this.lineTo(300, 0); this.lineTo(0, 0); this.endFill(); `

##### deltaTransformPoint

Availability: FP:8, AS:1.0

Generic Template: myMatrix.deltaTransformPoint(point);

Parameters:

• point A Point object.

Returns: Point

A new Point object.

Description:

This method will create a new Point object based on the transformation. This method differs from the transformPoint method in that it does not use tx or ty when the transformation occurs.

Example:

This example will use the deltaTransformPoint to transform a point from one coordinate to another:

` import flash.geom.Matrix; import flash.geom.Point; //create the point var point:Point = new Point(25, 15); //create the matrix var myMatrix:Matrix = new Matrix(1,2,3,4,5,6); //trace the point after delta transformation trace(myMatrix.deltaTransformPoint(point)); //output: (x=70, y=110) `

##### identity

Availability: FP:8, AS:1.0

Generic Template: myMatrix.identity();

Description:

This method will cause the matrix calling it to revert back to a default matrix.

Example:

This example will create a defined matrix and send its information to the Output panel. Then it will call the identity method and send the information again:

` import flash.geom.Matrix; var myMatrix:Matrix = new Matrix(4,5,6,7,8,9); trace(myMatrix); myMatrix.identity(); trace(myMatrix); //output:(a=4, b=5, c=6, d=7, tx=8, ty=9) //(a=1, b=0, c=0, d=1, tx=0, ty=0) `

##### invert

Availability: FP:8, AS:1.0

Generic Template: myMatrix.invert();

Description:

This method will completely reverse the matrix if, for instance, you want to undo a transformation.

Example:

This example will create a defined matrix and send its information to the Output panel. Then it will call the invert method and send the information again:

` import flash.geom.Matrix; var myMatrix:Matrix = new Matrix(1,2,3,4,5,6); trace(myMatrix); myMatrix.invert(); trace(myMatrix); //output:(a=1, b=2, c=3, d=4, tx=5, ty=6) //(a=-2, b=1, c=1.5, d=-0.5, tx=1, ty=-2) `

##### rotate

Availability: FP:8, AS:1.0

Generic Template: myMatrix.rotate(rot);

Parameters:

• rot The amount to rotate the matrix by, in radians.

Description:

This method will alter the a and d properties of the matrix so it can be used to apply rotation.

Example:

This example will create a defined matrix and send its information to the Output panel. Then it will be rotated 45 degrees and sent back to the Output panel:

`[View full width] import flash.geom.Matrix; var myMatrix:Matrix = new Matrix(1,2,3,4,5,6); trace(myMatrix); myMatrix.rotate(45); trace(myMatrix); //output:(a=1, b=2, c=3, d=4, tx=5, ty=6) //(a=-1.17648506025051, b=1.90154750216958, c=-1.82764813168328, d=4.65399852887327, tx=-2 .47881120311606, ty=7.40644955557697) `

##### scale

Availability: FP:8, AS:1.0

Generic Template: myMatrix.scale(x,y);

Parameters:

• x The horizontal scaling amount.

• y The vertical scaling amount.

Description:

This method will alter the a and d properties of the matrix so it can be used to apply scaling.

Example:

This example will create a defined matrix and send its information to the Output panel. Then it will be scaled and sent back to the Output panel:

` import flash.geom.Matrix; var myMatrix:Matrix = new Matrix(); trace(myMatrix); myMatrix.scale(2,2); trace(myMatrix); //output:(a=1, b=0, c=0, d=1, tx=0, ty=0) //(a=2, b=0, c=0, d=2, tx=0, ty=0) `

##### toString

Availability: FP:8, AS:1.0

Generic Template: myMatrix.toString();

Returns: String

The string will contain all the properties of the matrix.

Description:

This method will return a string with every property of the matrix fully defined.

Example:

This example will create a defined matrix and send the output of the toString method to the Output panel:

` import flash.geom.Matrix; var myMatrix:Matrix = new Matrix(); trace(myMatrix.toString()); //output:(a=1, b=0, c=0, d=1, tx=0, ty=0) `

##### TRansformPoint

Availability: FP:8, AS:1.0

Generic Template: myMatrix.transformPoint(point);

Parameters:

• point The Point object that will be transformed.

Returns: Point

A new Point object.

Description:

This method will apply a transformation on the defined point based on the given matrix.

Example:

This example will create a Point at (10,10) and run it with the matrix using the TRansformPoint method:

` import flash.geom.Matrix; import flash.geom.Point; var point:Point = new Point(10,10); var myMatrix:Matrix = new Matrix(1,1,1,1,1,1); trace(myMatrix.transformPoint(point)); //output:(x=21, y=21) `

##### translate

Availability: FP:8, AS:1.0

Generic Template: myMatrix.translate(x,y);

Parameters:

• x The amount of horizontal movement.

• y The amount of vertical movement.

Description:

This method will modify a matrix by adjusting it along the horizontal and vertical axes.

Example:

This example will create a default matrix and adjust it by (10,10):

` import flash.geom.Matrix; var myMatrix:Matrix = new Matrix(); trace(myMatrix); myMatrix.translate(10,10); trace(myMatrix); //output:(a=1, b=0, c=0, d=1, tx=0, ty=0) //(a=1, b=0, c=0, d=1, tx=10, ty=10) `

Macromedia Flash Professional 8 Unleashed
ISBN: 0672327619
EAN: 2147483647
Year: 2005
Pages: 319