⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 plotform.vb

📁 Mastering VBNet Include Source Code
💻 VB
📖 第 1 页 / 共 2 页
字号:
        Me.RadioButton1.TabIndex = 2
        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.ReadOnly = True
        Me.txtFunction1.Size = New System.Drawing.Size(416, 22)
        Me.txtFunction1.TabIndex = 9
        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.ReadOnly = True
        Me.txtFunction2.Size = New System.Drawing.Size(416, 22)
        Me.txtFunction2.TabIndex = 9
        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.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 = 6
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(592, 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

    Private Sub bttnPlot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnPlot.Click
        plot()
    End Sub

    Function Function1Eval(ByVal X As Double) As Double
        Function1Eval = 5 + 20 * Cos(X * 3) * Cos(X * 5) / Log(Abs(Sin(X)) / 10)
    End Function

    Function Function2Eval(ByVal X As Double) As Double
        Function2Eval = 10 + 15 * Sin(2 * X) * Sin(5 / X)
    End Function

    Private Sub plot()
        If Not (CheckBox1.Checked Or CheckBox2.Checked) Then Exit Sub
        Dim G As Graphics
        G = PictureBox1.CreateGraphics
        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 = txtXMin.Text
        Xmax = txtXMax.Text
        Dim val As Single
        Dim XPixels As Integer = PictureBox1.Width - 1

        For t = Xmin To Xmax Step (Xmax - Xmin) / (PictureBox1.Width - 2)
            If CheckBox1.Checked Then
                val = Function1Eval(t)
                If System.Double.IsInfinity(val) Or _
                            System.Double.IsNaN(val) Then
                    MsgBox("Can't plot this function in " & _
                           "the specified range!")
                    Exit Sub
                End If
                Ymax = Math.Max(val, Ymax)
                Ymin = Math.Min(val, Ymin)
            End If
            If CheckBox2.Checked Then
                val = Function2Eval(t)
                If System.Double.IsInfinity(val) Or _
                            System.Double.IsNaN(val) Then
                    MsgBox("Can't plot this function in " & _
                           "the specified range!")
                    Exit Sub
                End If
                Ymax = Math.Max(val, Ymax)
                Ymin = Math.Min(val, Ymin)
            End If
        Next
        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 = Function1Eval(Xmin)
            For t = Xmin To Xmax Step (Xmax - Xmin) / _
                                      (PictureBox1.Width - 1)
                X = t
                Y = Function1Eval(t)
                plot1.AddLine(oldX, oldY, X, Y)
                oldX = X
                oldY = Y
            Next
        End If
        If CheckBox2.Checked Then
            oldX = Xmin
            oldY = Function2Eval(Xmin)
            For t = Xmin To Xmax Step (Xmax - Xmin) / _
                                      (PictureBox1.Width - 1)
                X = t
                Y = 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
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -