转换的矩阵表示形式Matrix Representation of Transformations

M × n 矩阵是一组按 m 行和 n 列排列的数字。An m×n matrix is a set of numbers arranged in m rows and n columns. 下图显示几个矩阵。The following illustration shows several matrices.

转换Transformations

您可以通过添加各个元素添加相同大小的两个矩阵。You can add two matrices of the same size by adding individual elements. 下图显示矩阵添加两个的示例。The following illustration shows two examples of matrix addition.

转换Transformations

M × n 矩阵可以乘以 n × p 矩阵,并且结果为 m × p 矩阵。An m×n matrix can be multiplied by an n×p matrix, and the result is an m×p matrix. 中的第一个矩阵的列数必须与第二个矩阵中的行数相同。The number of columns in the first matrix must be the same as the number of rows in the second matrix. 例如的 4 × 2 矩阵 2 的 × 3 矩阵来生成的 4 × 3 矩阵相乘。For example, a 4×2 matrix can be multiplied by a 2×3 matrix to produce a 4×3 matrix.

平面和行和列矩阵的中点可以看作向量。Points in the plane and rows and columns of a matrix can be thought of as vectors. 例如,(2,5) 是一个具有两个组件,向量和 (3,7,1) 是一个具有三个组件的向量。For example, (2, 5) is a vector with two components, and (3, 7, 1) is a vector with three components. 两个向量的点积,如下所示定义:The dot product of two vectors is defined as follows:

(a、 b) • (c,d) = ac + bd(a, b) • (c, d) = ac + bd

(a、 b、 c) • (d、 e、 f) 有 + = ad + cf(a, b, c) • (d, e, f) = ad + be + cf

例如,个的点积 (2、 3) 和 (5,4) 是 (2)(5) + (3)(4) = 22。For example, the dot product of (2, 3) and (5, 4) is (2)(5) + (3)(4) = 22. 点积 (2、 5、 1) 和 (4,3,1) 是 (2)(4) + (5)(3) + (1)(1) = 24。The dot product of (2, 5, 1) and (4, 3, 1) is (2)(4) + (5)(3) + (1)(1) = 24. 请注意两个向量的点积,是一个数字,不是另一个向量。Note that the dot product of two vectors is a number, not another vector. 另请注意,仅当两个矢量具有相同数量的组件,你可以计算点积。Also note that you can calculate the dot product only if the two vectors have the same number of components.

让 A(i, j) 包含矩阵 A 中的第 i 个行和 jth 列条目。Let A(i, j) be the entry in matrix A in the ith row and the jth column. 例如 A (3,2) 是矩阵 A 中的第三行和第二列中的输入。For example A(3, 2) is the entry in matrix A in the 3rd row and the 2nd column. 假设 A、 B 和 C 是矩阵和 AB = c。C 的条目的计算方式如下:Suppose A, B, and C are matrices, and AB = C. The entries of C are calculated as follows:

C (i,j) = (第 i,A 的行) • (列 j 的 B)C(i, j) = (row i of A) • (column j of B)

下图显示矩阵乘法的几个的示例。The following illustration shows several examples of matrix multiplication.

转换Transformations

如果您认为 1 × 2 矩阵作为平面中的点,您可以通过将乘以 2 × 2 矩阵转换该点。If you think of a point in a plane as a 1×2 matrix, you can transform that point by multiplying it by a 2×2 matrix. 下图显示应用于点 (2,1) 的多个转换。The following illustration shows several transformations applied to the point (2, 1).

转换Transformations

所有前面的图中所示的转换都是线性转换。All of the transformations shown in the preceding figure are linear transformations. 某些其他转换,如转换过程中,不是线性的并且无法表示为 2 × 2 矩阵相乘。Certain other transformations, such as translation, are not linear, and cannot be expressed as multiplication by a 2×2 matrix. 假设你想开始 (2,1) 的点旋转 90 度,将其转换 3 个单位在 x 方向,并将其 y 方向的 4 个单位。Suppose you want to start with the point (2, 1), rotate it 90 degrees, translate it 3 units in the x direction, and translate it 4 units in the y direction. 你可以通过使用跟矩阵添加矩阵乘法完成此操作。You can accomplish this by using a matrix multiplication followed by a matrix addition.

转换Transformations

线性转换 (2 × 2 矩阵相乘) 后, 跟一个翻译 (1 × 2 矩阵的加法) 称为仿射转换。A linear transformation (multiplication by a 2×2 matrix) followed by a translation (addition of a 1×2 matrix) is called an affine transformation. 存储仿射转换矩阵 (一个用于线性部分),一个用于转换的一对中的替代方法是在 3 × 3 矩阵中存储整个转换。An alternative to storing an affine transformation in a pair of matrices (one for the linear part and one for the translation) is to store the entire transformation in a 3×3 matrix. 若要完成此操作,请平面中的点必须存储在与虚拟的第三坐标的 1 × 3 矩阵。To make this work, a point in the plane must be stored in a 1×3 matrix with a dummy 3rd coordinate. 常用方法是使所有第三个坐标等于 1。The usual technique is to make all 3rd coordinates equal to 1. 例如,由矩阵 [2 1 1] 表示点 (2,1)。For example, the point (2, 1) is represented by the matrix [2 1 1]. 下图显示仿射转换 (旋转 90 度; 转换在 x 方向 3 个单位,y 方向的 4 个单位) 表示为单个 3 × 3 矩阵相乘。The following illustration shows an affine transformation (rotate 90 degrees; translate 3 units in the x direction, 4 units in the y direction) expressed as multiplication by a single 3×3 matrix.

转换Transformations

在前面的示例中,点 (2,1) 映射到的点 (2,6)。In the preceding example, the point (2, 1) is mapped to the point (2, 6). 请注意,3 × 3 矩阵的第三个列包含数字 0,0,1。Note that the third column of the 3×3 matrix contains the numbers 0, 0, 1. 这将始终为仿射转换的 3 × 3 矩阵这种情况。This will always be the case for the 3×3 matrix of an affine transformation. 重要的数字是 1 和 2 的列中的六个数字。The important numbers are the six numbers in columns 1 and 2. 矩阵的左上角 2 × 2 部分表示线性的转换的一部分,并且第三行中的前两个条目表示平移。The upper-left 2×2 portion of the matrix represents the linear part of the transformation, and the first two entries in the 3rd row represent the translation.

转换Transformations

GDI+GDI+可以存储在仿射转换Matrix对象。In GDI+GDI+ you can store an affine transformation in a Matrix object. 因为表示仿射转换矩阵的第三个列始终为 (0,0,1),你在前两个列中指定六个数字,在构造时Matrix对象。Because the third column of a matrix that represents an affine transformation is always (0, 0, 1), you specify only the six numbers in the first two columns when you construct a Matrix object. 语句Matrix myMatrix = new Matrix(0, 1, -1, 0, 3, 4)构造前面的图中所示的矩阵。The statement Matrix myMatrix = new Matrix(0, 1, -1, 0, 3, 4) constructs the matrix shown in the preceding figure.

复合转换Composite Transformations

复合转换是转换,一个跟另一个序列。A composite transformation is a sequence of transformations, one followed by the other. 请考虑矩阵和以下列表中的转换:Consider the matrices and transformations in the following list:

矩阵 AMatrix A 旋转 90 度Rotate 90 degrees
矩阵 BMatrix B 在 x 方向的 2 倍缩放Scale by a factor of 2 in the x direction
矩阵 CMatrix C 平移 y 方向的 3 个单位Translate 3 units in the y direction

如果我们从开始点 (2,1)-表示通过矩阵 [2 1 1]-和乘以 A、 B,然后 C,点 (2,1) 将进行按列出的顺序的三个转换。If we start with the point (2, 1) — represented by the matrix [2 1 1] — and multiply by A, then B, then C, the point (2, 1) will undergo the three transformations in the order listed.

[2 1 1]ABC = [-2 5 1][2 1 1]ABC = [-2 5 1]

而是不是存储在三个单独的矩阵复合转换的三个部分,你可以乘以 A、 B 和 C 在一起以获取存储整个复合转换的单个 3 × 3 矩阵。Rather than store the three parts of the composite transformation in three separate matrices, you can multiply A, B, and C together to get a single 3×3 matrix that stores the entire composite transformation. 假设 ABC = d。然后乘以 D 点会使相同的结果为点乘以 A、 B,然后按 c。Suppose ABC = D. Then a point multiplied by D gives the same result as a point multiplied by A, then B, then C.

[2 1 1]D = [-2 5 1][2 1 1]D = [-2 5 1]

下图显示了矩阵 A、 B、 C 和 d。The following illustration shows the matrices A, B, C, and D.

转换Transformations

复合转换的矩阵,可以构建乘以单独的变换矩阵的事实意味着,可以在单个存储仿射转换的任何序列Matrix对象。The fact that the matrix of a composite transformation can be formed by multiplying the individual transformation matrices means that any sequence of affine transformations can be stored in a single Matrix object.

小心

复合转换的顺序很重要。The order of a composite transformation is important. 一般情况下,旋转,再缩放、 然后转换不相同与先缩放、 旋转,然后转换。In general, rotate, then scale, then translate is not the same as scale, then rotate, then translate. 同样,矩阵乘法的顺序很重要。Similarly, the order of matrix multiplication is important. 一般情况下,ABC 不与备份相同。In general, ABC is not the same as BAC.

Matrix类提供用于构建复合转换的多种方法: MultiplyRotateRotateAtScaleShear,和TranslateThe Matrix class provides several methods for building a composite transformation: Multiply, Rotate, RotateAt, Scale, Shear, and Translate. 下面的示例创建一个复合转换,它首先旋转 30 度,然后按 y 方向的 2 倍缩放并会将 5 个单位在 x 方向的转换的矩阵。The following example creates the matrix of a composite transformation that first rotates 30 degrees, then scales by a factor of 2 in the y direction, and then translates 5 units in the x direction:

Matrix myMatrix = new Matrix();
myMatrix.Rotate(30);
myMatrix.Scale(1, 2, MatrixOrder.Append);
myMatrix.Translate(5, 0, MatrixOrder.Append);
Dim myMatrix As New Matrix()
myMatrix.Rotate(30)
myMatrix.Scale(1, 2, MatrixOrder.Append)
myMatrix.Translate(5, 0, MatrixOrder.Append)

下图显示矩阵。The following illustration shows the matrix.

转换Transformations

请参阅See Also

坐标系统和转换Coordinate Systems and Transformations
在托管 GDI+ 中使用转换Using Transformations in Managed GDI+