home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH9 / SRC / DISTORT.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-05-01  |  6.0 KB  |  200 lines

  1. VERSION 4.00
  2. Begin VB.Form DistortForm 
  3.    Caption         =   "Distort"
  4.    ClientHeight    =   5925
  5.    ClientLeft      =   1710
  6.    ClientTop       =   750
  7.    ClientWidth     =   5910
  8.    Height          =   6615
  9.    KeyPreview      =   -1  'True
  10.    Left            =   1650
  11.    LinkTopic       =   "Form1"
  12.    ScaleHeight     =   5925
  13.    ScaleWidth      =   5910
  14.    Top             =   120
  15.    Width           =   6030
  16.    Begin VB.PictureBox Canvas 
  17.       AutoRedraw      =   -1  'True
  18.       Height          =   5895
  19.       Left            =   0
  20.       ScaleHeight     =   -800
  21.       ScaleLeft       =   -400
  22.       ScaleMode       =   0  'User
  23.       ScaleTop        =   400
  24.       ScaleWidth      =   800
  25.       TabIndex        =   0
  26.       Top             =   0
  27.       Width           =   5895
  28.    End
  29.    Begin VB.Menu mnuFile 
  30.       Caption         =   "&File"
  31.       Begin VB.Menu mnuFileExit 
  32.          Caption         =   "E&xit"
  33.       End
  34.    End
  35.    Begin VB.Menu mnuTrans 
  36.       Caption         =   "&Transformation"
  37.       Begin VB.Menu mnuTransChoice 
  38.          Caption         =   "&None"
  39.          Checked         =   -1  'True
  40.          Index           =   0
  41.          Shortcut        =   ^N
  42.       End
  43.       Begin VB.Menu mnuTransChoice 
  44.          Caption         =   "&Sines"
  45.          Index           =   1
  46.          Shortcut        =   ^S
  47.       End
  48.       Begin VB.Menu mnuTransChoice 
  49.          Caption         =   "&Twist"
  50.          Index           =   2
  51.          Shortcut        =   ^T
  52.       End
  53.       Begin VB.Menu mnuTransChoice 
  54.          Caption         =   "&Circle"
  55.          Index           =   3
  56.          Shortcut        =   ^C
  57.       End
  58.    End
  59. Attribute VB_Name = "DistortForm"
  60. Attribute VB_Creatable = False
  61. Attribute VB_Exposed = False
  62. Option Explicit
  63. Dim ThePicture As ObjPicture
  64. ' Location of viewing eye.
  65. Dim EyeR As Single
  66. Dim EyeTheta As Single
  67. Dim EyePhi As Single
  68. ' Location of focus point.
  69. Const FocusX = 0#
  70. Const FocusY = 0#
  71. Const FocusZ = 0#
  72. Dim Projector(1 To 4, 1 To 4) As Single
  73. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  74. Const Dtheta = PI / 20
  75.     Select Case KeyCode
  76.         Case vbKeyLeft
  77.             EyeTheta = EyeTheta - Dtheta
  78.         
  79.         Case vbKeyRight
  80.             EyeTheta = EyeTheta + Dtheta
  81.         
  82.         Case vbKeyUp
  83.             EyePhi = EyePhi - Dtheta
  84.         
  85.         Case vbKeyDown
  86.             EyePhi = EyePhi + Dtheta
  87.         
  88.         Case Else
  89.             Exit Sub
  90.     End Select
  91.     MousePointer = vbHourglass
  92.     DoEvents
  93.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  94.     DrawData canvas
  95.     MousePointer = vbDefault
  96. End Sub
  97. Private Sub Form_Load()
  98.     Me.Show
  99.     MousePointer = vbHourglass
  100.     DoEvents
  101.     ' Initialize the eye position.
  102.     EyeR = 1500
  103.     EyeTheta = PI * 0.17
  104.     EyePhi = PI * 0.16
  105.     ' Create the data.
  106.     CreateData
  107.     ' Display the data.
  108.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  109.     DrawData canvas
  110.     MousePointer = vbDefault
  111. End Sub
  112. Private Sub mnuFileExit_Click()
  113.     Unload Me
  114. End Sub
  115. ' ***********************************************
  116. ' Create some polylines to display.
  117. ' ***********************************************
  118. Sub CreateData()
  119. Const GAP = 25
  120. Dim i As Single
  121. Dim j As Single
  122. Dim poly As ObjPolyline
  123.     ' Create the picture object.
  124.     Set ThePicture = New ObjPicture
  125.     ' Create the top (perpendicular to Y axis).
  126.     Set poly = New ObjPolyline
  127.     ThePicture.objects.Add poly
  128.     For i = -200 To 200 Step GAP
  129.         For j = -200 To 200 - GAP Step GAP
  130.             poly.AddSegment i, 200, j, i, 200, j + GAP
  131.             poly.AddSegment j, 200, i, j + GAP, 200, i
  132.         Next j
  133.     Next i
  134.     ' Create the front (perpendicular to Z axis).
  135.     Set poly = New ObjPolyline
  136.     ThePicture.objects.Add poly
  137.     For i = -200 To 200 Step GAP
  138.         For j = -200 To 200 - GAP Step GAP
  139.             poly.AddSegment i, j, 200, i, j + GAP, 200
  140.             poly.AddSegment j, i, 200, j + GAP, i, 200
  141.         Next j
  142.     Next i
  143.     ' Create the side (perpendicular to X axis).
  144.     Set poly = New ObjPolyline
  145.     ThePicture.objects.Add poly
  146.     For i = -200 To 200 Step GAP
  147.         For j = -200 To 200 - GAP Step GAP
  148.             poly.AddSegment 200, i, j, 200, i, j + GAP
  149.             poly.AddSegment 200, j, i, 200, j + GAP, i
  150.         Next j
  151.     Next i
  152. End Sub
  153. ' ***********************************************
  154. ' Display the data.
  155. ' ***********************************************
  156. Private Sub DrawData(pic As Object)
  157.     ' Transform the points.
  158.     ThePicture.ApplyFull Projector
  159.     ' Draw the points.
  160.     pic.Cls
  161.     ThePicture.Draw pic
  162.     pic.Refresh
  163. End Sub
  164. Private Sub mnuTransChoice_Click(Index As Integer)
  165. Dim trans As Object
  166. Dim i As Integer
  167.     MousePointer = vbHourglass
  168.     DoEvents
  169.     ' Check the selected transformation.
  170.     For i = 0 To 3
  171.         mnuTransChoice(i).Checked = False
  172.     Next i
  173.     mnuTransChoice(Index).Checked = True
  174.     ' Reload the data.
  175.     CreateData
  176.     ' Load the correct transformation.
  177.     Select Case Index
  178.         Case 1  ' Sines.
  179.             Set trans = New DistortSines
  180.             trans.amplitude = 30
  181.             trans.period = 150
  182.         Case 2  ' Twist.
  183.             Set trans = New DistortTwist
  184.             trans.Cx = 0
  185.             trans.Cz = 0
  186.             trans.offset = -100
  187.             trans.period = 500 * PI
  188.         Case 3  ' Circle.
  189.             Set trans = New DistortCircle
  190.             trans.amplitude = 12.5
  191.             trans.period = 150
  192.     End Select
  193.     ' If the transformation is not none, apply it.
  194.     If Index > 0 Then ThePicture.Distort trans
  195.     ' Redraw the picture.
  196.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  197.     DrawData canvas
  198.     MousePointer = vbDefault
  199. End Sub
  200.