AS3: Get Matrix Object Scale

Most often asked questions about how to scale DisplayObject, and usually, as a rule, a matrix is ​​used.

My question is: how do you get the scale of the matrix (scaleX and scaleY)?

There is a Matrix.scale method for setting scaleX and scaleY, but it does not return a value, and there are no other properties to read it.

The reason why I ask, I use an object that is laid out deep in the display list, and each of them can be converted. Therefore, I am using the sprite.transform.concatenatedMatrix getter child object, but at this point I am stuck on how to read the scale from it.

Any math visas in the house?

+1
source share
3 answers

Typically, a reliable way to isolate the scaling component in a matrix is ​​to use the matrix in question to transform unit vectors along the axes and then measure the length of the resulting vectors.

For example, given transformfrom DisplayObject and using Matrix3D, it scaleXwill be obtained as follows:

transform.matrix3D.deltaTransformVector(Vector3D.X_AXIS).length

Or, if you are using a concatenated 2D matrix, it scaleYwill look like this:

transform.concatenatedMatrix.deltaTransformPoint(new Point(0,1)).length

Note that functions deltaTransform*ignore matrix translation effects that do not affect scaling.

+3
source

you have access to the objects of the matrix object aand d, which are the scaling of the x axis and y axis, respectively:

package
{
//Imports
import flash.display.Sprite;
import flash.geom.Matrix;
import flash.display.Shape;

//Class
public class Test extends Sprite
    {
    //Constructor
    public function Test()
        {       
        var sh:Shape = new Shape();
        sh.graphics.beginFill(0xFF0000, 1.0);
        sh.graphics.drawRect(0, 0, 100, 100);
        sh.graphics.endFill();

        var scaleMatrix:Matrix = new Matrix();
        scaleMatrix.scale(4, 6);

        sh.transform.matrix = scaleMatrix;

        addChild(sh);

        trace("Matrix X Scale: " + scaleMatrix.a, "\nMatrix Y Scale: " + scaleMatrix.d);
        }
    }
}

// Matrix X Scale: 4 
// Matrix Y Scale: 6
0

x y .

:

public static function getScaleX(m:Matrix):Number
{
    return Math.sqrt(Math.pow(m.a + m.b, 2));
}

public static function getScaleY(m:Matrix):Number
{
    return Math.sqrt(Math.pow(m.c + m.d, 2));
}

:

, A B C D , x y . A, B - x ( 1, 0, ), C, D - y ( 0, 1).

, x 2, A, B 2, 0. ( 2 ).

, 90 , A, B 0, 1 ( x y), C, D -1, 0 ( y x).

x - . , , . A, B 0, 1, 1. 90 , 0, 0 A, B : sqrt (a ^ 2 + b ^ 2) = c. , .

, -.

0

All Articles