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

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