AS3:获取 Matrix 对象的比例
最常见的问题是如何缩放 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通常,隔离矩阵中缩放分量的可靠方法是使用相关矩阵沿轴变换单位向量,然后测量所得向量的长度。
例如,给定 DisplayObject 的
transform
并使用 Matrix3D,将按如下方式获得scaleX
:或者,如果您使用串联的 2D Matrix,则
>scaleY
将是:请注意,
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, thescaleX
would be obtained as follows:Or, if you use the concatenated 2D Matrix, the
scaleY
would be:Note that the
deltaTransform*
functions ignore the translation effects of the matrices, which have no effect on the scaling.即使矩阵旋转,您也可以获得其 x 和 y 比例。
代码如下:
说明:
我发现将 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, 0
到A, 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:
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 as1, 0
, which will not transform), andC, D
is the position of the first point of the transformed y axis (identity values of0, 1
).If we have a matrix that will scale the x axis by 2 then
A, B
will be2, 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 be0, 1
(pointing the x axis along the positive side of the y axis) andC, 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
is0, 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 from0, 0
toA, B
by using the Pythagorean theorem: sqrt(a^2 + b^2) = c. This is what my code is doing.Hope this helps someone.
您可以访问矩阵对象的
a
和d
公共属性,它们分别表示 x 轴和 y 轴的缩放:you have access to the matrix object's
a
andd
public properties, which represent the scaling of the x-axis and y-axis respectively: