Option Strict On Imports System.Math Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox Friend WithEvents RadioButton1 As System.Windows.Forms.RadioButton Friend WithEvents RadioButton2 As System.Windows.Forms.RadioButton Friend WithEvents RadioButton3 As System.Windows.Forms.RadioButton Friend WithEvents RadioButton4 As System.Windows.Forms.RadioButton Friend WithEvents CheckBox1 As System.Windows.Forms.CheckBox Friend WithEvents CheckBox2 As System.Windows.Forms.CheckBox Friend WithEvents bttnPlot As System.Windows.Forms.Button Friend WithEvents txtFunction1 As System.Windows.Forms.TextBox Friend WithEvents txtFunction2 As System.Windows.Forms.TextBox Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents txtXMin As System.Windows.Forms.TextBox Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents txtXMax As System.Windows.Forms.TextBox 'Required by the Windows Form Designer Private components As System.ComponentModel.Container 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Private Sub InitializeComponent() Me.CheckBox2 = New System.Windows.Forms.CheckBox() Me.PictureBox1 = New System.Windows.Forms.PictureBox() Me.Label1 = New System.Windows.Forms.Label() Me.Label2 = New System.Windows.Forms.Label() Me.RadioButton4 = New System.Windows.Forms.RadioButton() Me.txtXMin = New System.Windows.Forms.TextBox() Me.bttnPlot = New System.Windows.Forms.Button() Me.RadioButton3 = New System.Windows.Forms.RadioButton() Me.RadioButton2 = New System.Windows.Forms.RadioButton() Me.txtXMax = New System.Windows.Forms.TextBox() Me.RadioButton1 = New System.Windows.Forms.RadioButton() Me.txtFunction1 = New System.Windows.Forms.TextBox() Me.txtFunction2 = New System.Windows.Forms.TextBox() Me.CheckBox1 = New System.Windows.Forms.CheckBox() Me.SuspendLayout() ' 'CheckBox2 ' Me.CheckBox2.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) Me.CheckBox2.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.CheckBox2.Location = New System.Drawing.Point(8, 363) Me.CheckBox2.Name = "CheckBox2" Me.CheckBox2.Size = New System.Drawing.Size(16, 16) Me.CheckBox2.TabIndex = 11 ' 'PictureBox1 ' Me.PictureBox1.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _ Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right) Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle Me.PictureBox1.Location = New System.Drawing.Point(16, 16) Me.PictureBox1.Name = "PictureBox1" Me.PictureBox1.Size = New System.Drawing.Size(436, 248) Me.PictureBox1.TabIndex = 0 Me.PictureBox1.TabStop = False ' 'Label1 ' Me.Label1.Anchor = (System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right) Me.Label1.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label1.Location = New System.Drawing.Point(460, 171) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(40, 16) Me.Label1.TabIndex = 4 Me.Label1.Text = "Xmin" ' 'Label2 ' Me.Label2.Anchor = (System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right) Me.Label2.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label2.Location = New System.Drawing.Point(460, 211) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(40, 16) Me.Label2.TabIndex = 6 Me.Label2.Text = "Xmax" ' 'RadioButton4 ' Me.RadioButton4.Anchor = (System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right) Me.RadioButton4.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.RadioButton4.Location = New System.Drawing.Point(468, 120) Me.RadioButton4.Name = "RadioButton4" Me.RadioButton4.Size = New System.Drawing.Size(112, 24) Me.RadioButton4.TabIndex = 3 Me.RadioButton4.Text = "High Speed" ' 'txtXMin ' Me.txtXMin.Anchor = (System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right) Me.txtXMin.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.txtXMin.Location = New System.Drawing.Point(508, 168) Me.txtXMin.Name = "txtXMin" Me.txtXMin.Size = New System.Drawing.Size(64, 22) Me.txtXMin.TabIndex = 5 Me.txtXMin.Text = "-0.5" ' 'bttnPlot ' Me.bttnPlot.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) Me.bttnPlot.Font = New System.Drawing.Font("Verdana", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.bttnPlot.Location = New System.Drawing.Point(16, 280) Me.bttnPlot.Name = "bttnPlot" Me.bttnPlot.Size = New System.Drawing.Size(440, 32) Me.bttnPlot.TabIndex = 8 Me.bttnPlot.Text = "Plot" ' 'RadioButton3 ' Me.RadioButton3.Anchor = (System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right) Me.RadioButton3.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.RadioButton3.Location = New System.Drawing.Point(468, 88) Me.RadioButton3.Name = "RadioButton3" Me.RadioButton3.Size = New System.Drawing.Size(112, 24) Me.RadioButton3.TabIndex = 2 Me.RadioButton3.Text = "High Quality" ' 'RadioButton2 ' Me.RadioButton2.Anchor = (System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right) Me.RadioButton2.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.RadioButton2.Location = New System.Drawing.Point(468, 56) Me.RadioButton2.Name = "RadioButton2" Me.RadioButton2.Size = New System.Drawing.Size(112, 24) Me.RadioButton2.TabIndex = 1 Me.RadioButton2.Text = "Default" ' 'txtXMax ' Me.txtXMax.Anchor = (System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right) Me.txtXMax.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.txtXMax.Location = New System.Drawing.Point(508, 208) Me.txtXMax.Name = "txtXMax" Me.txtXMax.Size = New System.Drawing.Size(64, 22) Me.txtXMax.TabIndex = 7 Me.txtXMax.Text = "5" ' 'RadioButton1 ' Me.RadioButton1.Anchor = (System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right) Me.RadioButton1.Checked = True Me.RadioButton1.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.RadioButton1.Location = New System.Drawing.Point(468, 24) Me.RadioButton1.Name = "RadioButton1" Me.RadioButton1.Size = New System.Drawing.Size(112, 24) Me.RadioButton1.TabIndex = 0 Me.RadioButton1.TabStop = True Me.RadioButton1.Text = "Antialiasing" ' 'txtFunction1 ' Me.txtFunction1.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) Me.txtFunction1.Font = New System.Drawing.Font("Tahoma", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.txtFunction1.Location = New System.Drawing.Point(40, 328) Me.txtFunction1.Name = "txtFunction1" Me.txtFunction1.Size = New System.Drawing.Size(416, 22) Me.txtFunction1.TabIndex = 10 Me.txtFunction1.Text = "5 + 20 * COS(X * 3) * COS(X * 5) " ' 'txtFunction2 ' Me.txtFunction2.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) Me.txtFunction2.Font = New System.Drawing.Font("Tahoma", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.txtFunction2.Location = New System.Drawing.Point(40, 360) Me.txtFunction2.Name = "txtFunction2" Me.txtFunction2.Size = New System.Drawing.Size(416, 22) Me.txtFunction2.TabIndex = 12 Me.txtFunction2.Text = "10 + 35 * SIN(2 * X) * SIN(0.80 / X)" ' 'CheckBox1 ' Me.CheckBox1.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) Me.CheckBox1.Checked = True Me.CheckBox1.CheckState = System.Windows.Forms.CheckState.Checked Me.CheckBox1.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.CheckBox1.Location = New System.Drawing.Point(8, 331) Me.CheckBox1.Name = "CheckBox1" Me.CheckBox1.Size = New System.Drawing.Size(24, 16) Me.CheckBox1.TabIndex = 9 ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(588, 393) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.txtXMax, Me.Label2, Me.txtXMin, Me.Label1, Me.txtFunction2, Me.txtFunction1, Me.CheckBox2, Me.CheckBox1, Me.RadioButton4, Me.RadioButton3, Me.RadioButton2, Me.RadioButton1, Me.bttnPlot, Me.PictureBox1}) Me.Name = "Form1" Me.Text = "Function Plotting Demo" Me.ResumeLayout(False) End Sub #End Region Dim World As System.Drawing.Drawing2D.Matrix Dim SmoothingMode As Drawing.Drawing2D.SmoothingMode Dim Script As New MSScriptControl.ScriptControl() Private Sub bttnPlot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnPlot.Click If Not (CheckBox1.Checked Or CheckBox2.Checked) Then Exit Sub ' functions are plotted on the Graphics object derived from ' the(Control) 's bitmap, and are persistent Dim bmp As Bitmap bmp = New Bitmap(PictureBox1.Width, PictureBox1.Height) PictureBox1.Image = bmp Dim G As Graphics G = Graphics.FromImage(bmp) G.Clear(PictureBox1.BackColor) Dim t As Double Dim Xmin, Xmax As Single Dim Ymax, Ymin As Single Ymin = System.Single.MaxValue Ymax = -System.Single.MaxValue Xmin = CSng(txtXMin.Text) Xmax = CSng(txtXMax.Text) Dim val As Single Dim XPixels As Integer = PictureBox1.Width - 1 ' Calculate Ymin, Ymax for both functions For t = Xmin To Xmax Step (Xmax - Xmin) / (PictureBox1.Width - 2) If CheckBox1.Checked Then Try val = CSng(Function1Eval(t)) Catch exc As Exception MsgBox("Can't plot this function in " & _ "the specified range!" & vbCrLf & exc.Message) Exit Sub End Try Ymax = Math.Max(val, Ymax) Ymin = Math.Min(val, Ymin) End If If CheckBox2.Checked Then Try val = CSng(Function2Eval(t)) Catch exc As Exception MsgBox("Can't plot this function in " & _ "the specified range!" & vbCrLf & exc.Message) Exit Sub End Try Ymax = Math.Max(val, Ymax) Ymin = Math.Min(val, Ymin) End If Next ' set up the appropriate Scale and Translate transformations World = New System.Drawing.Drawing2D.Matrix() World.Scale(((PictureBox1.Width - 2) / (Xmax - Xmin)), - _ (PictureBox1.Height - 2) / (Ymax - Ymin)) World.Translate(-Xmin, -Ymax) ' the following paths correspond to the two axes Dim Xaxis As New System.Drawing.Drawing2D.GraphicsPath() Dim Yaxis As New System.Drawing.Drawing2D.GraphicsPath() Xaxis.AddLine(New PointF(Xmin, 0), New PointF(Xmax, 0)) Yaxis.AddLine(New PointF(0, Ymax), New PointF(0, Ymin)) Dim oldX, oldY As Single Dim X, Y As Single ' Each segment in the path goes from (oldX, oldY) to (X, y) ' At each iteration (X, Y) becomes (oldX, oldY) for the next point ' the following two paths correspond to the functions to be plotted Dim plot1 As New System.Drawing.Drawing2D.GraphicsPath() Dim plot2 As New System.Drawing.Drawing2D.GraphicsPath() Dim plotPen As Pen = New Pen(Color.BlueViolet, 1) ' Calculate the min and max points of the plot If CheckBox1.Checked Then oldX = Xmin oldY = CSng(Function1Eval(Xmin)) For t = Xmin To Xmax Step (Xmax - Xmin) / _ (PictureBox1.Width - 1) X = CSng(t) Y = CSng(Function1Eval(t)) plot1.AddLine(oldX, oldY, X, Y) oldX = X oldY = Y Next End If If CheckBox2.Checked Then oldX = Xmin oldY = CSng(Function2Eval(Xmin)) For t = Xmin To Xmax Step (Xmax - Xmin) / _ (PictureBox1.Width - 1) X = CSng(t) Y = CSng(Function2Eval(t)) plot2.AddLine(oldX, oldY, X, Y) oldX = X oldY = Y Next End If ' create the plot1 and plot2 paths G.Clear(PictureBox1.BackColor) If RadioButton1.Checked Then SmoothingMode = _ Drawing.Drawing2D.SmoothingMode.AntiAlias If RadioButton2.Checked Then SmoothingMode = _ Drawing.Drawing2D.SmoothingMode.Default If RadioButton3.Checked Then SmoothingMode = _ Drawing.Drawing2D.SmoothingMode.HighQuality If RadioButton4.Checked Then SmoothingMode = _ Drawing.Drawing2D.SmoothingMode.HighSpeed G.SmoothingMode = SmoothingMode ' and finally draw everything Xaxis.Transform(World) plotPen.Color = Color.Red G.DrawPath(plotPen, Xaxis) ' The X axis Yaxis.Transform(World) plotPen.Color = Color.Red G.DrawPath(plotPen, Yaxis) ' The Y axis If CheckBox1.Checked Then plotPen.Color = Color.DarkMagenta plot1.Transform(World) ' The first function G.DrawPath(plotPen, plot1) End If If CheckBox2.Checked Then plotPen.Color = Color.DarkGreen plot2.Transform(World) G.DrawPath(plotPen, plot2) ' The second function End If End Sub Function Function1Eval(ByVal X As Double) As Double Try Script.ExecuteStatement("X=" & X) Function1Eval = CSng(Script.Eval(txtFunction1.Text)) Catch exc As Exception Throw New Exception("Can't evaluate function at X=" & X) End Try End Function Function Function2Eval(ByVal X As Double) As Double Try Script.ExecuteStatement("X=" & X) Function2Eval = CSng(Script.Eval(txtFunction2.Text)) Catch exc As Exception Throw New Exception("Can't evaluate function at X=" & X) End Try End Function Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Script.Language = "VBScript" End Sub End Class