home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH6 / SRC / BDITCH2.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-04-24  |  7.3 KB  |  254 lines

  1. VERSION 4.00
  2. Begin VB.Form BDitch2Form 
  3.    Caption         =   "Bowditch 2"
  4.    ClientHeight    =   5670
  5.    ClientLeft      =   2070
  6.    ClientTop       =   930
  7.    ClientWidth     =   4830
  8.    Height          =   6360
  9.    Left            =   2010
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   378
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   322
  14.    Top             =   300
  15.    Width           =   4950
  16.    Begin VB.PictureBox Canvas 
  17.       AutoRedraw      =   -1  'True
  18.       Height          =   4815
  19.       Left            =   0
  20.       ScaleHeight     =   -2.2
  21.       ScaleLeft       =   -1.1
  22.       ScaleMode       =   0  'User
  23.       ScaleTop        =   1.1
  24.       ScaleWidth      =   2.2
  25.       TabIndex        =   13
  26.       Top             =   840
  27.       Width           =   4815
  28.    End
  29.    Begin VB.TextBox QText 
  30.       Height          =   285
  31.       Left            =   3120
  32.       TabIndex        =   10
  33.       Text            =   "5"
  34.       Top             =   45
  35.       Width           =   615
  36.    End
  37.    Begin VB.TextBox PText 
  38.       Height          =   285
  39.       Left            =   2040
  40.       TabIndex        =   9
  41.       Text            =   "4"
  42.       Top             =   45
  43.       Width           =   615
  44.    End
  45.    Begin VB.TextBox ThetaText 
  46.       Height          =   285
  47.       Left            =   4200
  48.       TabIndex        =   7
  49.       Text            =   "30"
  50.       Top             =   480
  51.       Width           =   615
  52.    End
  53.    Begin VB.TextBox YscaleText 
  54.       Height          =   285
  55.       Left            =   2040
  56.       TabIndex        =   5
  57.       Text            =   "0.6"
  58.       Top             =   480
  59.       Width           =   615
  60.    End
  61.    Begin VB.TextBox XscaleText 
  62.       Height          =   285
  63.       Left            =   600
  64.       TabIndex        =   3
  65.       Text            =   "0.9"
  66.       Top             =   480
  67.       Width           =   615
  68.    End
  69.    Begin VB.TextBox DtText 
  70.       Height          =   285
  71.       Left            =   240
  72.       TabIndex        =   2
  73.       Text            =   "0.01"
  74.       Top             =   45
  75.       Width           =   615
  76.    End
  77.    Begin VB.CommandButton CmdGo 
  78.       Caption         =   "Go"
  79.       Default         =   -1  'True
  80.       Height          =   375
  81.       Left            =   4200
  82.       TabIndex        =   0
  83.       Top             =   0
  84.       Width           =   615
  85.    End
  86.    Begin VB.Label Label1 
  87.       Caption         =   "Q"
  88.       Height          =   255
  89.       Index           =   6
  90.       Left            =   2955
  91.       TabIndex        =   12
  92.       Top             =   60
  93.       Width           =   255
  94.    End
  95.    Begin VB.Label Label1 
  96.       Caption         =   "P"
  97.       Height          =   255
  98.       Index           =   4
  99.       Left            =   1920
  100.       TabIndex        =   11
  101.       Top             =   60
  102.       Width           =   255
  103.    End
  104.    Begin VB.Label Label1 
  105.       Caption         =   "Angle (degrees)"
  106.       Height          =   255
  107.       Index           =   5
  108.       Left            =   3000
  109.       TabIndex        =   8
  110.       Top             =   525
  111.       Width           =   1215
  112.    End
  113.    Begin VB.Label Label1 
  114.       Caption         =   "Y scale"
  115.       Height          =   255
  116.       Index           =   3
  117.       Left            =   1440
  118.       TabIndex        =   6
  119.       Top             =   525
  120.       Width           =   615
  121.    End
  122.    Begin VB.Label Label1 
  123.       Caption         =   "X scale"
  124.       Height          =   255
  125.       Index           =   2
  126.       Left            =   0
  127.       TabIndex        =   4
  128.       Top             =   525
  129.       Width           =   615
  130.    End
  131.    Begin VB.Label Label1 
  132.       Caption         =   "dt"
  133.       Height          =   255
  134.       Index           =   1
  135.       Left            =   0
  136.       TabIndex        =   1
  137.       Top             =   60
  138.       Width           =   255
  139.    End
  140.    Begin VB.Menu mnuFile 
  141.       Caption         =   "&File"
  142.       Begin VB.Menu mnuFileExit 
  143.          Caption         =   "E&xit"
  144.       End
  145.    End
  146. Attribute VB_Name = "BDitch2Form"
  147. Attribute VB_Creatable = False
  148. Attribute VB_Exposed = False
  149. Option Explicit
  150. Const PI = 3.14159
  151. Const TWO_PI = 2 * PI
  152. Dim P As Integer
  153. Dim Q As Integer
  154. ' ************************************************
  155. ' Draw the curve on the indicated picture box.
  156. ' ************************************************
  157. Sub DrawCurve(pic As PictureBox, start_t As Single, stop_t As Single, Dt As Single, xscale As Single, yscale As Single, theta As Single)
  158. Dim x1 As Single
  159. Dim y1 As Single
  160. Dim x2 As Single
  161. Dim y2 As Single
  162. Dim ctheta As Single
  163. Dim stheta As Single
  164. Dim t As Single
  165.     ' Save these values because we use them a lot.
  166.     stheta = Sin(theta)
  167.     ctheta = Cos(theta)
  168.     x1 = xscale * X(start_t)
  169.     y1 = yscale * Y(start_t)
  170.     x2 = x1 * ctheta - y1 * stheta
  171.     y2 = x1 * stheta + y1 * ctheta
  172.     pic.Cls
  173.     pic.CurrentX = x2
  174.     pic.CurrentY = y2
  175.     t = start_t + Dt
  176.     Do While t < stop_t
  177.         x1 = xscale * X(t)
  178.         y1 = yscale * Y(t)
  179.         x2 = x1 * ctheta - y1 * stheta
  180.         y2 = x1 * stheta + y1 * ctheta
  181.         pic.Line -(x2, y2)
  182.         t = t + Dt
  183.     Loop
  184.     x1 = xscale * X(stop_t)
  185.     y1 = yscale * Y(stop_t)
  186.     x2 = x1 * ctheta - y1 * stheta
  187.     y2 = x1 * stheta + y1 * ctheta
  188.     pic.Line -(x2, y2)
  189. End Sub
  190. ' ************************************************
  191. ' Non-recursively compute the greatest common
  192. ' divisor of to integers.
  193. ' ************************************************
  194. Private Function GCD(ByVal a As Integer, ByVal b As Integer) As Integer
  195. Dim B_Mod_A As Integer
  196.     B_Mod_A = b Mod a
  197.     Do While B_Mod_A <> 0
  198.         ' Prepare the arguments for the "recursion."
  199.         b = a
  200.         a = B_Mod_A
  201.         B_Mod_A = b Mod a
  202.     Loop
  203.     GCD = a
  204. End Function
  205. ' ************************************************
  206. ' Find the least common multiple of two integers.
  207. ' ************************************************
  208. Function LCM(a As Integer, b As Integer) As Integer
  209.     LCM = a * b / GCD(a, b)
  210. End Function
  211. ' ************************************************
  212. ' Calculate the values t must cross to draw a
  213. ' Bowditch Curve.
  214. ' ************************************************
  215. Sub SetTBounds(tmin As Single, tmax As Single)
  216.     tmin = 0
  217.     tmax = LCM(P, Q) / P / Q * TWO_PI
  218.     If P Mod 2 = 1 And Q Mod 2 = 1 Then
  219.         tmin = -tmax / 4
  220.         tmax = tmax / 4
  221.     End If
  222. End Sub
  223. ' ************************************************
  224. ' The parametric function X(t).
  225. ' ************************************************
  226. Function X(t As Single) As Single
  227.     X = Sin(P * t)
  228. End Function
  229. ' ************************************************
  230. ' The parametric function Y(t).
  231. ' ************************************************
  232. Function Y(t As Single) As Single
  233.     Y = Sin(Q * t)
  234. End Function
  235. Private Sub CmdGo_Click()
  236. Dim tmin As Single
  237. Dim tmax As Single
  238. Dim Dt As Single
  239. Dim xscale As Single
  240. Dim yscale As Single
  241. Dim theta As Single
  242.     P = CInt(PText.Text)
  243.     Q = CInt(QText.Text)
  244.     SetTBounds tmin, tmax
  245.     Dt = CSng(DtText.Text)
  246.     xscale = CSng(XscaleText.Text)
  247.     yscale = CSng(YscaleText.Text)
  248.     theta = CSng(ThetaText.Text) / 180 * PI
  249.     DrawCurve Canvas, tmin, tmax, Dt, xscale, yscale, theta
  250. End Sub
  251. Private Sub mnuFileExit_Click()
  252.     Unload Me
  253. End Sub
  254.