home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH7 / SRC / M2OPS.BAS < prev    next >
Encoding:
BASIC Source File  |  1995-11-07  |  6.7 KB  |  227 lines

  1. Attribute VB_Name = "M2Ops"
  2. ' ***********************************************
  3. ' Routines for manipulating 2-dimensional
  4. ' vectors and matrices.
  5. ' ***********************************************
  6. Option Explicit
  7.  
  8. ' ************************************************
  9. ' Multiply a point and a matrix.
  10. ' ************************************************
  11. Sub m2PointMultiply(x As Single, y As Single, A() As Single)
  12. Dim newx As Single
  13. Dim newy As Single
  14.  
  15.     newx = x * A(1, 1) + y * A(2, 1) + A(3, 1)
  16.     newy = x * A(1, 2) + y * A(2, 2) + A(3, 2)
  17.     x = newx
  18.     y = newy
  19. End Sub
  20. ' *******************************************************
  21. ' Create a 2-dimensional identity matrix.
  22. ' *******************************************************
  23. Public Sub m2Identity(M() As Single)
  24. Dim i As Integer
  25. Dim j As Integer
  26.  
  27.     For i = 1 To 3
  28.         For j = 1 To 3
  29.             If i = j Then
  30.                 M(i, j) = 1
  31.             Else
  32.                 M(i, j) = 0
  33.             End If
  34.         Next j
  35.     Next i
  36. End Sub
  37. ' *******************************************************
  38. ' Set copy = orig.
  39. ' *******************************************************
  40. Public Sub m2PointCopy(copy() As Single, orig() As Single)
  41. Dim i As Integer
  42.  
  43.     For i = 1 To 3
  44.         copy(i) = orig(i)
  45.     Next i
  46. End Sub
  47.  
  48.  
  49. ' *******************************************************
  50. ' Set copy = orig.
  51. ' *******************************************************
  52. Public Sub m2MatCopy(copy() As Single, orig() As Single)
  53. Dim i As Integer
  54. Dim j As Integer
  55.  
  56.     For i = 1 To 3
  57.         For j = 1 To 3
  58.             copy(i, j) = orig(i, j)
  59.         Next j
  60.     Next i
  61. End Sub
  62.  
  63.  
  64.  
  65. ' ************************************************
  66. ' Apply a transformation matrix to a point.
  67. ' ************************************************
  68. Sub m2Apply(V() As Single, A() As Single, Result() As Single)
  69.     Result(1) = V(1) * A(1, 1) + V(2) * A(2, 1) + A(3, 1)
  70.     Result(2) = V(1) * A(1, 2) + V(2) * A(2, 2) + A(3, 2)
  71.     Result(3) = 1#
  72. End Sub
  73.  
  74.  
  75. ' ************************************************
  76. ' Create a translation matrix for translation by
  77. ' distances Tx and Ty.
  78. ' ************************************************
  79. Sub m2Translate(Result() As Single, Tx As Single, Ty As Single)
  80.     m2Identity Result
  81.     Result(3, 1) = Tx
  82.     Result(3, 2) = Ty
  83. End Sub
  84.  
  85. ' ************************************************
  86. ' Multiply two transformation matrices.
  87. ' ************************************************
  88. Sub m2MatMultiply(Result() As Single, A() As Single, B() As Single)
  89.     Result(1, 1) = A(1, 1) * B(1, 1) + A(1, 2) * B(2, 1)
  90.     Result(1, 2) = A(1, 1) * B(1, 2) + A(1, 2) * B(2, 2)
  91.     Result(1, 3) = 0#
  92.     Result(2, 1) = A(2, 1) * B(1, 1) + A(2, 2) * B(2, 1)
  93.     Result(2, 2) = A(2, 1) * B(1, 2) + A(2, 2) * B(2, 2)
  94.     Result(2, 3) = 0#
  95.     Result(3, 1) = A(3, 1) * B(1, 1) + A(3, 2) * B(2, 1) + B(3, 1)
  96.     Result(3, 2) = A(3, 1) * B(1, 2) + A(3, 2) * B(2, 2) + B(3, 2)
  97.     Result(3, 3) = 1#
  98. End Sub
  99.  
  100. ' ************************************************
  101. ' Create a rotation matrix that rotates the point
  102. ' (x, y) onto the X axis.
  103. ' ************************************************
  104. Sub m2RotateIntoX(Result() As Single, x As Single, y As Single)
  105. Dim d As Single
  106.  
  107.     m2Identity Result
  108.     d = Sqr(x * x + y * y)
  109.     Result(1, 1) = x / d
  110.     Result(1, 2) = -y / d
  111.     Result(2, 1) = -Result(1, 2)
  112.     Result(2, 2) = Result(1, 1)
  113. End Sub
  114. ' ************************************************
  115. ' Create a rotation matrix for rotating by the
  116. ' given angle (in radians).
  117. ' ************************************************
  118. Sub m2Rotate(Result() As Single, Theta As Single)
  119.     m2Identity Result
  120.     Result(1, 1) = Cos(Theta)
  121.     Result(1, 2) = Sin(Theta)
  122.     Result(2, 1) = -Result(1, 2)
  123.     Result(2, 2) = Result(1, 1)
  124. End Sub
  125.  
  126.  
  127. ' ************************************************
  128. ' Create a scaling matrix for scaling by factors
  129. ' of Sx and Sy.
  130. ' ************************************************
  131. Sub m2Scale(Result() As Single, Sx As Single, Sy As Single)
  132.     m2Identity Result
  133.     Result(1, 1) = Sx
  134.     Result(2, 2) = Sy
  135. End Sub
  136.  
  137. ' ************************************************
  138. ' Create a rotation matrix for rotating through
  139. ' angle theta around the point (x, y).
  140. ' ************************************************
  141. Sub m2RotateAround(Result() As Single, Theta As Single, x As Single, y As Single)
  142. Dim T(1 To 3, 1 To 3) As Single
  143. Dim R(1 To 3, 1 To 3) As Single
  144. Dim T_Inv(1 To 3, 1 To 3) As Single
  145. Dim M(1 To 3, 1 To 3) As Single
  146.  
  147.     ' Translate the point to the origin.
  148.     m2Translate T, -x, -y
  149.     m2Translate T_Inv, x, y
  150.     
  151.     ' Rotate.
  152.     m2Rotate R, Theta
  153.     
  154.     ' Combine the transformations.
  155.     m2MatMultiply M, T, R
  156.     m2MatMultiply Result, M, T_Inv
  157. End Sub
  158.  
  159. ' ************************************************
  160. ' Create a matrix for reflecting across the line
  161. ' passing through (x, y) in direction <dx, dy>.
  162. ' ************************************************
  163. Sub m2ReflectAcross(Result() As Single, x As Single, y As Single, dx As Single, dy As Single)
  164. Dim T(1 To 3, 1 To 3) As Single
  165. Dim R(1 To 3, 1 To 3) As Single
  166. Dim S(1 To 3, 1 To 3) As Single
  167. Dim R_Inv(1 To 3, 1 To 3) As Single
  168. Dim T_Inv(1 To 3, 1 To 3) As Single
  169. Dim M1(1 To 3, 1 To 3) As Single
  170. Dim M2(1 To 3, 1 To 3) As Single
  171. Dim M3(1 To 3, 1 To 3) As Single
  172.  
  173.     ' Translate the point to the origin.
  174.     m2Translate T, -x, -y
  175.  
  176.     ' Compute the inverse translation.
  177.     m2Translate T_Inv, x, y
  178.     
  179.     ' Rotate so the direction vector lies in the
  180.     ' Y axis.
  181.     m2RotateIntoX R, dx, dy
  182.  
  183.     ' Compute the inverse translation.
  184.     m2RotateIntoX R_Inv, dx, -dy
  185.  
  186.     ' Reflect across the X axis.
  187.     m2Scale S, 1, -1
  188.     
  189.     ' Combine the transformations.
  190.     m2MatMultiply M1, T, R     ' T * R
  191.     m2MatMultiply M2, S, R_Inv ' S * R_Inv
  192.     m2MatMultiply M3, M1, M2   ' T * R * S * R_Inv
  193.     
  194.     ' T * R * S * R_Inv * T_Inv
  195.     m2MatMultiply Result, M3, T_Inv
  196. End Sub
  197.  
  198.  
  199.  
  200. ' ************************************************
  201. ' Create a scaling matrix for scaling by factors
  202. ' of Sx and Sy at the point (x, y).
  203. ' ************************************************
  204. Sub m2ScaleAt(Result() As Single, Sx As Single, Sy As Single, x As Single, y As Single)
  205. Dim T(1 To 3, 1 To 3) As Single
  206. Dim S(1 To 3, 1 To 3) As Single
  207. Dim T_Inv(1 To 3, 1 To 3) As Single
  208. Dim M(1 To 3, 1 To 3) As Single
  209.  
  210.     ' Translate the point to the origin.
  211.     m2Translate T, -x, -y
  212.  
  213.     ' Compute the inverse translation.
  214.     m2Translate T_Inv, x, y
  215.     
  216.     ' Scale.
  217.     m2Scale S, Sx, Sy
  218.     
  219.     ' Combine the transformations.
  220.     m2MatMultiply M, T, S           ' T * S
  221.     m2MatMultiply Result, M, T_Inv  ' T * S * T_Inv
  222. End Sub
  223.  
  224.  
  225.  
  226.  
  227.