Quantunet.com

My Account

Joins Us
Flash 8 Actionscript 2.0 Knowledgebase
The Scaling Matrix  

A The Scaling matrix operator can vertically or horizontally scale a point or set of points. When the scale operator is multiplied by a vector the x and y coordinates are multiplied by a scaling constant which has the effect of stretching or shrinking the vector along the x or y axis.

As we have seen in the identity matrix multiplying a vector by a unit matrix has no effect on the vector coordinates.

unitmatrix

The x and y coordinates are unchanged by the multiplication.
xyz unit matrix
However if we modify the first element in the identity matrix to equal a value Sx. Following the same rules of matrix multiplication the x coordinates of the vector is no longer multiplied by one by instead it is multiplied by the factor Sx.

Note: The place holder value "1" in the 2d row vector has been put there to satisfy the condition that the numbers of columns of the first matrix must equal the number of rows of the second matrix in a matrix multiplication. see [Matrix Multiplication Knowledgebase].

horizontal scaling matrix

If the term Sx had a value of 2 the value of the x coordinates would increase by a factor of two. This means that vector would position twice as far along the x axis. The value of Sx can be replaced by any number this value will in turn scale the horizontal of the vector accordingly.

horizontally scale transformation matrix
The same effect can be achieved for the vertical coordiante of the vector by modifying the second element of the second row of the unit matrix.

vertical scaling matrix

The constant Sy can be used to scae the y axis coordinate of the vector.

vertical scale transformation matrix

Both methods can be employed at once to create a horizontal and vertical scaling effect. e.g.

scaling matrix

vertical and horizontal scaling transformation matrix multiplication

The effect is hard to imagine for one point but become clear to see when you have multiple points such as a triangle or square. Each point is treated in the same way. The constants Sx and Sy act on the x and y values independently to create a combined scaling effect.

vertical and horizontal scaling of multiple 2d points multiplication

In the matrix multiplication above the x and y coordinates of three pointsare vertically and horizontally scaled by the 2d scale matrix. I have ignored the z coordinates in this example but have kept a unit value (1) where the value would normally be so that you can see how the 3x3 scale matrix operates on the 2d points (as the number of columns must equal the number of rows).


To learn more about matrices in flash see the [Matrices Knowledgebase].

Matrix Scale Using Actionscript (movie clips)

Method I: Modified Tranformation matrix

This example uses the transform matrix class to dynamically scale a movicip in the x and y direction.

import flash.geom.Matrix;
var xscale:Number = 0;
var yscale:Number = 0;

onEnterFrame = function{
xscale +=0.05;
yscale +=0.07;

my_mc.transform.matrix = new Matrix(xscale,0,0,yscale,0);
}

Note: Although the transform matrix is a 3x3 matrix the values of u,v and w cannot be accessed by the matrix transform class. For more information about the transform class see [Transform Matrix Knowledgebase].

Method II: Using a specific matrix transformation "method"
In this example the movie clip "my_mc" is dynamically scaled using the scale method of the matrix transform class. Each time the movie clipo enters a new frame a value of 0.05 is added to x scale and 0.07 added to yscale. This causes the movie clip to horizontally and vertically inscrease in size getting larger and larger as the movie plays.

import flash.geom.Matrix;
var xscale:Number = 0;
var yscale:Number = 0;

onEnterFrame = function{
xscale +=0.05;
yscale+=0.07;

my_matrix = new Matrix()
my_mc.my_matrix.scale(xscale,yscale);
}

Matrix Scale Using Actionscript (4 2d points)
In this example four 2d points have been horizontsally and vertically scaled
using a custom made scale matrix operator "xy_scale_matrix" and a matrix multiplication function "matrix_mult".

var xscale = 10;
var yscale = 15;

my_point_matrix = new Array (20,150, 80,60, 95,130, 220,110);
xy_scale_matrix = new Array (xscale,0, 0,yscale);

matrix_mult (my_point_matrix, xy_scale_matrix);


matrix_mult (matrixA:Array, martrixB:Array):Array {
var result_coords:Array = new Array();

result_coords[0] = matrixA[0]*martrixB[0] + matrixA[1]*martrixB[3];
result_coords[1] = matrixA[0]*martrixB[1] + matrixA[1]*martrixB[4];

result_coords[2] = matrixA[2]*martrixB[0] + matrixA[3]*martrixB[3];
result_coords[3] = matrixA[2]*martrixB[1] + matrixA[3]*martrixB[4];

result_coords[4] = matrixA[4]*martrixB[0] + matrixA[5]*martrixB[3];
result_coords[5] = matrixA[4]*martrixB[1] + matrixA[5]*martrixB[4];

result_coords[6] = matrixA[6]*martrixB[0] + matrixA[7]*martrixB[3];
result_coords[7] = matrixA[6]*martrixB[1] + matrixA[7]*martrixB[4];

return result_coords;
}

To expand this scale matrix multiplication to use a set of 3d points see [3D Scale Matrix Knowledgebase].


© 2008 Quantunet LLC All Rights Reserved | Intellectual Property | Terms of Use | Privacy
Home | About Quantunet | FAQ's | Contact Us