home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH14 / SRC / OBJPLN4D.CLS < prev    next >
Encoding:
Text File  |  1996-05-04  |  6.0 KB  |  213 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "ObjPolyline4D"
  6. Attribute VB_Creatable = False
  7. Attribute VB_Exposed = False
  8. Option Explicit
  9.  
  10. ' Point4D and Segment4D are defined in module M4OPS.BAS as:
  11. '    Type Point4D
  12. '        coord(1 To 5) As Single
  13. '        trans(1 To 5) As Single
  14. '    End Type
  15. '
  16. '    Type Segment4D
  17. '        pt1 As Integer
  18. '        pt2 As Integer
  19. '    End Type
  20.  
  21. Private NumPoints As Integer ' Number of points.
  22. Private Points() As Point4D  ' Data points.
  23.  
  24. Private NumSegs As Integer   ' Number of segments.
  25. Private Segs() As Segment4D  ' The segments.
  26.  
  27.  
  28. ' ***********************************************
  29. ' Return a string indicating the object type.
  30. ' ***********************************************
  31. Property Get ObjectType() As String
  32.     ObjectType = "POLYLINE"
  33. End Property
  34.  
  35. ' ************************************************
  36. ' Add one or more line segments to the polyline.
  37. ' ************************************************
  38. Public Sub AddSegment(ParamArray coord() As Variant)
  39. Dim num_segs As Integer
  40. Dim i As Integer
  41. Dim last As Integer
  42. Dim pt As Integer
  43.  
  44.     num_segs = (UBound(coord) + 1) \ 4 - 1
  45.     ReDim Preserve Segs(1 To NumSegs + num_segs)
  46.  
  47.     last = AddPoint((coord(0)), (coord(1)), (coord(2)), (coord(3)))
  48.     pt = 0
  49.     For i = 1 To num_segs
  50.         Segs(NumSegs + i).pt1 = last
  51.         pt = pt + 4
  52.         last = AddPoint((coord(pt)), (coord(pt + 1)), (coord(pt + 2)), (coord(pt + 3)))
  53.         Segs(NumSegs + i).pt2 = last
  54.     Next i
  55.  
  56.     NumSegs = NumSegs + num_segs
  57. End Sub
  58.  
  59. ' ************************************************
  60. ' Add a point to the polyline or reuse a point.
  61. ' Return the point's index.
  62. ' ************************************************
  63. Private Function AddPoint(x As Single, y As Single, z As Single, w As Single) As Integer
  64. Dim i As Integer
  65.  
  66.     ' See if the point is already here.
  67.     For i = 1 To NumPoints
  68.         If x = Points(i).coord(1) And _
  69.            y = Points(i).coord(2) And _
  70.            z = Points(i).coord(3) And _
  71.            w = Points(i).coord(4) Then _
  72.                 Exit For
  73.     Next i
  74.     AddPoint = i
  75.     
  76.     ' If so, we're done.
  77.     If i <= NumPoints Then Exit Function
  78.     
  79.     ' Otherwise create the new point.
  80.     NumPoints = NumPoints + 1
  81.     ReDim Preserve Points(1 To NumPoints)
  82.     Points(i).coord(1) = x
  83.     Points(i).coord(2) = y
  84.     Points(i).coord(3) = z
  85.     Points(i).coord(4) = w
  86.     Points(i).coord(5) = 1#
  87. End Function
  88.  
  89.  
  90. ' ***********************************************
  91. ' Fix the data coordinates at their transformed
  92. ' values.
  93. ' ***********************************************
  94. Public Sub FixPoints()
  95. Dim i As Integer
  96. Dim j As Integer
  97.  
  98.     For i = 1 To NumPoints
  99.         For j = 1 To 4
  100.             Points(i).coord(j) = Points(i).trans(j)
  101.         Next j
  102.     Next i
  103. End Sub
  104.  
  105. ' ************************************************
  106. ' Apply a transformation matrix which may not
  107. ' contain 0, 0, 0, 0, 1 in the last column to the
  108. ' object.
  109. ' ************************************************
  110. Public Sub ApplyFull(M() As Single)
  111. Dim i As Integer
  112.  
  113.     For i = 1 To NumPoints
  114.         m4ApplyFull Points(i).coord, M, Points(i).trans
  115.     Next i
  116. End Sub
  117.  
  118. ' ************************************************
  119. ' Apply a transformation matrix to the object.
  120. ' ************************************************
  121. Public Sub Apply(M() As Single)
  122. Dim i As Integer
  123.  
  124.     For i = 1 To NumPoints
  125.         m4Apply Points(i).coord, M, Points(i).trans
  126.     Next i
  127. End Sub
  128.  
  129.  
  130. ' ************************************************
  131. ' Apply a nonlinear transformation.
  132. ' ************************************************
  133. Public Sub Distort4d(D As Object)
  134. Dim i As Integer
  135.  
  136.     For i = 1 To NumPoints
  137.         D.Distort4d Points(i).coord(1), Points(i).coord(2), Points(i).coord(3)
  138.     Next i
  139. End Sub
  140.  
  141. ' ************************************************
  142. ' Write a polyline to a file using Write.
  143. ' Begin with "POLYLINE4D" to identify this object.
  144. ' ************************************************
  145. Public Sub FileWrite(filenum As Integer)
  146. Dim i As Integer
  147.  
  148.     Write #filenum, "POLYLINE4D", NumPoints, NumSegs
  149.     
  150.     ' Write the points.
  151.     For i = 1 To NumPoints
  152.         Write #filenum, Points(i).coord(1), Points(i).coord(2), Points(i).coord(3), Points(i).coord(4)
  153.     Next i
  154.  
  155.     ' Write the segments.
  156.     For i = 1 To NumSegs
  157.         Write #filenum, Segs(i).pt1, Segs(i).pt2
  158.     Next i
  159. End Sub
  160.  
  161. ' ************************************************
  162. ' Draw the transformed points on a Form, Printer,
  163. ' or PictureBox.
  164. ' ************************************************
  165. Public Sub Draw(canvas As Object, Optional r As Variant)
  166. Dim seg As Integer
  167. Dim pt1 As Integer
  168. Dim pt2 As Integer
  169. Dim dist As Single
  170.  
  171.     On Error Resume Next
  172.     If IsMissing(r) Then r = INFINITY
  173.     dist = r
  174.     For seg = 1 To NumSegs
  175.         pt1 = Segs(seg).pt1
  176.         pt2 = Segs(seg).pt2
  177.         ' Don't draw if either point is farther
  178.         ' from the focus point than the center of
  179.         ' projection (which is distance dist away).
  180.         If Points(pt1).trans(4) < r And Points(pt2).trans(4) < r Then _
  181.             canvas.Line _
  182.                 (Points(pt1).trans(1), Points(pt1).trans(2))- _
  183.                 (Points(pt2).trans(1), Points(pt2).trans(2))
  184.     Next seg
  185. End Sub
  186.  
  187. ' ************************************************
  188. ' Read a polyline from a file using Input.
  189. ' Assume the "POLYLINE4D" label has already been
  190. ' read.
  191. ' ************************************************
  192. Public Sub FileInput(filenum As Integer)
  193. Dim i As Integer
  194.  
  195.     Input #filenum, NumPoints, NumSegs
  196.     
  197.     ' Allocate and read the points.
  198.     ReDim Points(1 To NumPoints)
  199.     For i = 1 To NumPoints
  200.         Input #filenum, Points(i).coord(1), Points(i).coord(2), Points(i).coord(3), Points(i).coord(4)
  201.         Points(i).coord(5) = 1
  202.     Next i
  203.     
  204.     ' Allocate and read the segments.
  205.     ReDim Segs(1 To NumSegs)
  206.     For i = 1 To NumSegs
  207.         Input #filenum, Segs(i).pt1, Segs(i).pt2
  208.     Next i
  209. End Sub
  210.  
  211.  
  212.  
  213.