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

  1. VERSION 4.00
  2. Begin VB.Form FractalForm 
  3.    Appearance      =   0  'Flat
  4.    BackColor       =   &H00C0C0C0&
  5.    Caption         =   "Fractal Surfaces"
  6.    ClientHeight    =   5700
  7.    ClientLeft      =   300
  8.    ClientTop       =   855
  9.    ClientWidth     =   9090
  10.    BeginProperty Font 
  11.       name            =   "MS Sans Serif"
  12.       charset         =   1
  13.       weight          =   700
  14.       size            =   8.25
  15.       underline       =   0   'False
  16.       italic          =   0   'False
  17.       strikethrough   =   0   'False
  18.    EndProperty
  19.    ForeColor       =   &H80000008&
  20.    Height          =   6390
  21.    KeyPreview      =   -1  'True
  22.    Left            =   240
  23.    LinkTopic       =   "Form1"
  24.    ScaleHeight     =   380
  25.    ScaleMode       =   3  'Pixel
  26.    ScaleWidth      =   606
  27.    Top             =   225
  28.    Width           =   9210
  29.    Begin VB.TextBox DyText 
  30.       Height          =   285
  31.       Left            =   8280
  32.       TabIndex        =   18
  33.       Text            =   "0.15"
  34.       Top             =   4920
  35.       Width           =   495
  36.    End
  37.    Begin VB.TextBox LevelText 
  38.       Height          =   285
  39.       Left            =   8280
  40.       MaxLength       =   2
  41.       TabIndex        =   17
  42.       Text            =   "3"
  43.       Top             =   4560
  44.       Width           =   495
  45.    End
  46.    Begin VB.CheckBox ShowHiddenCheck 
  47.       Caption         =   "Show Hidden Surfaces"
  48.       Height          =   495
  49.       Left            =   7080
  50.       TabIndex        =   14
  51.       Top             =   3720
  52.       Width           =   2055
  53.    End
  54.    Begin VB.CheckBox ShowAxesCheck 
  55.       Caption         =   "Show Axes"
  56.       Height          =   255
  57.       Left            =   7080
  58.       TabIndex        =   13
  59.       Top             =   3360
  60.       Width           =   2055
  61.    End
  62.    Begin VB.TextBox PhiText 
  63.       Height          =   285
  64.       Left            =   3600
  65.       TabIndex        =   12
  66.       Text            =   "0.1570"
  67.       Top             =   5400
  68.       Width           =   855
  69.    End
  70.    Begin VB.TextBox ThetaText 
  71.       Height          =   285
  72.       Left            =   2040
  73.       TabIndex        =   10
  74.       Text            =   "0.6283"
  75.       Top             =   5400
  76.       Width           =   855
  77.    End
  78.    Begin VB.TextBox RText 
  79.       Height          =   285
  80.       Left            =   480
  81.       TabIndex        =   8
  82.       Text            =   "10"
  83.       Top             =   5400
  84.       Width           =   855
  85.    End
  86.    Begin VB.OptionButton Choice 
  87.       Caption         =   "Random"
  88.       Height          =   255
  89.       Index           =   5
  90.       Left            =   7080
  91.       TabIndex        =   7
  92.       Top             =   1800
  93.       Width           =   2055
  94.    End
  95.    Begin VB.OptionButton Choice 
  96.       Caption         =   "Rugged Ridge"
  97.       Height          =   255
  98.       Index           =   4
  99.       Left            =   7080
  100.       TabIndex        =   6
  101.       Top             =   1440
  102.       Width           =   2055
  103.    End
  104.    Begin VB.OptionButton Choice 
  105.       Caption         =   "Peaked Ridge"
  106.       Height          =   255
  107.       Index           =   3
  108.       Left            =   7080
  109.       TabIndex        =   5
  110.       Top             =   1080
  111.       Width           =   2055
  112.    End
  113.    Begin VB.OptionButton Choice 
  114.       Caption         =   "Ridge"
  115.       Height          =   255
  116.       Index           =   2
  117.       Left            =   7080
  118.       TabIndex        =   4
  119.       Top             =   720
  120.       Width           =   2055
  121.    End
  122.    Begin VB.OptionButton Choice 
  123.       Caption         =   "Hill"
  124.       Height          =   255
  125.       Index           =   1
  126.       Left            =   7080
  127.       TabIndex        =   3
  128.       Top             =   360
  129.       Width           =   2055
  130.    End
  131.    Begin VB.OptionButton Choice 
  132.       Caption         =   "Mountain"
  133.       Height          =   255
  134.       Index           =   0
  135.       Left            =   7080
  136.       TabIndex        =   2
  137.       Top             =   0
  138.       Value           =   -1  'True
  139.       Width           =   2055
  140.    End
  141.    Begin VB.PictureBox Pict 
  142.       AutoRedraw      =   -1  'True
  143.       Height          =   5295
  144.       Left            =   0
  145.       ScaleHeight     =   349
  146.       ScaleMode       =   3  'Pixel
  147.       ScaleWidth      =   461
  148.       TabIndex        =   0
  149.       Top             =   0
  150.       Width           =   6975
  151.    End
  152.    Begin VB.Label Label1 
  153.       Caption         =   "Displacement"
  154.       Height          =   255
  155.       Index           =   4
  156.       Left            =   7080
  157.       TabIndex        =   16
  158.       Top             =   4950
  159.       Width           =   1215
  160.    End
  161.    Begin VB.Label Label1 
  162.       Caption         =   "Level"
  163.       Height          =   255
  164.       Index           =   3
  165.       Left            =   7080
  166.       TabIndex        =   15
  167.       Top             =   4590
  168.       Width           =   1215
  169.    End
  170.    Begin MSComDlg.CommonDialog LoadDialog 
  171.       Left            =   5880
  172.       Top             =   5280
  173.       _version        =   65536
  174.       _extentx        =   847
  175.       _extenty        =   847
  176.       _stockprops     =   0
  177.       cancelerror     =   -1  'True
  178.    End
  179.    Begin VB.Label Label1 
  180.       Caption         =   "Phi"
  181.       Height          =   255
  182.       Index           =   2
  183.       Left            =   3240
  184.       TabIndex        =   11
  185.       Top             =   5400
  186.       Width           =   375
  187.    End
  188.    Begin VB.Label Label1 
  189.       Caption         =   "Theta"
  190.       Height          =   255
  191.       Index           =   1
  192.       Left            =   1440
  193.       TabIndex        =   9
  194.       Top             =   5400
  195.       Width           =   495
  196.    End
  197.    Begin VB.Label Label1 
  198.       Caption         =   "R"
  199.       Height          =   255
  200.       Index           =   0
  201.       Left            =   240
  202.       TabIndex        =   1
  203.       Top             =   5400
  204.       Width           =   255
  205.    End
  206.    Begin VB.Menu mnuFile 
  207.       Caption         =   "&File"
  208.       Begin VB.Menu mnuFileLoad 
  209.          Caption         =   "&Load..."
  210.          Shortcut        =   ^L
  211.       End
  212.       Begin VB.Menu mnuFileSaveAs 
  213.          Caption         =   "&Save As..."
  214.          Shortcut        =   ^A
  215.       End
  216.       Begin VB.Menu mnuFileSep 
  217.          Caption         =   "-"
  218.       End
  219.       Begin VB.Menu mnuFileExit 
  220.          Caption         =   "E&xit"
  221.       End
  222.    End
  223. Attribute VB_Name = "FractalForm"
  224. Attribute VB_Creatable = False
  225. Attribute VB_Exposed = False
  226. Option Explicit
  227. ' Location of viewing eye.
  228. Dim EyeR As Single
  229. Dim EyeTheta As Single
  230. Dim EyePhi As Single
  231. Const Dtheta = PI / 20
  232. Const Dphi = PI / 20
  233. Const Dr = 1
  234. ' Location of focus point.
  235. Const FocusX = 0#
  236. Const FocusY = 0#
  237. Const FocusZ = 0#
  238. Dim Projector(1 To 4, 1 To 4) As Single
  239. Dim ThePicture As ObjPicture
  240. Dim TheGrid As ObjFractalGrid
  241. Dim ShowingParameters As Boolean
  242. Dim ChoiceNum As Integer
  243. ' ************************************************
  244. ' Turn hidden surfaces on or off.
  245. ' ************************************************
  246. Private Sub ShowHiddenCheck_Click()
  247.     TheGrid.ShowHidden = (ShowHiddenCheck.value = vbChecked)
  248.     DrawData Pict
  249.     Pict.SetFocus
  250. End Sub
  251. ' *******************************************************
  252. ' Rotate the points in the cube and draw the cube.
  253. ' *******************************************************
  254. Private Sub DrawData(pic As Object)
  255. Dim x As Single
  256. Dim y As Single
  257. Dim z As Single
  258. Dim S(1 To 4, 1 To 4) As Single
  259. Dim t(1 To 4, 1 To 4) As Single
  260. Dim ST(1 To 4, 1 To 4) As Single
  261. Dim PST(1 To 4, 1 To 4) As Single
  262.     MousePointer = vbHourglass
  263.     Refresh
  264.     ' Prevent overflow errors when drawing lines
  265.     ' too far out of bounds.
  266.     On Error Resume Next
  267.     ' Scale and translate so it looks OK in pixels.
  268.     m3Scale S, 35, -35, 1
  269.     m3Translate t, 230, 175, 0
  270.     m3MatMultiplyFull ST, S, t
  271.     m3MatMultiplyFull PST, Projector, ST
  272.     ' Transform the points.
  273.     ThePicture.ApplyFull PST
  274.     ' Display the data.
  275.     pic.Cls
  276.     ThePicture.Draw pic, EyeR
  277.     pic.Refresh
  278.     ' Display the viewnig parameters.
  279.     ShowViewingParameters
  280.     MousePointer = vbDefault
  281. End Sub
  282. Sub ShowViewingParameters()
  283.     ShowingParameters = True
  284.     RText.Text = Format$(EyeR, "0.0000")
  285.     ThetaText.Text = Format$(EyeTheta, "0.0000")
  286.     PhiText.Text = Format$(EyePhi, "0.0000")
  287.     RText.Refresh
  288.     ThetaText.Refresh
  289.     PhiText.Refresh
  290.     ShowingParameters = False
  291. End Sub
  292. Private Sub Choice_Click(Index As Integer)
  293.     ChoiceNum = Index
  294.     CreateData (ShowAxesCheck.value = vbChecked)
  295.     DrawData Pict
  296.     Pict.SetFocus
  297. End Sub
  298. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  299.     Select Case KeyCode
  300.         Case vbKeyLeft
  301.             EyeTheta = EyeTheta - Dtheta
  302.         
  303.         Case vbKeyRight
  304.             EyeTheta = EyeTheta + Dtheta
  305.         
  306.         Case vbKeyUp
  307.             EyePhi = EyePhi - Dphi
  308.         
  309.         Case vbKeyDown
  310.             EyePhi = EyePhi + Dphi
  311.                 
  312.         Case Else
  313.             Exit Sub
  314.     End Select
  315.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  316.     DrawData Pict
  317. End Sub
  318. Private Sub Form_KeyPress(KeyAscii As Integer)
  319.     Select Case KeyAscii
  320.         Case Asc("+")
  321.             EyeR = EyeR + Dr
  322.         
  323.         Case Asc("-")
  324.             EyeR = EyeR - Dr
  325.         
  326.         Case Else
  327.             Exit Sub
  328.     End Select
  329.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  330.     DrawData Pict
  331. End Sub
  332. Private Sub Form_Load()
  333.     Randomize
  334.     ' Initialize the eye position.
  335.     EyeR = 10
  336.     EyeTheta = PI * 0.2
  337.     EyePhi = PI * 0.1
  338.     ' Initialize the projection transformation.
  339.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  340.     ' Create the data.
  341.     CreateData (ShowAxesCheck.value = vbChecked)
  342.     ' Project and draw the data.
  343.     Me.Show
  344.     DrawData Pict
  345. End Sub
  346. ' ************************************************
  347. ' Create the surface.
  348. ' ************************************************
  349. Sub CreateData(show_axes As Boolean)
  350. Const Xmin = -5
  351. Const Zmin = -5
  352. Const Dx = 1
  353. Const Dz = 1
  354. Const NumX = -2 * Xmin / Dx
  355. Const NumZ = -2 * Zmin / Dz
  356. Const Amp = 0.25
  357. Const Per = 2 * PI / 4
  358. Const Amp2 = 1
  359. Const Per2 = 2 * PI / 16
  360. Const Amp3 = 2
  361. Dim axis As ObjPolyline
  362. Dim i As Integer
  363. Dim j As Integer
  364. Dim x As Single
  365. Dim y As Single
  366. Dim z As Single
  367. Dim D As Single
  368. Dim d2 As Single
  369. Dim R2 As Single
  370. Dim x1 As Single
  371. Dim z1 As Single
  372. Dim x2 As Single
  373. Dim z2 As Single
  374. Dim level As Integer
  375. Dim Dy As Single
  376.     MousePointer = vbHourglass
  377.     Refresh
  378.     Set ThePicture = New ObjPicture
  379.     Set TheGrid = New ObjFractalGrid
  380.     TheGrid.SetBounds Xmin, Dx, NumX, Zmin, Dz, NumZ
  381.     ThePicture.objects.Add TheGrid
  382.     TheGrid.ShowHidden = (ShowHiddenCheck.value = vbChecked)
  383.     If show_axes Then
  384.         Set axis = New ObjPolyline
  385.         ThePicture.objects.Add axis
  386.         axis.AddSegment 0, 0, 0, 5.5, 0, 0
  387.         axis.AddSegment 0, 0, 0, 0, 3, 0
  388.         axis.AddSegment 0, 0, 0, 0, 0, 5.5
  389.     End If
  390.     R2 = (Xmin + 3 * Dx) * (Xmin + 3 * Dx)
  391.     x1 = PI * Rnd
  392.     x = Xmin
  393.     For i = 1 To NumX
  394.         z = Zmin
  395.         For j = 1 To NumZ
  396.             Select Case ChoiceNum
  397.                 Case 0  ' Mountain.
  398.                     x1 = x + 0.5
  399.                     D = 2 * (Amp3 - Sqr(x1 * x1 + z * z))
  400.                     x2 = x - 0.5
  401.                     d2 = 2 * (Amp3 - Sqr(x2 * x2 + z * z)) - 0.5
  402.                     If D < d2 Then D = d2
  403.                     If D < -Amp3 Then D = -Amp3
  404.                     y = D
  405.                 
  406.                 Case 1  ' Hill.
  407.                     R2 = 9
  408.                     D = x * x + z * z
  409.                     If D >= R2 Then
  410.                         y = 0
  411.                     ElseIf z < 0 Then
  412.                         y = 0.75 * Sqr(R2 - D)
  413.                     Else
  414.                         y = 0.75 * Sqr(R2 - D) * (3 - z) / 3
  415.                     End If
  416.                 
  417.                 Case 2  ' Ridge.
  418.                     y = 2 * Cos(2 * PI / 10 * z) * (5 - Abs(z)) / 5 + 0.5 * Rnd
  419.                 Case 3  ' Peaked Ridge.
  420.                     y = 2 * Cos(2 * PI / 10 * z) * (5 - Abs(z)) / 5 + 0.25 * Sin(2 * x + x1) + 0.25 * Sin(1# * x + x1) + 0.5 * Rnd
  421.                 
  422.                 Case 4  ' Rugged ridge.
  423.                     y = 2 * Cos(2 * PI / 10 * z) * (5 - Abs(z)) / 5 + Rnd
  424.                     If i = 1 Or i = NumX Or j = 1 Or j = NumX Then
  425.                         ' Keep the edges down a bit.
  426.                         y = y - 0.5 * Rnd
  427.                     End If
  428.             
  429.                 Case 5  ' Random.
  430.                     y = Rnd
  431.                             
  432.             End Select
  433.             
  434.             TheGrid.SetValue x, y, z
  435.             z = z + Dz
  436.         Next j
  437.         x = x + Dx
  438.     Next i
  439.     If Not IsNumeric(LevelText.Text) Then _
  440.         LevelText.Text = "4"
  441.     level = CInt(LevelText.Text)
  442.     If Not IsNumeric(DyText.Text) Then _
  443.         DyText.Text = "0.25"
  444.     Dy = CSng(DyText.Text)
  445.     TheGrid.GenerateSurface level, Dy
  446.     MousePointer = vbDefault
  447. End Sub
  448. Private Sub mnuFileExit_Click()
  449.     Unload Me
  450. End Sub
  451. Private Sub mnuFileLoad_Click()
  452. Dim fname As String
  453. Dim filenum As Integer
  454. Dim txt As String
  455. Dim Xmin As Single
  456. Dim ymin As Single
  457. Dim Xmax As Single
  458. Dim ymax As Single
  459.     ' Allow the user to pick a file.
  460.     On Error Resume Next
  461.     LoadDialog.filename = "*.APF"
  462.     LoadDialog.ShowOpen
  463.     If Err.Number = cdlCancel Then
  464.         Unload LoadDialog
  465.         Exit Sub
  466.     ElseIf Err.Number <> 0 Then
  467.         Unload LoadDialog
  468.         Beep
  469.         MsgBox "Error selecting file.", , vbExclamation
  470.         Exit Sub
  471.     End If
  472.     On Error GoTo 0
  473.     fname = LoadDialog.filename
  474.     LoadDialog.InitDir = Left$(fname, Len(fname) _
  475.         - Len(LoadDialog.FileTitle) - 1)
  476.     ' Clear the picture.
  477.     Set ThePicture = Nothing
  478.     ' Open the file.
  479.     filenum = FreeFile
  480.     Open fname For Input As #filenum
  481.     ' Make sure it's an Object Picture File.
  482.     Input #filenum, txt
  483.     If txt <> "3D APF PICTURE" Then
  484.         Close filenum
  485.         Beep
  486.         MsgBox "Error reading file """ & fname & """.", , vbExclamation
  487.         Exit Sub
  488.     End If
  489.     ' Read the picture.
  490.     Set ThePicture = New ObjPicture
  491.     ThePicture.FileInput filenum
  492.     If ThePicture.objects(1).ObjectType = "FRACTALGRID" Then
  493.         Set TheGrid = ThePicture.objects(1)
  494.         TheGrid.ShowHidden = (ShowHiddenCheck.value = vbChecked)
  495.     End If
  496.     ' Close the file.
  497.     Close filenum
  498.     ' Refresh the display.
  499.     DrawData Pict
  500.     ' Deselect all the option buttons.
  501.     For ChoiceNum = 0 To 5
  502.         If Choice(ChoiceNum).value Then _
  503.             Choice(ChoiceNum).value = False
  504.     Next ChoiceNum
  505. End Sub
  506. Private Sub mnuFileSaveAs_Click()
  507. Dim fname As String
  508. Dim filenum As Integer
  509.     ' Allow the user to pick a file.
  510.     On Error Resume Next
  511.     LoadDialog.filename = "*.APF"
  512.     LoadDialog.Flags = cdlOFNOverwritePrompt + cdlOFNHideReadOnly
  513.     LoadDialog.ShowSave
  514.     If Err.Number = cdlCancel Then
  515.         Unload LoadDialog
  516.         Exit Sub
  517.     ElseIf Err.Number <> 0 Then
  518.         Unload LoadDialog
  519.         Beep
  520.         MsgBox "Error selecting file.", , vbExclamation
  521.         Exit Sub
  522.     End If
  523.     On Error GoTo 0
  524.     fname = LoadDialog.filename
  525.     LoadDialog.InitDir = Left$(fname, Len(fname) _
  526.         - Len(LoadDialog.FileTitle) - 1)
  527.     ' Open the file.
  528.     MousePointer = vbHourglass
  529.     DoEvents
  530.     filenum = FreeFile
  531.     Open fname For Output As #filenum
  532.     ' Write the picture.
  533.     ThePicture.FileWrite filenum
  534.     ' Close the file.
  535.     Close filenum
  536.     MousePointer = vbDefault
  537. End Sub
  538. Private Sub PhiText_Change()
  539.     If ShowingParameters Then Exit Sub
  540.     EyePhi = CSng(PhiText.Text)
  541.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  542.     DrawData Pict
  543. End Sub
  544. Private Sub RText_Change()
  545.     If ShowingParameters Then Exit Sub
  546.     EyeR = CSng(RText.Text)
  547.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  548.     DrawData Pict
  549. End Sub
  550. Private Sub ShowAxesCheck_Click()
  551.     CreateData (ShowAxesCheck.value = vbChecked)
  552.     DrawData Pict
  553.     Pict.SetFocus
  554. End Sub
  555. Private Sub ThetaText_Change()
  556.     If ShowingParameters Then Exit Sub
  557.     EyeTheta = CSng(ThetaText.Text)
  558.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  559.     DrawData Pict
  560. End Sub
  561.