AS3:获取 Matrix 对象的比例

发布于 2024-10-22 10:28:34 字数 291 浏览 4 评论 0原文

最常见的问题是如何缩放 DisplayObject,答案通常是使用 Matrix。

我的问题是,如何获取矩阵的比例(scaleX 和scaleY)?

有一个 Matrix.scale 方法来设置scaleX和scaleY,但它不返回值,并且不存在其他属性来读回它。

我问的原因是,我使用的对象深埋在显示列表中,并且每个对象都可以进行转换。因此,我使用子对象的 sprite.transform.concatenatedMatrix getter,但此时我陷入了如何从中读取比例的困境。

家里有数学奇才吗?

Most often, questions are asked about how to scale a DisplayObject, and the answer is usually to use a Matrix.

My question is, how to you GET the scale of a Matrix (scaleX and scaleY)?

There's a Matrix.scale method to set the scaleX and scaleY, but it doesn't return a value, and no other properties exist to read it back.

The reason I ask, I'm using object burried deep down into a Display list, and each may be transformed. So I use the child object's sprite.transform.concatenatedMatrix getter, but am stuck at this point on how to read the scale from it.

Any Math Wiz in the house?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

扛刀软妹 2024-10-29 10:28:34

通常,隔离矩阵中缩放分量的可靠​​方法是使用相关矩阵沿轴变换单位向量,然后测量所得向量的长度。

例如,给定 DisplayObject 的 transform 并使用 Matrix3D,将按如下方式获得 scaleX

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

或者,如果您使用串联的 2D Matrix,则 >scaleY 将是:

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

请注意,deltaTransform* 函数忽略矩阵的平移效果,这对缩放没有影响。

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

For instance, given the transform from a DisplayObject, and using the Matrix3D, the scaleX would be obtained as follows:

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

Or, if you use the concatenated 2D Matrix, the scaleY would be:

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

Note that the deltaTransform* functions ignore the translation effects of the matrices, which have no effect on the scaling.

倾城花音 2024-10-29 10:28:34

即使矩阵旋转,您也可以获得其 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));
}

说明:

我发现将 ABC D 视为定义变换后的坐标空间中的 x 轴和 y 轴的点更容易。 A, B 是变换后的 x 轴第一个点的位置(单位矩阵的值为 1, 0,不会变换),C, D 是变换后的 y 轴第一个点的位置(标识值为 0, 1)。

如果我们有一个将 x 轴缩放 2 的矩阵,则 A, B 将是 2, 0。 x 轴上的其余点与最后一个点的距离相同(因此与最后一个点的距离为 2 个点)。

如果我们有一个顺时针旋转 90 度的矩阵,则 A, B 将是 0, 1(将 x 轴指向 y 轴的正侧)并且 < code>C, D 将是 -1, 0 (将 y 轴指向 x 轴的负侧)。

x 轴的刻度是到第一个点的距离。在我提到的场景中,很容易找到比例尺。在前面的示例中,A, B 为 0, 1,因此比例为 1。如果旋转未以 90 度增量打开,那么您可以找到直线的长度使用毕达哥拉斯定理从 0, 0A, B 进行分段:sqrt(a^2 + b^2) = c。这就是我的代码正在做的事情。

希望这对某人有帮助。

You can get the x and y scales of a matrix even when it's rotated.

Here's the code:

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));
}

Explanation:

I've found that it's easier to think of A B C D as points that define the x and y axes in the transformed coordinate space. A, B is the position of the first point of the transformed x axis (an identity matrix has these as 1, 0, which will not transform), and C, D is the position of the first point of the transformed y axis (identity values of 0, 1).

If we have a matrix that will scale the x axis by 2 then A, B will be 2, 0. The rest of the points on x axis will be this same distance away from the last (so 2 points away from the last).

If we have a matrix that will rotate 90 degrees clockwise then A, B will be 0, 1 (pointing the x axis along the positive side of the y axis) and C, D will be -1, 0 (pointing the y axis down the negative side of the x axis).

The scale of the x axis is the distance to the first point. In the scenarios that I've mentioned the scale is easy to find. In the previous example A, B is 0, 1 so the scale is 1. If rotation is not on at a 90 degree increment then you can find the length of the line segment from 0, 0 to A, B by using the Pythagorean theorem: sqrt(a^2 + b^2) = c. This is what my code is doing.

Hope this helps someone.

意中人 2024-10-29 10:28:34

您可以访问矩阵对象的 ad 公共属性,它们分别表示 x 轴和 y 轴的缩放:

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

you have access to the matrix object's a and d public properties, which represent 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文