home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH5 / SRC / PLANETS.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-05-02  |  8.3 KB  |  280 lines

  1. VERSION 4.00
  2. Begin VB.Form PlanetForm 
  3.    Caption         =   "Planets"
  4.    ClientHeight    =   5775
  5.    ClientLeft      =   1590
  6.    ClientTop       =   735
  7.    ClientWidth     =   6015
  8.    Height          =   6465
  9.    Left            =   1530
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   385
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   401
  14.    Top             =   105
  15.    Width           =   6135
  16.    Begin VB.TextBox FPSText 
  17.       Height          =   285
  18.       Left            =   1560
  19.       TabIndex        =   3
  20.       Text            =   "20"
  21.       Top             =   5400
  22.       Width           =   375
  23.    End
  24.    Begin VB.CommandButton CmdRun 
  25.       Caption         =   "Run"
  26.       Default         =   -1  'True
  27.       Enabled         =   0   'False
  28.       Height          =   495
  29.       Left            =   2160
  30.       TabIndex        =   1
  31.       Top             =   5280
  32.       Width           =   855
  33.    End
  34.    Begin VB.PictureBox Canvas 
  35.       AutoRedraw      =   -1  'True
  36.       FillStyle       =   0  'Solid
  37.       Height          =   5250
  38.       Left            =   0
  39.       Picture         =   "PLANETS.frx":0000
  40.       ScaleHeight     =   346
  41.       ScaleMode       =   3  'Pixel
  42.       ScaleWidth      =   396
  43.       TabIndex        =   0
  44.       Top             =   0
  45.       Width           =   6000
  46.    End
  47.    Begin MSComDlg.CommonDialog FileDialog 
  48.       Left            =   3120
  49.       Top             =   5280
  50.       _version        =   65536
  51.       _extentx        =   847
  52.       _extenty        =   847
  53.       _stockprops     =   0
  54.       cancelerror     =   -1  'True
  55.    End
  56.    Begin VB.Label Label1 
  57.       Caption         =   "Frames per second:"
  58.       Height          =   255
  59.       Left            =   120
  60.       TabIndex        =   2
  61.       Top             =   5400
  62.       Width           =   1455
  63.    End
  64.    Begin VB.Menu mnuFile 
  65.       Caption         =   "&File"
  66.       Begin VB.Menu mnuFileLoad 
  67.          Caption         =   "&Load..."
  68.          Shortcut        =   ^L
  69.       End
  70.       Begin VB.Menu mnuFileSep 
  71.          Caption         =   "-"
  72.       End
  73.       Begin VB.Menu mnuFileExit 
  74.          Caption         =   "E&xit"
  75.       End
  76.    End
  77. Attribute VB_Name = "PlanetForm"
  78. Attribute VB_Creatable = False
  79. Attribute VB_Exposed = False
  80. Option Explicit
  81. Dim Playing As Boolean
  82. Dim NumPlanets As Integer
  83. Dim Cx() As Double          ' Position.
  84. Dim Cy() As Double
  85. Dim Vx() As Double          ' Velocity.
  86. Dim Vy() As Double
  87. Dim M() As Double           ' Mass.
  88. Dim R() As Double           ' Radius.
  89. Dim Clr() As Long           ' Colors.
  90. ' ************************************************
  91. ' Pause a little while.
  92. ' ************************************************
  93. Sub Delay(interval As Double)
  94. Dim start_time As Double
  95.     start_time = Timer
  96.     Do While Timer - start_time < interval
  97.         DoEvents
  98.     Loop
  99. End Sub
  100. ' ************************************************
  101. ' Load the data in a planet file.
  102. ' ************************************************
  103. Sub LoadPlanets(fname As String)
  104. Dim fnum As Integer
  105. Dim i As Integer
  106. Dim old_style As Integer
  107.     fnum = FreeFile
  108.     Open fname For Input As #fnum
  109.         
  110.     Input #fnum, NumPlanets
  111.     ReDim Cx(1 To NumPlanets)
  112.     ReDim Cy(1 To NumPlanets)
  113.     ReDim Vx(1 To NumPlanets)
  114.     ReDim Vy(1 To NumPlanets)
  115.     ReDim M(1 To NumPlanets)
  116.     ReDim R(1 To NumPlanets)
  117.     ReDim Clr(1 To NumPlanets)
  118.     For i = 1 To NumPlanets
  119.         Input #fnum, _
  120.             Cx(i), Cy(i), Vx(i), Vy(i), M(i), Clr(i)
  121.         R(i) = Sqr(M(i)) + 1
  122.     Next i
  123.         
  124.     Close #fnum
  125.     ' Draw the planets.
  126.     old_style = Canvas.FillStyle
  127.     Canvas.FillStyle = vbSolid
  128.     Canvas.Cls
  129.     For i = 1 To NumPlanets
  130.         Canvas.FillColor = Clr(i)
  131.         Canvas.Circle (Cx(i), Cy(i)), R(i), Clr(i)
  132.     Next i
  133.     Canvas.FillStyle = old_style
  134.     Caption = "Planets [" & fname & "]"
  135.     CmdRun.Enabled = True
  136. End Sub
  137. ' ************************************************
  138. ' Make the planets move until Playing is false.
  139. ' ************************************************
  140. Sub PlaySimulation()
  141. Const F_SCALE = 1000
  142. Dim mpf As Long     ' Milliseconds per frame.
  143. Dim next_time As Long
  144. Dim old_style As Integer
  145. Dim i As Integer
  146. Dim j As Integer
  147. Dim dx As Double
  148. Dim dy As Double
  149. Dim d2 As Double
  150. Dim d As Double
  151. Dim f As Double
  152. Dim a_d As Double
  153.     ' Set FillStyle to vbSolid.
  154.     old_style = Canvas.FillStyle
  155.     Canvas.FillStyle = vbSolid
  156.     ' See how fast we should go.
  157.     If Not IsNumeric(FPSText.Text) Then _
  158.         FPSText.Text = "10"
  159.     mpf = 1000 \ CLng(FPSText.Text)
  160.     ' Start the animation.
  161.     next_time = GetTickCount()
  162.     Do While Playing
  163.         ' Calculate the forces on the planets.
  164.         For i = 1 To NumPlanets - 1
  165.             For j = i + 1 To NumPlanets
  166.                 ' Calculate the force between planets
  167.                 ' i and j. Translate the force into a
  168.                 ' change in velocity.
  169.                 dx = Cx(i) - Cx(j)
  170.                 dy = Cy(i) - Cy(j)
  171.                 d2 = dx * dx + dy * dy
  172.                 f = F_SCALE * M(i) * M(j) / d2
  173.                 d = Sqr(d2)
  174.                             
  175.                 a_d = f / M(i) / d
  176.                 Vx(i) = Vx(i) - a_d * dx
  177.                 Vy(i) = Vy(i) - a_d * dy
  178.             
  179.                 a_d = f / M(j) / d
  180.                 Vx(j) = Vx(j) + a_d * dx
  181.                 Vy(j) = Vy(j) + a_d * dy
  182.             Next j
  183.         Next i
  184.         
  185.         ' Move all the planets.
  186.         For i = 1 To NumPlanets
  187.             Cx(i) = Cx(i) + Vx(i)
  188.             Cy(i) = Cy(i) + Vy(i)
  189.         Next i
  190.            
  191.         ' Redraw the planets.
  192.         Canvas.Cls
  193.         For i = 1 To NumPlanets
  194.             Canvas.FillColor = Clr(i)
  195.             Canvas.Circle (Cx(i), Cy(i)), R(i), Clr(i)
  196.         Next i
  197.         ' Wait until it's time for the next frame.
  198.         next_time = next_time + mpf
  199.         WaitTill next_time
  200.     Loop
  201.     ' Restore the old FillStyle.
  202.     Canvas.FillStyle = old_style
  203. End Sub
  204. ' ************************************************
  205. ' Start a new simulation.
  206. ' ************************************************
  207. Private Sub CmdRun_Click()
  208.     If Playing Then
  209.         CmdRun.Caption = "Stopped"
  210.         CmdRun.Enabled = False
  211.         Playing = False
  212.     Else
  213.         Playing = True
  214.         CmdRun.Caption = "Stop"
  215.         PlaySimulation
  216.         CmdRun.Caption = "Run"
  217.         CmdRun.Enabled = True
  218.         Playing = False
  219.     End If
  220. End Sub
  221. Private Sub Form_Resize()
  222. Const GAP = 3
  223. Dim hgt As Double
  224.     hgt = ScaleHeight - CmdRun.Height - 2 * GAP
  225.     Canvas.Move 0, 0, ScaleWidth, hgt
  226.     CmdRun.Move (ScaleWidth - CmdRun.Width) / 2, _
  227.         Canvas.Height + GAP
  228.     Label1.Top = CmdRun.Top
  229.     FPSText.Top = CmdRun.Top
  230. End Sub
  231. Private Sub Form_Unload(Cancel As Integer)
  232.     End
  233. End Sub
  234. ' ***********************************************
  235. ' Load a new data file.
  236. ' ***********************************************
  237. Private Sub mnuFileLoad_Click()
  238. Dim fname As String
  239.     ' Allow the user to pick a file.
  240.     On Error Resume Next
  241.     FileDialog.filename = "*.PLA"
  242.     FileDialog.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  243.     FileDialog.ShowOpen
  244.     If Err.Number = cdlCancel Then
  245.         Exit Sub
  246.     ElseIf Err.Number <> 0 Then
  247.         Beep
  248.         MsgBox "Error selecting file.", , vbExclamation
  249.         Exit Sub
  250.     End If
  251.     On Error GoTo 0
  252.     fname = Trim$(FileDialog.filename)
  253.     FileDialog.InitDir = Left$(fname, Len(fname) _
  254.         - Len(FileDialog.FileTitle) - 1)
  255.     ' Load the picture.
  256.     WaitStart
  257.     LoadPlanets fname
  258.     WaitEnd
  259. End Sub
  260. ' ***********************************************
  261. ' Give the form and all the picture boxes an
  262. ' hourglass cursor.
  263. ' ***********************************************
  264. Sub WaitStart()
  265.     MousePointer = vbHourglass
  266.     Canvas.MousePointer = vbHourglass
  267.     DoEvents
  268. End Sub
  269. ' ***********************************************
  270. ' Restore the mouse pointers for the form and all
  271. ' the picture boxes.
  272. ' ***********************************************
  273. Sub WaitEnd()
  274.     MousePointer = vbDefault
  275.     Canvas.MousePointer = vbDefault
  276. End Sub
  277. Private Sub mnuFileExit_Click()
  278.     Unload Me
  279. End Sub
  280.