📄 plotform.vb
字号:
'
'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
'
'AxScriptControl1
'
Me.AxScriptControl1.Anchor = (System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right)
Me.AxScriptControl1.Enabled = True
Me.AxScriptControl1.Location = New System.Drawing.Point(502, 246)
Me.AxScriptControl1.Name = "AxScriptControl1"
Me.AxScriptControl1.OcxState = CType(resources.GetObject("AxScriptControl1.OcxState"), System.Windows.Forms.AxHost.State)
Me.AxScriptControl1.Size = New System.Drawing.Size(38, 38)
Me.AxScriptControl1.TabIndex = 12
'
'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.AxScriptControl1, 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"
CType(Me.AxScriptControl1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Dim World As System.Drawing.Drawing2D.Matrix
Dim SmoothingMode As Drawing.Drawing2D.SmoothingMode
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
AxScriptControl1.ExecuteStatement("X=" & X)
Function1Eval = CSng(AxScriptControl1.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
AxScriptControl1.ExecuteStatement("X=" & X)
Function2Eval = CSng(AxScriptControl1.Eval(txtFunction2.Text))
Catch exc As Exception
Throw New Exception("Can't evaluate function at X=" & X)
End Try
End Function
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -