home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch12 / 2dPgon2.cls < prev    next >
Encoding:
Visual Basic class definition  |  1999-06-17  |  2.7 KB  |  98 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "TwoDPolygon"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = False
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Option Explicit
  15.  
  16. Private Declare Function Polygon Lib "gdi32" (ByVal hdc As Long, lpPoint As POINTAPI, ByVal nCount As Long) As Long
  17. Private Type POINTAPI
  18.     X As Long
  19.     Y As Long
  20. End Type
  21.  
  22. ' The object's points.
  23. Private m_NumPoints As Long
  24. Private m_Points() As POINTAPI
  25.  
  26. ' Invalid property-array index
  27. Private Const INVALID_INDEX = 381
  28. ' Return the number of points.
  29. Public Property Get NumPoints() As Integer
  30.     NumPoints = m_NumPoints
  31. End Property
  32. ' Set the number of points.
  33. Public Property Let NumPoints(ByVal new_value As Integer)
  34.     m_NumPoints = new_value
  35.     If m_NumPoints < 1 Then
  36.         Erase m_Points
  37.     Else
  38.         ReDim m_Points(1 To NumPoints)
  39.     End If
  40. End Property
  41. ' Return an X coordinate.
  42. Property Get X(ByVal Index As Integer) As Single
  43.     If (Index < 1) Or (Index > NumPoints) Then
  44.         Err.Raise INVALID_INDEX, "TwoDPolygon.X"
  45.     End If
  46.  
  47.     X = m_Points(Index).X
  48. End Property
  49. ' Return a Y coordinate.
  50. Property Get Y(ByVal Index As Integer) As Single
  51.     If (Index < 1) Or (Index > NumPoints) Then
  52.         Err.Raise INVALID_INDEX, "TwoDPolygon.X"
  53.     End If
  54.  
  55.     Y = m_Points(Index).Y
  56. End Property
  57. ' Set an X coordinate.
  58. Property Let X(ByVal Index As Integer, ByVal new_value As Single)
  59.     If (Index < 1) Or (Index > NumPoints) Then
  60.         Err.Raise INVALID_INDEX, "TwoDPolygon.X"
  61.     End If
  62.  
  63.     m_Points(Index).X = new_value
  64. End Property
  65. ' Set a Y coordinate.
  66. Property Let Y(ByVal Index As Integer, ByVal new_value As Single)
  67.     If (Index < 1) Or (Index > NumPoints) Then
  68.         Err.Raise INVALID_INDEX, "TwoDPolygon.X"
  69.     End If
  70.  
  71.     m_Points(Index).Y = new_value
  72. End Property
  73.  
  74. ' Draw the object on the canvas.
  75. Public Sub Draw(ByVal canvas As Object)
  76.     ' Make sure we have at least 2 points.
  77.     If NumPoints < 2 Then Exit Sub
  78.  
  79.     ' Draw the polygon.
  80.     Polygon canvas.hdc, m_Points(1), NumPoints
  81. End Sub
  82. ' Transform the object using a two-dimensional
  83. ' transformation matrix.
  84. Public Sub Transform(M() As Single)
  85. Dim i As Integer
  86. Dim new_x As Single
  87. Dim new_y As Single
  88.  
  89.     For i = 1 To m_NumPoints
  90.         With m_Points(i)
  91.             new_x = .X * M(1, 1) + .Y * M(2, 1) + M(3, 1)
  92.             new_y = .X * M(1, 2) + .Y * M(2, 2) + M(3, 2)
  93.             .X = new_x
  94.             .Y = new_y
  95.         End With
  96.     Next i
  97. End Sub
  98.