home *** CD-ROM | disk | FTP | other *** search
/ Mastering Visual Basic 6 / mastvb6.iso / ch_code / ch20 / graph / graph.frm (.txt) next >
Encoding:
Visual Basic Form  |  1996-05-13  |  9.8 KB  |  298 lines

  1. VERSION 5.00
  2. Object = "{0E59F1D2-1FBE-11D0-8FF2-00A0D10038BC}#1.0#0"; "MSSCRIPT.OCX"
  3. Begin VB.Form GraphForm 
  4.    BorderStyle     =   1  'Fixed Single
  5.    Caption         =   "Function Plot"
  6.    ClientHeight    =   4995
  7.    ClientLeft      =   45
  8.    ClientTop       =   330
  9.    ClientWidth     =   6750
  10.    LinkTopic       =   "Form1"
  11.    MaxButton       =   0   'False
  12.    MinButton       =   0   'False
  13.    ScaleHeight     =   4995
  14.    ScaleWidth      =   6750
  15.    StartUpPosition =   3  'Windows Default
  16.    Begin MSScriptControlCtl.ScriptControl ScriptControl1 
  17.       Left            =   0
  18.       Top             =   720
  19.       _ExtentX        =   1005
  20.       _ExtentY        =   1005
  21.       AllowUI         =   -1  'True
  22.    End
  23.    Begin VB.TextBox Text2 
  24.       BeginProperty Font 
  25.          Name            =   "Verdana"
  26.          Size            =   9
  27.          Charset         =   0
  28.          Weight          =   400
  29.          Underline       =   0   'False
  30.          Italic          =   0   'False
  31.          Strikethrough   =   0   'False
  32.       EndProperty
  33.       Height          =   315
  34.       Left            =   1575
  35.       TabIndex        =   5
  36.       Text            =   "Cos(3*X)*Sin(5*X)"
  37.       Top             =   450
  38.       Width           =   5040
  39.    End
  40.    Begin VB.TextBox Text1 
  41.       BeginProperty Font 
  42.          Name            =   "Verdana"
  43.          Size            =   9
  44.          Charset         =   0
  45.          Weight          =   400
  46.          Underline       =   0   'False
  47.          Italic          =   0   'False
  48.          Strikethrough   =   0   'False
  49.       EndProperty
  50.       Height          =   315
  51.       Left            =   1575
  52.       TabIndex        =   4
  53.       Text            =   "Exp(2/X)*Cos(2*X)"
  54.       Top             =   105
  55.       Width           =   5040
  56.    End
  57.    Begin VB.CommandButton Command3 
  58.       Caption         =   "Draw both functions"
  59.       BeginProperty Font 
  60.          Name            =   "Tahoma"
  61.          Size            =   9.75
  62.          Charset         =   0
  63.          Weight          =   400
  64.          Underline       =   0   'False
  65.          Italic          =   0   'False
  66.          Strikethrough   =   0   'False
  67.       EndProperty
  68.       Height          =   405
  69.       Left            =   4530
  70.       TabIndex        =   3
  71.       Top             =   4470
  72.       Width           =   2085
  73.    End
  74.    Begin VB.CommandButton Command2 
  75.       Caption         =   "Draw second function"
  76.       BeginProperty Font 
  77.          Name            =   "Tahoma"
  78.          Size            =   9.75
  79.          Charset         =   0
  80.          Weight          =   400
  81.          Underline       =   0   'False
  82.          Italic          =   0   'False
  83.          Strikethrough   =   0   'False
  84.       EndProperty
  85.       Height          =   405
  86.       Left            =   2310
  87.       TabIndex        =   2
  88.       Top             =   4470
  89.       Width           =   2085
  90.    End
  91.    Begin VB.CommandButton Command1 
  92.       Caption         =   "Draw first function"
  93.       BeginProperty Font 
  94.          Name            =   "Tahoma"
  95.          Size            =   9.75
  96.          Charset         =   0
  97.          Weight          =   400
  98.          Underline       =   0   'False
  99.          Italic          =   0   'False
  100.          Strikethrough   =   0   'False
  101.       EndProperty
  102.       Height          =   405
  103.       Left            =   105
  104.       TabIndex        =   1
  105.       Top             =   4470
  106.       Width           =   2085
  107.    End
  108.    Begin VB.PictureBox Picture1 
  109.       BackColor       =   &H00FFFFFF&
  110.       Height          =   3480
  111.       Left            =   105
  112.       ScaleHeight     =   228
  113.       ScaleMode       =   3  'Pixel
  114.       ScaleWidth      =   430
  115.       TabIndex        =   0
  116.       Top             =   855
  117.       Width           =   6510
  118.    End
  119.    Begin VB.Label Label2 
  120.       Caption         =   "Function #2"
  121.       BeginProperty Font 
  122.          Name            =   "Verdana"
  123.          Size            =   9
  124.          Charset         =   0
  125.          Weight          =   400
  126.          Underline       =   0   'False
  127.          Italic          =   0   'False
  128.          Strikethrough   =   0   'False
  129.       EndProperty
  130.       Height          =   210
  131.       Left            =   135
  132.       TabIndex        =   7
  133.       Top             =   495
  134.       Width           =   1365
  135.    End
  136.    Begin VB.Label Label1 
  137.       Caption         =   "Function #1"
  138.       BeginProperty Font 
  139.          Name            =   "Verdana"
  140.          Size            =   9
  141.          Charset         =   0
  142.          Weight          =   400
  143.          Underline       =   0   'False
  144.          Italic          =   0   'False
  145.          Strikethrough   =   0   'False
  146.       EndProperty
  147.       Height          =   210
  148.       Left            =   135
  149.       TabIndex        =   6
  150.       Top             =   150
  151.       Width           =   1365
  152.    End
  153. Attribute VB_Name = "GraphForm"
  154. Attribute VB_GlobalNameSpace = False
  155. Attribute VB_Creatable = False
  156. Attribute VB_PredeclaredId = True
  157. Attribute VB_Exposed = False
  158. '  ******************************
  159. '  ******************************
  160. '  ** MASTERING VB6            **
  161. '  ** by Evangelos Petroutos   **
  162. '  ** SYBEX, 1998              **
  163. '  ******************************
  164. '  ******************************
  165. Function FunctionEval1(ByVal X As Double) As Double
  166.     ScriptControl1.ExecuteStatement "X=" & X
  167.     FunctionEval1 = ScriptControl1.Eval(Trim(Text1.Text))
  168. End Function
  169. Function FunctionEval2(ByVal X As Double) As Double
  170.     ScriptControl1.AddCode "X=" & X
  171.     FunctionEval2 = ScriptControl1.Eval(Trim(Text2.Text))
  172. End Function
  173. Private Sub Command1_Click()
  174. Dim t As Double
  175. Dim XMin As Double, XMax As Double, YMin As Double, YMax As Double
  176. Dim XPixels As Integer
  177. On Error GoTo FncError
  178.     YMin = 1E+101: YMax = -1E+101
  179.     XMin = 2: XMax = 10
  180.     Picture1.Cls
  181.     Picture1.ScaleMode = 3
  182.     XPixels = Picture1.ScaleWidth - 1
  183.     Me.Caption = "Calculating range..."
  184.     Screen.MousePointer = vbHourglass
  185.     ' Calculate Min and Max for Y axis
  186.     For i = 1 To XPixels
  187.         t = XMin + (XMax - XMin) * i / XPixels
  188.         functionVal = FunctionEval1(t)
  189.         If functionVal > YMax Then YMax = functionVal
  190.         If functionVal < YMin Then YMin = functionVal
  191.     Next
  192.     Me.Caption = "Plotting function..."
  193.     ' Set up a user defined scale mode
  194.     Picture1.Scale (XMin, YMin)-(XMax, YMax)
  195.     Picture1.ForeColor = RGB(0, 0, 255)
  196.     ' Move to the first point
  197.     Picture1.PSet (XMin, FunctionEval1(XMin))
  198.     ' Plot the function
  199.     For i = 0 To XPixels
  200.         t = XMin + (XMax - XMin) * i / XPixels
  201.         'Picture1.PSet (t, FunctionEval1(t))
  202.         Picture1.Line -(t, FunctionEval1(t))
  203.     Next
  204.     Me.Caption = "Function Plot"
  205.     Screen.MousePointer = vbDefault
  206.     Exit Sub
  207. FncError:
  208.     MsgBox "There was an error in evaluating the function"
  209.     Screen.MousePointer = vbDefault
  210. End Sub
  211. Private Sub Command2_Click()
  212. Dim t As Double
  213. Dim XMin As Double, XMax As Double, YMin As Double, YMax As Double
  214. Dim XPixels As Integer
  215.     YMin = 1E+101: YMax = -1E+101
  216.     XMin = 2: XMax = 10
  217.     Picture1.Cls
  218.     Picture1.ScaleMode = 3
  219.     XPixels = Picture1.ScaleWidth - 1
  220.     Me.Caption = "Calculating range..."
  221.     Screen.MousePointer = vbHourglass
  222.     ' Calculate Min and Max for Y axis
  223.     For i = 0 To XPixels
  224.         t = XMin + (XMax - XMin) * i / XPixels
  225.         functionVal = FunctionEval2(t)
  226.         If functionVal > YMax Then YMax = functionVal
  227.         If functionVal < YMin Then YMin = functionVal
  228.     Next
  229.     Me.Caption = "Plotting function..."
  230.     ' Set up a user defined scale mode
  231.         Picture1.Scale (XMin, YMin)-(XMax, YMax)
  232.     Picture1.ForeColor = RGB(255, 0, 0)
  233.     ' Move to the first point
  234.     Picture1.PSet (XMin, FunctionEval1(XMin))
  235.     ' Plot the function
  236.     For i = 0 To XPixels - 1
  237.         t = XMin + (XMax - XMin) * i / XPixels
  238.         functionVal = FunctionEval2(t)
  239.         'Picture1.PSet (t, functionVal)
  240.         Picture1.Line -(t, functionVal)
  241.     Next
  242.     Me.Caption = "Function Plot"
  243.     Screen.MousePointer = vbDefault
  244.     Exit Sub
  245. FncError:
  246.     MsgBox "There was an error in evaluating the function"
  247.     Screen.MousePointer = vbDefault
  248. End Sub
  249. Private Sub Command3_Click()
  250. Dim t As Double
  251. Dim XMin As Double, XMax As Double, YMin As Double, YMax As Double
  252. Dim XPixels As Integer
  253.     YMin = 1E+101: YMax = -1E+101
  254.     XMin = 2: XMax = 10
  255.     Picture1.Cls
  256.     Picture1.ScaleMode = 3
  257.     XPixels = Picture1.ScaleWidth - 1
  258.     Me.Caption = "Calculating range..."
  259.     Screen.MousePointer = vbHourglass
  260.     ' Calculate Min and Max for Y axis
  261.     For i = 1 To XPixels
  262.         t = XMin + (XMax - XMin) * i / XPixels
  263.         functionVal = FunctionEval1(t)
  264.         If functionVal > YMax Then YMax = functionVal
  265.         If functionVal < YMin Then YMin = functionVal
  266.     Next
  267.     Me.Caption = "Plotting functions..."
  268.     ' Set up a user defined scale mode
  269.     Picture1.Scale (XMin, YMin)-(XMax, YMax)
  270.     Picture1.ForeColor = RGB(0, 0, 255)
  271.     ' Move to the first point
  272.     Picture1.PSet (XMin, FunctionEval1(XMin))
  273.     ' Plot the function
  274.     For i = 0 To XPixels
  275.         t = XMin + (XMax - XMin) * i / XPixels
  276.         'Picture1.PSet (t, FunctionEval1(t))
  277.         Picture1.Line -(t, FunctionEval1(t))
  278.     Next
  279.     Picture1.ForeColor = RGB(255, 0, 0)
  280.     Picture1.PSet (XMin, FunctionEval2(XMin))
  281.     ' Plot the function
  282.     For i = 0 To XPixels
  283.         t = XMin + (XMax - XMin) * i / XPixels
  284.         'Picture1.PSet (t, FunctionEval2(t))
  285.         Picture1.Line -(t, FunctionEval2(t))
  286.     Next
  287.     Me.Caption = "Function Plot"
  288.     Screen.MousePointer = vbDefault
  289.     Exit Sub
  290. FncError:
  291.     MsgBox "There was an error in evaluating the function"
  292.     Screen.MousePointer = vbHourglass
  293. End Sub
  294. Private Sub ScriptControl1_Error()
  295.     Debug.Print ScriptControl1.Error.Number
  296.     Debug.Print ScriptControl1.Error.Text
  297. End Sub
  298.