home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch10 / Flake2.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-06-08  |  6.3 KB  |  213 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
  3. Begin VB.Form frmFlake2 
  4.    Caption         =   "Flake2"
  5.    ClientHeight    =   4785
  6.    ClientLeft      =   2280
  7.    ClientTop       =   1185
  8.    ClientWidth     =   5820
  9.    LinkTopic       =   "Form1"
  10.    PaletteMode     =   1  'UseZOrder
  11.    ScaleHeight     =   4785
  12.    ScaleWidth      =   5820
  13.    Begin MSComDlg.CommonDialog dlgFile 
  14.       Left            =   480
  15.       Top             =   1920
  16.       _ExtentX        =   847
  17.       _ExtentY        =   847
  18.       _Version        =   393216
  19.       CancelError     =   -1  'True
  20.    End
  21.    Begin VB.TextBox txtDepth 
  22.       Height          =   285
  23.       Left            =   600
  24.       MaxLength       =   3
  25.       TabIndex        =   0
  26.       Text            =   "3"
  27.       Top             =   0
  28.       Width           =   375
  29.    End
  30.    Begin VB.PictureBox picCanvas 
  31.       AutoRedraw      =   -1  'True
  32.       Height          =   4335
  33.       Left            =   1080
  34.       ScaleHeight     =   285
  35.       ScaleMode       =   3  'Pixel
  36.       ScaleWidth      =   261
  37.       TabIndex        =   3
  38.       Top             =   0
  39.       Width           =   3975
  40.    End
  41.    Begin VB.CommandButton cmdGo 
  42.       Caption         =   "Go"
  43.       Default         =   -1  'True
  44.       Enabled         =   0   'False
  45.       Height          =   375
  46.       Left            =   240
  47.       TabIndex        =   1
  48.       Top             =   480
  49.       Width           =   615
  50.    End
  51.    Begin VB.Label Label1 
  52.       Caption         =   "Depth"
  53.       Height          =   255
  54.       Index           =   0
  55.       Left            =   0
  56.       TabIndex        =   2
  57.       Top             =   0
  58.       Width           =   495
  59.    End
  60.    Begin VB.Menu mnuFile 
  61.       Caption         =   "&File"
  62.       Begin VB.Menu mnuFileOpen 
  63.          Caption         =   "&Open File..."
  64.          Shortcut        =   ^O
  65.       End
  66.    End
  67. Attribute VB_Name = "frmFlake2"
  68. Attribute VB_GlobalNameSpace = False
  69. Attribute VB_Creatable = False
  70. Attribute VB_PredeclaredId = True
  71. Attribute VB_Exposed = False
  72. Option Explicit
  73. Private Const PI = 3.14159
  74. ' Coordinates of the points in the initiator.
  75. Private NumInitiatorPoints As Integer
  76. Private InitiatorX() As Single
  77. Private InitiatorY() As Single
  78. ' Angles and distances for the generator.
  79. Private NumGeneratorAngles As Integer
  80. Private ScaleFactor As Single
  81. Private GeneratorDTheta() As Single
  82. ' Draw the complete snowflake.
  83. Private Sub DrawFlake(ByVal depth As Integer, ByVal length As Single)
  84. Dim i As Integer
  85. Dim x1 As Single
  86. Dim y1 As Single
  87. Dim x2 As Single
  88. Dim y2 As Single
  89. Dim dx As Single
  90. Dim dy As Single
  91. Dim theta As Single
  92.     picCanvas.Cls
  93.     ' Draw the snowflake.
  94.     For i = 1 To NumInitiatorPoints
  95.         x1 = InitiatorX(i - 1)
  96.         y1 = InitiatorY(i - 1)
  97.         x2 = InitiatorX(i)
  98.         y2 = InitiatorY(i)
  99.         dx = x2 - x1
  100.         dy = y2 - y1
  101.         theta = ATan2(dy, dx)
  102.         DrawFlakeEdge depth, x1, y1, _
  103.             theta, length
  104.     Next i
  105. End Sub
  106. ' Recursively draw a snowflake edge starting at
  107. ' (x1, y1) in direction theta and distance dist.
  108. ' Leave the coordinates of the endpoint in
  109. ' (x1, y1).
  110. Private Sub DrawFlakeEdge(ByVal depth As Integer, ByRef x1 As Single, ByRef y1 As Single, ByVal theta As Single, ByVal dist As Single)
  111. Dim status As Integer
  112. Dim i As Integer
  113. Dim x2 As Single
  114. Dim y2 As Single
  115.     If depth <= 0 Then
  116.         x2 = x1 + dist * Cos(theta)
  117.         y2 = y1 + dist * Sin(theta)
  118.         picCanvas.Line (x1, y1)-(x2, y2)
  119.         x1 = x2
  120.         y1 = y2
  121.         Exit Sub
  122.     End If
  123.     ' Recursively draw the edge.
  124.     dist = dist * ScaleFactor
  125.     For i = 1 To NumGeneratorAngles
  126.         theta = theta + GeneratorDTheta(i)
  127.         DrawFlakeEdge depth - 1, x1, y1, theta, dist
  128.     Next i
  129. End Sub
  130. Private Sub CmdGo_Click()
  131. Dim depth As Integer
  132. Dim dx As Single
  133. Dim dy As Single
  134. Dim length As Single
  135.     picCanvas.Cls
  136.     MousePointer = vbHourglass
  137.     DoEvents
  138.     ' Get the parameters.
  139.     If Not IsNumeric(txtDepth.Text) Then txtDepth.Text = "5"
  140.     depth = CInt(txtDepth.Text)
  141.     ' Find the distance between initiator points.
  142.     dx = InitiatorX(2) - InitiatorX(1)
  143.     dy = InitiatorY(2) - InitiatorY(1)
  144.     length = Sqr(dx * dx + dy * dy)
  145.     ' Draw the snowflake.
  146.     DrawFlake depth, length
  147.     MousePointer = vbDefault
  148. End Sub
  149. Private Sub Form_Load()
  150.     dlgFile.Filter = "Snowflake Files (*.sno)|*.sno"
  151.     dlgFile.InitDir = App.Path
  152. End Sub
  153. Private Sub Form_Resize()
  154. Dim wid As Single
  155.     ' Make the picCanvas as big as possible.
  156.     wid = ScaleWidth - picCanvas.Left
  157.     If wid < 120 Then wid = 120
  158.     picCanvas.Move picCanvas.Left, 0, wid, ScaleHeight
  159. End Sub
  160. ' Load a snowflake definition file with format:
  161. '   # Initiator points.
  162. '   (x1, y1)
  163. '   (x2, y2)
  164. '       :
  165. '   scalefactor
  166. '   # Generator angles.
  167. '   theta1
  168. '   theta2
  169. '       :
  170. Private Sub mnuFileOpen_Click()
  171. Dim file_name As String
  172. Dim fnum As Integer
  173. Dim theta As Single
  174. Dim i As Integer
  175.     ' Allow the user to pick a file.
  176.     On Error Resume Next
  177.     dlgFile.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  178.     dlgFile.ShowOpen
  179.     If Err.Number = cdlCancel Then
  180.         Exit Sub
  181.     ElseIf Err.Number <> 0 Then
  182.         Beep
  183.         MsgBox "Error selecting file.", , vbExclamation
  184.         Exit Sub
  185.     End If
  186.     On Error GoTo 0
  187.     file_name = Trim$(dlgFile.FileName)
  188.     dlgFile.InitDir = Left$(file_name, Len(file_name) _
  189.         - Len(dlgFile.FileTitle) - 1)
  190.     ' Open the file.
  191.     fnum = FreeFile
  192.     Open file_name For Input Access Read As #fnum
  193.     ' Read the initiator.
  194.     Input #fnum, NumInitiatorPoints
  195.     ReDim InitiatorX(0 To NumInitiatorPoints)
  196.     ReDim InitiatorY(0 To NumInitiatorPoints)
  197.     For i = 1 To NumInitiatorPoints
  198.         Input #fnum, InitiatorX(i), InitiatorY(i)
  199.     Next i
  200.     InitiatorX(0) = InitiatorX(NumInitiatorPoints)
  201.     InitiatorY(0) = InitiatorY(NumInitiatorPoints)
  202.     ' Read the generator information.
  203.     Input #fnum, ScaleFactor, NumGeneratorAngles
  204.     ReDim GeneratorDTheta(1 To NumGeneratorAngles)
  205.     For i = 1 To NumGeneratorAngles
  206.         Input #fnum, theta
  207.         GeneratorDTheta(i) = theta * PI / 180
  208.     Next i
  209.     Close #fnum
  210.     Caption = "Flake2 [" & dlgFile.FileTitle & "]"
  211.     cmdGo.Enabled = True
  212. End Sub
  213.