home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / Mathematic581462272002.psc / frmGraph.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  2002-02-27  |  13.0 KB  |  388 lines

  1. VERSION 5.00
  2. Begin VB.Form frmGraph 
  3.    BorderStyle     =   3  'Fixed Dialog
  4.    Caption         =   "Graph"
  5.    ClientHeight    =   5070
  6.    ClientLeft      =   45
  7.    ClientTop       =   330
  8.    ClientWidth     =   9750
  9.    LinkTopic       =   "Form1"
  10.    MaxButton       =   0   'False
  11.    MinButton       =   0   'False
  12.    ScaleHeight     =   338
  13.    ScaleMode       =   3  'Pixel
  14.    ScaleWidth      =   650
  15.    ShowInTaskbar   =   0   'False
  16.    StartUpPosition =   1  'CenterOwner
  17.    Begin VB.PictureBox pctcontainer 
  18.       BorderStyle     =   0  'None
  19.       Height          =   4455
  20.       Left            =   4800
  21.       ScaleHeight     =   4455
  22.       ScaleWidth      =   4815
  23.       TabIndex        =   14
  24.       Top             =   120
  25.       Width           =   4815
  26.       Begin VB.TextBox txtClarity 
  27.          Height          =   375
  28.          Left            =   120
  29.          TabIndex        =   19
  30.          Text            =   "1"
  31.          Top             =   2160
  32.          Width           =   1455
  33.       End
  34.       Begin VB.CommandButton cmdHelp 
  35.          Caption         =   "&Help..."
  36.          Height          =   375
  37.          Left            =   1680
  38.          TabIndex        =   17
  39.          Top             =   3960
  40.          Width           =   1455
  41.       End
  42.       Begin VB.TextBox Equation 
  43.          Height          =   285
  44.          Left            =   480
  45.          TabIndex        =   0
  46.          Text            =   "X"
  47.          Top             =   360
  48.          Width           =   4215
  49.       End
  50.       Begin VB.CommandButton cmdSolve 
  51.          Caption         =   "Solve!"
  52.          Default         =   -1  'True
  53.          Height          =   375
  54.          Left            =   120
  55.          TabIndex        =   5
  56.          Top             =   3960
  57.          Width           =   1455
  58.       End
  59.       Begin VB.TextBox txtFromY 
  60.          Height          =   285
  61.          Left            =   480
  62.          TabIndex        =   3
  63.          Text            =   "-10"
  64.          Top             =   1440
  65.          Width           =   735
  66.       End
  67.       Begin VB.TextBox txtToY 
  68.          Height          =   285
  69.          Left            =   1800
  70.          TabIndex        =   4
  71.          Text            =   "10"
  72.          Top             =   1440
  73.          Width           =   735
  74.       End
  75.       Begin VB.TextBox txtFromX 
  76.          Height          =   285
  77.          Left            =   480
  78.          TabIndex        =   1
  79.          Text            =   "-10"
  80.          Top             =   1080
  81.          Width           =   735
  82.       End
  83.       Begin VB.TextBox txtToX 
  84.          Height          =   285
  85.          Left            =   1800
  86.          TabIndex        =   2
  87.          Text            =   "10"
  88.          Top             =   1080
  89.          Width           =   735
  90.       End
  91.       Begin VB.Label lblClarity 
  92.          AutoSize        =   -1  'True
  93.          Caption         =   "Clarity:"
  94.          Height          =   195
  95.          Left            =   120
  96.          TabIndex        =   18
  97.          Top             =   1920
  98.          Width           =   465
  99.       End
  100.       Begin VB.Label lblEquation 
  101.          AutoSize        =   -1  'True
  102.          Caption         =   "Equation:"
  103.          Height          =   195
  104.          Left            =   120
  105.          TabIndex        =   6
  106.          Top             =   120
  107.          Width           =   675
  108.       End
  109.       Begin VB.Label lblY 
  110.          Caption         =   "y ="
  111.          BeginProperty Font 
  112.             Name            =   "MS Sans Serif"
  113.             Size            =   8.25
  114.             Charset         =   0
  115.             Weight          =   700
  116.             Underline       =   0   'False
  117.             Italic          =   0   'False
  118.             Strikethrough   =   0   'False
  119.          EndProperty
  120.          Height          =   255
  121.          Left            =   120
  122.          TabIndex        =   7
  123.          Top             =   360
  124.          Width           =   375
  125.       End
  126.       Begin VB.Label lblFrom 
  127.          AutoSize        =   -1  'True
  128.          Caption         =   "From:"
  129.          Height          =   195
  130.          Left            =   120
  131.          TabIndex        =   8
  132.          Top             =   840
  133.          Width           =   390
  134.       End
  135.       Begin VB.Label lblFromY 
  136.          Caption         =   "y ="
  137.          BeginProperty Font 
  138.             Name            =   "MS Sans Serif"
  139.             Size            =   8.25
  140.             Charset         =   0
  141.             Weight          =   700
  142.             Underline       =   0   'False
  143.             Italic          =   0   'False
  144.             Strikethrough   =   0   'False
  145.          EndProperty
  146.          Height          =   255
  147.          Left            =   120
  148.          TabIndex        =   11
  149.          Top             =   1440
  150.          Width           =   375
  151.       End
  152.       Begin VB.Label lblToY 
  153.          Caption         =   "y ="
  154.          BeginProperty Font 
  155.             Name            =   "MS Sans Serif"
  156.             Size            =   8.25
  157.             Charset         =   0
  158.             Weight          =   700
  159.             Underline       =   0   'False
  160.             Italic          =   0   'False
  161.             Strikethrough   =   0   'False
  162.          EndProperty
  163.          Height          =   255
  164.          Left            =   1440
  165.          TabIndex        =   12
  166.          Top             =   1440
  167.          Width           =   375
  168.       End
  169.       Begin VB.Label lblTo 
  170.          AutoSize        =   -1  'True
  171.          Caption         =   "To:"
  172.          Height          =   195
  173.          Left            =   1440
  174.          TabIndex        =   9
  175.          Top             =   840
  176.          Width           =   240
  177.       End
  178.       Begin VB.Label lblFromX 
  179.          Caption         =   "x ="
  180.          BeginProperty Font 
  181.             Name            =   "MS Sans Serif"
  182.             Size            =   8.25
  183.             Charset         =   0
  184.             Weight          =   700
  185.             Underline       =   0   'False
  186.             Italic          =   0   'False
  187.             Strikethrough   =   0   'False
  188.          EndProperty
  189.          Height          =   255
  190.          Left            =   120
  191.          TabIndex        =   10
  192.          Top             =   1080
  193.          Width           =   375
  194.       End
  195.       Begin VB.Label lblToX 
  196.          Caption         =   "x ="
  197.          BeginProperty Font 
  198.             Name            =   "MS Sans Serif"
  199.             Size            =   8.25
  200.             Charset         =   0
  201.             Weight          =   700
  202.             Underline       =   0   'False
  203.             Italic          =   0   'False
  204.             Strikethrough   =   0   'False
  205.          EndProperty
  206.          Height          =   255
  207.          Left            =   1440
  208.          TabIndex        =   13
  209.          Top             =   1080
  210.          Width           =   375
  211.       End
  212.    End
  213.    Begin VB.PictureBox pctGraph 
  214.       Appearance      =   0  'Flat
  215.       AutoRedraw      =   -1  'True
  216.       BackColor       =   &H80000005&
  217.       ForeColor       =   &H80000008&
  218.       Height          =   4500
  219.       Left            =   120
  220.       ScaleHeight     =   298
  221.       ScaleMode       =   3  'Pixel
  222.       ScaleWidth      =   298
  223.       TabIndex        =   15
  224.       Top             =   120
  225.       Width           =   4500
  226.    End
  227.    Begin VB.Label lblStatus 
  228.       AutoSize        =   -1  'True
  229.       Height          =   195
  230.       Left            =   120
  231.       TabIndex        =   16
  232.       Top             =   4680
  233.       Width           =   45
  234.    End
  235. Attribute VB_Name = "frmGraph"
  236. Attribute VB_GlobalNameSpace = False
  237. Attribute VB_Creatable = False
  238. Attribute VB_PredeclaredId = True
  239. Attribute VB_Exposed = False
  240. ' Jonathan A. Feucht
  241. ' MathTool Graph Sample
  242. ' ----------------------------------------------------------------------
  243. Option Explicit
  244. ' API call used to load the help document
  245. Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
  246. Const SW_SHOWMAXIMIZED = 3
  247. Private Type PointAPI
  248.     X As Double
  249.     Y As Double
  250. End Type
  251. ' Graph data
  252. Dim Clarity As Double
  253. Dim Range As Double, Domain As Double
  254. Dim FromPos As PointAPI, ToPos As PointAPI
  255. Dim Step As PointAPI
  256. Sub SetStatus(Description As String, Status As String)
  257.     If Len(Description) > 0 Then Description = Description & ":"
  258.     lblStatus = Description & " " & Status
  259. End Sub
  260. Private Sub cmdHelp_Click()
  261. Dim HelpPath As String
  262.     HelpPath = App.Path
  263.     If Not Right$(HelpPath, 1) = "\" Then HelpPath = HelpPath & "\"
  264.     ShellExecute hwnd, "Open", "MathTool.doc", "", HelpPath, SW_SHOWMAXIMIZED
  265. End Sub
  266. Private Sub cmdSolve_Click()
  267.     On Error GoTo FoundErr
  268.     EnableContainerObjects Me, pctcontainer, False
  269.     Clarity = Val(txtClarity)
  270.     FromPos.X = SolveEq(txtFromX)
  271.     FromPos.Y = SolveEq(txtFromY)
  272.     ToPos.X = SolveEq(txtToX)
  273.     ToPos.Y = SolveEq(txtToY)
  274.     Range = ToPos.X - FromPos.X
  275.     Domain = ToPos.Y - FromPos.Y
  276.        
  277.     If Range <= 0 Then Err.Raise 1, , "Empty range"
  278.     If Domain <= 0 Then Err.Raise 1, , "Empty domain"
  279.             
  280.     Step.X = (pctGraph.ScaleWidth - 1) / Range
  281.     Step.Y = (pctGraph.ScaleHeight - 1) / Domain
  282.     ' Test the equation for syntax errors
  283.     SetVar "X", 0
  284.     If Err.Number > 0 Then Err.Raise Err.Number, , Err.Description
  285.     On Error Resume Next
  286.     SolveEq Equation
  287.     If Err.Number > 1 And Err.Number < 5 Then GoTo FoundErr
  288.     pctGraph.Cls
  289.     DrawGrid
  290.     GraphEq
  291.     EnableContainerObjects Me, pctcontainer, True
  292.     SetStatus "Equation", "y=" & CleanExpression(Equation)
  293.     Exit Sub
  294. FoundErr:
  295.     MsgBox Err.Description, vbCritical, "Error"
  296.     Err.Clear
  297.     EnableContainerObjects Me, pctcontainer, True
  298. End Sub
  299. Sub GraphEq()
  300. Dim i As Long
  301. Dim CurPt As PointAPI, OldPt As PointAPI
  302.     On Error Resume Next
  303.     pctGraph.DrawWidth = 1
  304.     For i = 0 To pctGraph.ScaleWidth * Range * Clarity
  305.             
  306.         OldPt = CurPt
  307.         CurPt.X = i / (Range * Clarity)
  308.         SetVar "X", CurPt.X / Step.X + FromPos.X
  309.         CurPt.Y = pctGraph.ScaleHeight - (Solve(Equation) - FromPos.Y) * Step.Y
  310.         If CurPt.Y <= pctGraph.ScaleHeight And CurPt.Y >= 0 And Err.Number = 0 Then
  311.             pctGraph.PSet (CurPt.X, CurPt.Y), 0
  312.         End If
  313.         
  314.     Next i
  315. End Sub
  316. Sub DrawGrid()
  317. Dim i As Double, Pos As Double
  318. Dim Location As PointAPI
  319. Dim StepVal As PointAPI, Start As PointAPI, DecStr As String
  320. Dim CurPos As Double
  321.     DecStr = CStr(Range)
  322.     StepVal.X = 1 * Mid$(DecStr, InStr(1, DecStr, ".") + 1) / 10
  323.     DecStr = CStr(Domain)
  324.     StepVal.Y = 1 * Mid$(DecStr, InStr(1, DecStr, ".") + 1) / 10
  325.     DrawWidth = 1
  326.     pctGraph.DrawWidth = 1
  327.     pctGraph.ForeColor = RGB(225, 225, 225)
  328.     For i = FromPos.X To ToPos.X + 1 Step 0.1
  329.         Pos = (Fix(i) - FromPos.X) * Step.X
  330.         pctGraph.Line (Pos, 0)-(Pos, pctGraph.ScaleHeight)
  331.     Next i
  332.     For i = FromPos.Y To ToPos.Y + 1 Step Sgn(Domain)
  333.         Pos = pctGraph.ScaleHeight - (Fix(i) - FromPos.Y) * Step.Y - 1
  334.         pctGraph.Line (0, Pos)-(pctGraph.ScaleWidth, Pos)
  335.     Next i
  336.     pctGraph.DrawWidth = 2
  337.     pctGraph.ForeColor = RGB(225, 125, 255)
  338.     Pos = -FromPos.X * Step.X
  339.     pctGraph.Line (Pos, 0)-(Pos, pctGraph.ScaleHeight)
  340.     pctGraph.ForeColor = RGB(125, 225, 225)
  341.     Pos = pctGraph.ScaleHeight + FromPos.Y * Step.Y - 1
  342.     pctGraph.Line (0, Pos)-(pctGraph.ScaleWidth, Pos)
  343. End Sub
  344. Function SolveEq(ByRef Equation As TextBox) As Double
  345.     If Err.Number > 0 Then Exit Function
  346.     SolveEq = Solve(Equation.Text)
  347.     If Err.Number > 1 And Err.Number < 5 Then
  348.         Equation.ForeColor = RGB(150, 0, 0)
  349.     Else
  350.         Equation.ForeColor = 0
  351.     End If
  352. End Function
  353. Function GetFraction(Number As Double) As Double
  354.     GetFraction = Number - Int(Number)
  355. End Function
  356. Private Function GetGraphPos(Point As PointAPI) As PointAPI
  357.     GetGraphPos.X = Point.X + CenterPos.X
  358.     GetGraphPos.Y = Point.Y + CenterPos.Y
  359. End Function
  360. Public Sub EnableContainerObjects(ContainerForm As Form, ContainerName As Control, Enabled As Boolean)
  361. Dim i As Integer
  362.     For i = 0 To ContainerForm.Controls.Count - 1
  363.         If ContainerForm.Controls(i).Container.Name = ContainerName.Name Then
  364.             ContainerForm.Controls(i).Enabled = Enabled
  365.         End If
  366.     Next i
  367. End Sub
  368. Sub UpperCase(ByRef KeyAscii As Integer)
  369.     KeyAscii = Asc(UCase$(Chr(KeyAscii)))
  370. End Sub
  371. Private Sub Equation_KeyPress(KeyAscii As Integer)
  372.     UpperCase KeyAscii
  373. End Sub
  374. Private Sub Form_Load()
  375. End Sub
  376. Private Sub txtFromX_KeyPress(KeyAscii As Integer)
  377.     UpperCase KeyAscii
  378. End Sub
  379. Private Sub txtFromY_KeyPress(KeyAscii As Integer)
  380.     UpperCase KeyAscii
  381. End Sub
  382. Private Sub txtToX_KeyPress(KeyAscii As Integer)
  383.     UpperCase KeyAscii
  384. End Sub
  385. Private Sub txtToY_KeyPress(KeyAscii As Integer)
  386.     UpperCase KeyAscii
  387. End Sub
  388.