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

  1. VERSION 4.00
  2. Begin VB.Form RotatedForm 
  3.    Appearance      =   0  'Flat
  4.    BackColor       =   &H00C0C0C0&
  5.    Caption         =   "Surfaces of Rotation"
  6.    ClientHeight    =   5700
  7.    ClientLeft      =   690
  8.    ClientTop       =   900
  9.    ClientWidth     =   7830
  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            =   630
  23.    LinkTopic       =   "Form1"
  24.    ScaleHeight     =   380
  25.    ScaleMode       =   3  'Pixel
  26.    ScaleWidth      =   522
  27.    Top             =   270
  28.    Width           =   7950
  29.    Begin VB.Frame Frame2 
  30.       Caption         =   "Curve"
  31.       Height          =   5295
  32.       Left            =   0
  33.       TabIndex        =   8
  34.       Top             =   0
  35.       Width           =   2295
  36.       Begin VB.OptionButton CurveChoice 
  37.          Caption         =   "Tornado"
  38.          Height          =   255
  39.          Index           =   13
  40.          Left            =   120
  41.          TabIndex        =   22
  42.          Top             =   4920
  43.          Width           =   2055
  44.       End
  45.       Begin VB.OptionButton CurveChoice 
  46.          Caption         =   "Helix"
  47.          Height          =   255
  48.          Index           =   12
  49.          Left            =   120
  50.          TabIndex        =   21
  51.          Top             =   4560
  52.          Width           =   2055
  53.       End
  54.       Begin VB.OptionButton CurveChoice 
  55.          Caption         =   "Tower"
  56.          Height          =   255
  57.          Index           =   11
  58.          Left            =   120
  59.          TabIndex        =   20
  60.          Top             =   4200
  61.          Width           =   2055
  62.       End
  63.       Begin VB.OptionButton CurveChoice 
  64.          Caption         =   "Football"
  65.          Height          =   255
  66.          Index           =   10
  67.          Left            =   120
  68.          TabIndex        =   19
  69.          Top             =   3840
  70.          Width           =   2055
  71.       End
  72.       Begin VB.OptionButton CurveChoice 
  73.          Caption         =   "Goblet"
  74.          Height          =   255
  75.          Index           =   9
  76.          Left            =   120
  77.          TabIndex        =   18
  78.          Top             =   3480
  79.          Width           =   2055
  80.       End
  81.       Begin VB.OptionButton CurveChoice 
  82.          Caption         =   "Urn"
  83.          Height          =   255
  84.          Index           =   8
  85.          Left            =   120
  86.          TabIndex        =   17
  87.          Top             =   3120
  88.          Width           =   2055
  89.       End
  90.       Begin VB.OptionButton CurveChoice 
  91.          Caption         =   "Sine Wave"
  92.          Height          =   255
  93.          Index           =   7
  94.          Left            =   120
  95.          TabIndex        =   16
  96.          Top             =   2760
  97.          Width           =   2055
  98.       End
  99.       Begin VB.OptionButton CurveChoice 
  100.          Caption         =   "Semicircle 2"
  101.          Height          =   255
  102.          Index           =   6
  103.          Left            =   120
  104.          TabIndex        =   15
  105.          Top             =   2400
  106.          Width           =   2055
  107.       End
  108.       Begin VB.OptionButton CurveChoice 
  109.          Caption         =   "Semicircle 1"
  110.          Height          =   255
  111.          Index           =   5
  112.          Left            =   120
  113.          TabIndex        =   14
  114.          Top             =   2040
  115.          Width           =   2055
  116.       End
  117.       Begin VB.OptionButton CurveChoice 
  118.          Caption         =   "Circle 2"
  119.          Height          =   255
  120.          Index           =   4
  121.          Left            =   120
  122.          TabIndex        =   13
  123.          Top             =   1680
  124.          Width           =   2055
  125.       End
  126.       Begin VB.OptionButton CurveChoice 
  127.          Caption         =   "Circle 1"
  128.          Height          =   255
  129.          Index           =   3
  130.          Left            =   120
  131.          TabIndex        =   12
  132.          Top             =   1320
  133.          Width           =   2055
  134.       End
  135.       Begin VB.OptionButton CurveChoice 
  136.          Caption         =   "3/4 Rectangle"
  137.          Height          =   255
  138.          Index           =   2
  139.          Left            =   120
  140.          TabIndex        =   11
  141.          Top             =   960
  142.          Width           =   2055
  143.       End
  144.       Begin VB.OptionButton CurveChoice 
  145.          Caption         =   "Diamond"
  146.          Height          =   255
  147.          Index           =   1
  148.          Left            =   120
  149.          TabIndex        =   10
  150.          Top             =   600
  151.          Width           =   2055
  152.       End
  153.       Begin VB.OptionButton CurveChoice 
  154.          Caption         =   "Rectangle"
  155.          Height          =   255
  156.          Index           =   0
  157.          Left            =   120
  158.          TabIndex        =   9
  159.          Top             =   240
  160.          Value           =   -1  'True
  161.          Width           =   2055
  162.       End
  163.    End
  164.    Begin VB.CheckBox ShowAxesCheck 
  165.       Caption         =   "Show Axes"
  166.       Height          =   255
  167.       Left            =   2400
  168.       TabIndex        =   7
  169.       Top             =   5400
  170.       Width           =   1335
  171.    End
  172.    Begin VB.TextBox PhiText 
  173.       Height          =   285
  174.       Left            =   6960
  175.       TabIndex        =   6
  176.       Text            =   "0.1570"
  177.       Top             =   5400
  178.       Width           =   855
  179.    End
  180.    Begin VB.TextBox ThetaText 
  181.       Height          =   285
  182.       Left            =   5640
  183.       TabIndex        =   4
  184.       Text            =   "0.6283"
  185.       Top             =   5400
  186.       Width           =   855
  187.    End
  188.    Begin VB.TextBox RText 
  189.       Height          =   285
  190.       Left            =   4080
  191.       TabIndex        =   2
  192.       Text            =   "10"
  193.       Top             =   5400
  194.       Width           =   855
  195.    End
  196.    Begin VB.PictureBox Pict 
  197.       AutoRedraw      =   -1  'True
  198.       Height          =   5295
  199.       Left            =   2400
  200.       ScaleHeight     =   349
  201.       ScaleMode       =   3  'Pixel
  202.       ScaleWidth      =   357
  203.       TabIndex        =   0
  204.       Top             =   0
  205.       Width           =   5415
  206.    End
  207.    Begin MSComDlg.CommonDialog LoadDialog 
  208.       Left            =   1800
  209.       Top             =   5280
  210.       _version        =   65536
  211.       _extentx        =   847
  212.       _extenty        =   847
  213.       _stockprops     =   0
  214.       cancelerror     =   -1  'True
  215.    End
  216.    Begin VB.Label Label1 
  217.       Caption         =   "Phi"
  218.       Height          =   255
  219.       Index           =   2
  220.       Left            =   6600
  221.       TabIndex        =   5
  222.       Top             =   5415
  223.       Width           =   375
  224.    End
  225.    Begin VB.Label Label1 
  226.       Caption         =   "Theta"
  227.       Height          =   255
  228.       Index           =   1
  229.       Left            =   5040
  230.       TabIndex        =   3
  231.       Top             =   5415
  232.       Width           =   495
  233.    End
  234.    Begin VB.Label Label1 
  235.       Caption         =   "R"
  236.       Height          =   255
  237.       Index           =   0
  238.       Left            =   3840
  239.       TabIndex        =   1
  240.       Top             =   5415
  241.       Width           =   255
  242.    End
  243.    Begin VB.Menu mnuFile 
  244.       Caption         =   "&File"
  245.       Begin VB.Menu mnuFileLoad 
  246.          Caption         =   "&Load..."
  247.          Shortcut        =   ^L
  248.       End
  249.       Begin VB.Menu mnuFileSaveAs 
  250.          Caption         =   "&Save As..."
  251.          Shortcut        =   ^A
  252.       End
  253.       Begin VB.Menu mnuFileSep 
  254.          Caption         =   "-"
  255.       End
  256.       Begin VB.Menu mnuFileExit 
  257.          Caption         =   "E&xit"
  258.       End
  259.    End
  260. Attribute VB_Name = "RotatedForm"
  261. Attribute VB_Creatable = False
  262. Attribute VB_Exposed = False
  263. Option Explicit
  264. ' Location of viewing eye.
  265. Dim EyeR As Single
  266. Dim EyeTheta As Single
  267. Dim EyePhi As Single
  268. Const dtheta = PI / 20
  269. Const Dphi = PI / 20
  270. Const Dr = 1
  271. ' Location of focus point.
  272. Const FocusX = 0#
  273. Const FocusY = 0#
  274. Const FocusZ = 0#
  275. Dim Projector(1 To 4, 1 To 4) As Single
  276. Dim CurveNum As Integer
  277. Dim ThePicture As ObjPicture
  278. Dim TheSurface As ObjRotated
  279. Dim ShowingParameters As Boolean
  280. ' ************************************************
  281. ' Create the selected curve.
  282. ' ************************************************
  283. Sub CreateCurve()
  284. Dim r As Single
  285. Dim offset As Single
  286. Dim dtheta As Single
  287. Dim theta As Single
  288. Dim y As Single
  289.     Select Case CurveNum
  290.         Case 0  ' Rectangle.
  291.             TheSurface.AddCurvePoint -3, -1.5, 0
  292.             TheSurface.AddCurvePoint -3, 1.5, 0
  293.             TheSurface.AddCurvePoint -1, 1.5, 0
  294.             TheSurface.AddCurvePoint -1, -1.5, 0
  295.             TheSurface.AddCurvePoint -3, -1.5, 0
  296.         Case 1  ' Diamond.
  297.             TheSurface.AddCurvePoint -3, 0, 0
  298.             TheSurface.AddCurvePoint -2, -1, 0
  299.             TheSurface.AddCurvePoint -1, 0, 0
  300.             TheSurface.AddCurvePoint -2, 1, 0
  301.             TheSurface.AddCurvePoint -3, 0, 0
  302.         
  303.         Case 2  ' 3/4 Rectangle.
  304.             TheSurface.AddCurvePoint 0, -1.5, 0
  305.             TheSurface.AddCurvePoint -3, -1.5, 0
  306.             TheSurface.AddCurvePoint -3, 1.5, 0
  307.             TheSurface.AddCurvePoint 0, 1.5, 0
  308.         
  309.         Case 3, 4   ' Circle 1, circle 2.
  310.             If CurveNum = 3 Then
  311.                 r = 2
  312.                 offset = 2
  313.             Else
  314.                 r = 1.5
  315.                 offset = 2.5
  316.             End If
  317.             dtheta = PI / 8
  318.             TheSurface.AddCurvePoint offset + r, 0, 0
  319.             For theta = dtheta To 2 * PI - dtheta + 0.1 Step dtheta
  320.                 TheSurface.AddCurvePoint _
  321.                     offset + r * Cos(theta), r * Sin(theta), 0
  322.             Next theta
  323.             TheSurface.AddCurvePoint offset + r, 0, 0
  324.         
  325.         Case 5, 6   ' Semicircle 1, semicircle 2.
  326.             If CurveNum = 5 Then
  327.                 r = 4
  328.                 offset = 0
  329.             Else
  330.                 r = 2
  331.                 offset = 2
  332.             End If
  333.             dtheta = PI / 8
  334.             TheSurface.AddCurvePoint offset, -r, 0
  335.             For theta = -PI / 2 + dtheta To PI / 2 - dtheta + 0.1 Step dtheta
  336.                 TheSurface.AddCurvePoint _
  337.                     offset + r * Cos(theta), _
  338.                     r * Sin(theta), _
  339.                     0
  340.             Next theta
  341.             TheSurface.AddCurvePoint offset, r, 0
  342.             
  343.         Case 7  ' Sine wave.
  344.             r = 0.7
  345.             dtheta = PI / 10
  346.             For theta = -PI To PI Step dtheta
  347.                 TheSurface.AddCurvePoint _
  348.                     1 + r + r * Sin(2 * theta), _
  349.                     theta, _
  350.                     0
  351.             Next theta
  352.             
  353.         Case 8  ' Urn.
  354.             dtheta = PI / 10
  355.             For theta = -PI To PI Step dtheta
  356.                 TheSurface.AddCurvePoint _
  357.                     PI / 2 + (-PI + theta) / 4 * Sin(2 * theta), _
  358.                     theta, _
  359.                     0
  360.             Next theta
  361.             
  362.         Case 9  ' Goblet.
  363.             TheSurface.AddCurvePoint 3, 3.5, 0
  364.             TheSurface.AddCurvePoint 2.5, 3, 0
  365.             TheSurface.AddCurvePoint 3, 1.5, 0
  366.             TheSurface.AddCurvePoint 2.5, 1, 0
  367.             TheSurface.AddCurvePoint 1, 1, 0
  368.             TheSurface.AddCurvePoint 0.5, 0.5, 0
  369.             TheSurface.AddCurvePoint 0.5, -1, 0
  370.             TheSurface.AddCurvePoint 1, -1.5, 0
  371.             TheSurface.AddCurvePoint 2, -1.5, 0
  372.             TheSurface.AddCurvePoint 2.5, -2, 0
  373.         
  374.         Case 10 ' Football.
  375.             For y = -4 To 4 Step 0.5
  376.                 TheSurface.AddCurvePoint 16 / 5 - y * y / 5, y, 0
  377.             Next y
  378.         
  379.         Case 11 ' Tower.
  380.             r = 1
  381.             dtheta = PI / 8
  382.             For theta = -PI To -PI / 2 Step dtheta
  383.                 TheSurface.AddCurvePoint _
  384.                     r + r * Cos(theta), _
  385.                     4 * r + r * Sin(theta), _
  386.                     0
  387.             Next theta
  388.             For theta = PI / 2 To -PI / 2 Step -dtheta
  389.                 TheSurface.AddCurvePoint _
  390.                     r + r * Cos(theta), _
  391.                     2 * r + r * Sin(theta), _
  392.                     0
  393.             Next theta
  394.             TheSurface.AddCurvePoint r, -3, 0
  395.         
  396.         Case 12 ' Helix.
  397.             r = 2
  398.             dtheta = PI / 4
  399.             For theta = -PI To PI Step dtheta
  400.                 TheSurface.AddCurvePoint _
  401.                     r * Cos(theta / 2), _
  402.                     theta, _
  403.                     r * Sin(theta / 2)
  404.             Next theta
  405.         
  406.         Case 13 ' Tornado.
  407.             r = 2
  408.             dtheta = PI / 4
  409.             For theta = -PI To PI Step dtheta
  410.                 r = 2 + theta / 2
  411.                 TheSurface.AddCurvePoint _
  412.                     r * Cos(theta / 2), _
  413.                     theta, _
  414.                     r * Sin(theta / 2)
  415.             Next theta
  416.     End Select
  417. End Sub
  418. Sub WaitEnd()
  419.     MousePointer = vbDefault
  420. End Sub
  421. Sub WaitStart()
  422.     MousePointer = vbHourglass
  423.     DoEvents
  424. End Sub
  425. ' ************************************************
  426. ' Create a new curve and rotate it.
  427. ' ************************************************
  428. Private Sub CurveChoice_Click(Index As Integer)
  429. Dim pline As ObjPolyline
  430.     WaitStart
  431.     Set ThePicture = New ObjPicture
  432.     Set TheSurface = New ObjRotated
  433.     ThePicture.objects.Add TheSurface
  434.     CurveNum = Index
  435.     CreateCurve
  436.     TheSurface.Rotate
  437.     If ShowAxesCheck.value = vbChecked Then
  438.         Set pline = New ObjPolyline
  439.         ThePicture.objects.Add pline
  440.         pline.AddSegment 0, 0, 0, 5, 0, 0
  441.         pline.AddSegment 0, 0, 0, 0, 5, 0
  442.         pline.AddSegment 0, 0, 0, 0, 0, 5
  443.     End If
  444.     DrawData Pict
  445.     Pict.SetFocus
  446. End Sub
  447. ' *******************************************************
  448. ' Rotate the points in the cube and draw the cube.
  449. ' *******************************************************
  450. Private Sub DrawData(pic As Object)
  451. Dim x As Single
  452. Dim y As Single
  453. Dim z As Single
  454. Dim S(1 To 4, 1 To 4) As Single
  455. Dim t(1 To 4, 1 To 4) As Single
  456. Dim ST(1 To 4, 1 To 4) As Single
  457. Dim PST(1 To 4, 1 To 4) As Single
  458.     MousePointer = vbHourglass
  459.     Refresh
  460.     ' Prevent overflow errors when drawing lines
  461.     ' too far out of bounds.
  462.     On Error Resume Next
  463.     ' Scale and translate so it looks OK in pixels.
  464.     m3Scale S, 35, -35, 1
  465.     m3Translate t, 180, 200, 0
  466.     m3MatMultiplyFull ST, S, t
  467.     m3MatMultiplyFull PST, Projector, ST
  468.     ' Transform the points.
  469.     ThePicture.ApplyFull PST
  470.     ' Display the data.
  471.     pic.Cls
  472.     ThePicture.Draw pic, EyeR
  473.     pic.Refresh
  474.     ' Display the viewnig parameters.
  475.     ShowViewingParameters
  476.     MousePointer = vbDefault
  477. End Sub
  478. Sub ShowViewingParameters()
  479.     ShowingParameters = True
  480.     RText.Text = Format$(EyeR, "0.0000")
  481.     ThetaText.Text = Format$(EyeTheta, "0.0000")
  482.     PhiText.Text = Format$(EyePhi, "0.0000")
  483.     RText.Refresh
  484.     ThetaText.Refresh
  485.     PhiText.Refresh
  486.     ShowingParameters = False
  487. End Sub
  488. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  489.     Select Case KeyCode
  490.         Case vbKeyLeft
  491.             EyeTheta = EyeTheta - dtheta
  492.         
  493.         Case vbKeyRight
  494.             EyeTheta = EyeTheta + dtheta
  495.         
  496.         Case vbKeyUp
  497.             EyePhi = EyePhi - Dphi
  498.         
  499.         Case vbKeyDown
  500.             EyePhi = EyePhi + Dphi
  501.                 
  502.         Case Else
  503.             Exit Sub
  504.     End Select
  505.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  506.     DrawData Pict
  507. End Sub
  508. Private Sub Form_KeyPress(KeyAscii As Integer)
  509.     Select Case KeyAscii
  510.         Case Asc("+")
  511.             EyeR = EyeR + Dr
  512.         
  513.         Case Asc("-")
  514.             EyeR = EyeR - Dr
  515.         
  516.         Case Else
  517.             Exit Sub
  518.     End Select
  519.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  520.     DrawData Pict
  521. End Sub
  522. Private Sub Form_Load()
  523.     ' Initialize the eye position.
  524.     EyeR = 10
  525.     EyeTheta = PI * 0.2
  526.     EyePhi = PI * 0.1
  527.     ' Initialize the projection transformation.
  528.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  529.     Me.Show
  530.     CurveChoice_Click 0
  531. End Sub
  532. Private Sub mnuFileExit_Click()
  533.     Unload Me
  534. End Sub
  535. Private Sub mnuFileLoad_Click()
  536. Dim fname As String
  537. Dim filenum As Integer
  538. Dim txt As String
  539. Dim Xmin As Single
  540. Dim ymin As Single
  541. Dim xmax As Single
  542. Dim ymax As Single
  543. Dim i As Integer
  544.     ' Allow the user to pick a file.
  545.     On Error Resume Next
  546.     LoadDialog.filename = "*.APF"
  547.     LoadDialog.ShowOpen
  548.     If Err.Number = cdlCancel Then
  549.         Unload LoadDialog
  550.         Exit Sub
  551.     ElseIf Err.Number <> 0 Then
  552.         Unload LoadDialog
  553.         Beep
  554.         MsgBox "Error selecting file.", , vbExclamation
  555.         Exit Sub
  556.     End If
  557.     On Error GoTo 0
  558.     fname = LoadDialog.filename
  559.     LoadDialog.InitDir = Left$(fname, Len(fname) _
  560.         - Len(LoadDialog.FileTitle) - 1)
  561.     ' Clear the picture.
  562.     Set ThePicture = Nothing
  563.     ' Open the file.
  564.     filenum = FreeFile
  565.     Open fname For Input As #filenum
  566.     ' Make sure it's an Object Picture File.
  567.     Input #filenum, txt
  568.     If txt <> "3D APF PICTURE" Then
  569.         Close filenum
  570.         Beep
  571.         MsgBox "Error reading file """ & fname & """.", , vbExclamation
  572.         Exit Sub
  573.     End If
  574.     ' Read the picture.
  575.     MousePointer = vbHourglass
  576.     DoEvents
  577.     Set ThePicture = New ObjPicture
  578.     ThePicture.FileInput filenum
  579.     ' Close the file.
  580.     Close filenum
  581.     ' Refresh the display.
  582.     DrawData Pict
  583.     ' Deselect all the option buttons.
  584.     For i = 0 To 13
  585.         If CurveChoice(i).value Then _
  586.             CurveChoice(i).value = False
  587.     Next i
  588.     MousePointer = vbDefault
  589. End Sub
  590. Private Sub mnuFileSaveAs_Click()
  591. Dim fname As String
  592. Dim filenum As Integer
  593.     ' Allow the user to pick a file.
  594.     On Error Resume Next
  595.     LoadDialog.filename = "*.APF"
  596.     LoadDialog.Flags = cdlOFNOverwritePrompt + cdlOFNHideReadOnly
  597.     LoadDialog.ShowSave
  598.     If Err.Number = cdlCancel Then
  599.         Unload LoadDialog
  600.         Exit Sub
  601.     ElseIf Err.Number <> 0 Then
  602.         Unload LoadDialog
  603.         Beep
  604.         MsgBox "Error selecting file.", , vbExclamation
  605.         Exit Sub
  606.     End If
  607.     On Error GoTo 0
  608.     fname = LoadDialog.filename
  609.     LoadDialog.InitDir = Left$(fname, Len(fname) _
  610.         - Len(LoadDialog.FileTitle) - 1)
  611.     ' Open the file.
  612.     filenum = FreeFile
  613.     Open fname For Output As #filenum
  614.     ' Write the picture.
  615.     ThePicture.FileWrite filenum
  616.     ' Close the file.
  617.     Close filenum
  618. End Sub
  619. Private Sub PhiText_Change()
  620.     If ShowingParameters Then Exit Sub
  621.     EyePhi = CSng(PhiText.Text)
  622.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  623.     DrawData Pict
  624. End Sub
  625. Private Sub RText_Change()
  626.     If ShowingParameters Then Exit Sub
  627.     EyeR = CSng(RText.Text)
  628.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  629.     DrawData Pict
  630. End Sub
  631. ' ************************************************
  632. ' Redraw with the axes on or off as appropriate.
  633. ' ************************************************
  634. Private Sub ShowAxesCheck_Click()
  635.     CurveChoice_Click CurveNum
  636. End Sub
  637. Private Sub ThetaText_Change()
  638.     If ShowingParameters Then Exit Sub
  639.     EyeTheta = CSng(ThetaText.Text)
  640.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  641.     DrawData Pict
  642. End Sub
  643.