在给定 4 x 4 转换矩阵的情况下,可以使用 TransformBy 方法来移动、缩放或旋转对象。
下表说明了转换矩阵的配置,其中 R = 旋转,T = 平移:
转换矩阵配置 | |||
---|---|---|---|
R00 | R01 | R02 | T0 |
R10 | R11 | R12 | T1 |
R20 | R21 | R22 | T2 |
0 | 0 | 0 | 1 |
要变换对象,首先需要初始化转换矩阵。下例显示了指定给变量 tMatrix 的转换矩阵,该矩阵将图元绕点 (0,0,0) 旋转 90 度:
tMatrix(0,0) = 0.0
tMatrix(0,1) = -1.0
tMatrix(0,2) = 0.0
tMatrix(0,3) = 0.0
tMatrix(1,0) = 1.0
tMatrix(1,1) = 0.0
tMatrix(1,2) = 0.0
tMatrix(1,3) = 0.0
tMatrix(2,0) = 0.0
tMatrix(2,1) = 0.0
tMatrix(2,2) = 1.0
tMatrix(2,3) = 0.0
tMatrix(3,0) = 0.0
tMatrix(3,1) = 0.0
tMatrix(3,2) = 0.0
tMatrix(3,3) = 1.0
完成转换矩阵之后,使用 TransformBy 方法将矩阵应用到对象。下面的代码行演示了如何将矩阵 (tMatrix) 应用到对象 (anObj):
anObj.TransformBy tMatrix
Sub Ch4_TransformBy()
' 创建直线
Dim lineObj As AcadLine
Dim startPt(0 To 2) As Double
Dim endPt(0 To 2) As Double
startPt(0) = 2
startPt(1) = 1
startPt(2) = 0
endPt(0) = 5
endPt(1) = 1
endPt(2) = 0
Set lineObj = ThisDrawing.ModelSpace. _
AddLine(startPt, endPt)
ZoomAll
' 将 transMat 变量初始化为
' 转换矩阵,该矩阵将对象绕
' 点 (0,0,0) 旋转 90 度
Dim transMat(0 To 3, 0 To 3) As Double
transMat(0, 0) = 0#: transMat(0, 1) = -1#
transMat(0, 2) = 0#: transMat(0, 3) = 0#
transMat(1, 0) = 1#: transMat(1, 1) = 0#
transMat(1, 2) = 0#: transMat(1, 3) = 0#
transMat(2, 0) = 0#: transMat(2, 1) = 0#
transMat(2, 2) = 1#: transMat(2, 3) = 0#
transMat(3, 0) = 0#: transMat(3, 1) = 0#
transMat(3, 2) = 0#: transMat(3, 3) = 1#
' 使用定义的转换矩阵来变换直线
lineObj.TransformBy transMat
lineObj.Update
End Sub
旋转矩阵:围绕点 (0, 0, 0) 旋转 90 度 | |||
---|---|---|---|
0.0 | -1.0 | 0.0 | 0.0 |
1.0 | 0.0 | 0.0 | 0.0 |
0.0 | 0.0 | 1.0 | 0.0 |
0.0 | 0.0 | 0.0 | 1.0 |
旋转矩阵:围绕点 (5, 5, 0) 旋转 45 度 | |||
---|---|---|---|
0.707107 | -0.707107 | 0.0 | 5.0 |
0.707107 | 0.707107 | 0.0 | -2.071068 |
0.0 | 0.0 | 1.0 | 0.0 |
0.0 | 0.0 | 0.0 | 1.0 |
转换矩阵:将图元移到 (10, 10, 0) | |||
---|---|---|---|
1.0 | 0.0 | 0.0 | 10.0 |
0.0 | 1.0 | 0.0 | 10.0 |
0.0 | 0.0 | 1.0 | 0.0 |
0.0 | 0.0 | 0.0 | 1.0 |
缩放矩阵:在点 (0, 0, 0) 上按 10,10 进行缩放 | |||
---|---|---|---|
10.0 | 0.0 | 0.0 | 0.0 |
0.0 | 10.0 | 0.0 | 0.0 |
0.0 | 0.0 | 10.0 | 0.0 |
0.0 | 0.0 | 0.0 | 1.0 |
缩放矩阵:在点 (2, 2, 0) 上按 10,10 进行缩放 | |||
---|---|---|---|
10.0 | 0.0 | 0.0 | -18.0 |
0.0 | 10.0 | 0.0 | -18.0 |
0.0 | 0.0 | 10.0 | 0.0 |
0.0 | 0.0 | 0.0 | 1.0 |