怎么在VB中绘制数学函数图象?

2025-04-18 01:53:24
推荐回答(2个)
回答1:

  第一步,新建一个工程,新建一个Form1,在上面添加一个picture控件和一个command控件,然后选中此控件,右击"复制",在窗体空白处右击"粘贴",在弹出的对话框中选择"是",创建了一个Command控件数组,复制,使窗体上总共出现7个Command控件,然后复制代码:
  Const Pi = 3.1415926535 '定义圆周率
  Dim a, wor
  Dim i As Integer
  Static Function Loge(X)
  Loge = Log(X) / Log(Exp(1))
  End Function
  '定义用于在Picture1上的一个位置打印字符函数
  Private Function PrintWord(X, y, Word As String)
  With Picture1
  .CurrentX = X
  .CurrentY = y
  .ForeColor = RGB(0, 0, 255)
  End With
  Picture1.Print Word
  End Function
  Private Function DrawDot(Px, Py, Color)
  Picture1.PSet (Px, Py), Color
  End Function
  Sub XY() '建立直角坐标系
  Picture1.DrawWidth = 1 '设置线条宽度
  Picture1.Cls
  '设定用户坐标系,坐标原点在Picture1中心
  Picture1.Scale (-10, 10)-(10, -10)
  Picture1.Line (-10, 0)-(10, 0), RGB(0, 0, 255)
  Picture1.Line -(9.5, 0.5), RGB(0, 0, 255)
  Picture1.Line (10, 0)-(9.5, -0.5), RGB(0, 0, 255)
  Picture1.ForeColor = RGB(0, 0, 255)
  Picture1.Print "X"
  '画 X 轴
  Picture1.Line (0, -10)-(0, 10), RGB(0, 0, 255)
  Picture1.Line -(0.5, 9.5), RGB(0, 0, 255)
  Picture1.Line (0, 10)-(-0.5, 9.5), RGB(0, 0, 255)
  Picture1.Print "Y"
  '画 Y 轴
  For lin = -9 To 9
  Picture1.Line (lin, 0)-(lin, 0.25)
  wor = PrintWord(lin - 0.5, -0.5, Str(lin))
  Picture1.Line (0, lin)-(-0.25, lin)
  If lin <> 0 Then
  wor = PrintWord(-0.9, lin, Str(lin))
  End If
  Next lin
  Picture1.DrawWidth = 2
  End Sub
  Private Sub Command1_Click(Index As Integer)
  Select Case Index
  Case 0
  For a = -3 To 3 Step Pi / 6000
  Dot = DrawDot(a, a ^ 2, RGB(0, 0, 0))
  Next a
  wor = PrintWord(4, 9, "二次曲线 y=x^2")
  Case 1
  For a = -9 To 9 Step Pi / 6000
  Dot = DrawDot(a, a, RGB(0, 0, 0))
  Next a
  wor = PrintWord(8, 5, "一次曲线 y=x")
  Case 2
  For a = -9 To 3 Step Pi / 6000
  Dot = DrawDot(a, Exp(a), RGB(0, 0, 0))
  Next a
  wor = PrintWord(4, 9, "指数曲线 y=e^x")
  Case 3
  For a = 0.0001 To 9 Step Pi / 6000
  Dot = DrawDot(a, Loge(a), RGB(0, 0, 0))
  Next a
  wor = PrintWord(8, 3, "对数曲线 y=ln x")
  Case 4
  For a = -10 To 10 Step Pi / 6000
  Dot = DrawDot(a, Sin(a), RGB(0, 0, 0))
  Next a
  wor = PrintWord(-5, 2, "正弦曲线 y=sin x")
  Case 5
  For a = -10 To 10 Step Pi / 6000
  Dot = DrawDot(a, Cos(a), RGB(0, 0, 0))
  Next a
  wor = PrintWord(-9, 2, "余弦曲线 y=cos x")
  Case 6
  XY
  End Select
  End Sub
  Private Sub Form_Load()
  Me.Caption = "数学函数作图?quot;"
  Me.Show
  Me.AutoRedraw = True
  Picture1.AutoRedraw = True
  Command1(0).Caption = "二次曲线"
  Command1(1).Caption = "一次曲线"
  Command1(2).Caption = "指数曲线"
  Command1(3).Caption = "对数曲线"
  Command1(4).Caption = "正弦曲线"
  Command1(5).Caption = "余弦曲线"
  Command1(6).Caption = "清空"
  XY
  End Sub
  Private Sub Form_Resize()
  Picture1.Width = Me.Width * 0.94
  Picture1.Height = Me.Height - (Command1(0).Height * 4 + 100)
  Command1(0).Top = Me.Height - (Command1(0).Height * 2.5 + 100)
  Command1(0).Left = Me.Width * 0.01
  For i = 1 To 6
  Command1(i).Top = Me.Height - (Command1(0).Height * 2.5 + 100)
  Command1(i).Left = Command1(i - 1).Left + 1000
  Next
  XY
  End Sub

回答2:

Option Explicit
Dim i, j As Double
Private Sub Command1_Click()
Picture1.Font = "times new roman"
Picture1.FontItalic = True
Picture1.FontSize = 10
'设置字体
Picture1.Scale (-100, 100)-(100, -100)
'设置坐标轴标尺为100
Picture1.Line (-100, 0)-(100, 0)
Picture1.Line (95, 3)-(100, 0)
Picture1.Line (95, -3)-(100, 0)
Picture1.CurrentX = 90
Picture1.CurrentY = 15
Picture1.Print "x"
'画X轴及箭头
Picture1.Line (0, 100)-(0, -100)
Picture1.Line (3, 95)-(0, 100)
Picture1.Line (-3, 95)-(0, 100)
Picture1.CurrentX = 5
Picture1.CurrentY = 95
Picture1.Print "y"
'画Y轴及箭头
Picture1.CurrentX = -7
Picture1.CurrentY = 0
Picture1.Print "O"
'画原点
Command2.Enabled = True
Command3.Enabled = True
Command1.Enabled = False
End Sub

Private Sub Command2_Click()
For i = -100 To 100 Step 0.1
j = -0.1 * i * i + 6 * i - 10
Picture1.PSet (i, j), vbRed
Next i
'画y=-0.1x^2+6x-10的图像
End Sub

Private Sub Command3_Click()
For i = -100 To 100 Step 0.1
j = 100 * Sin(i * 3.1416 / 180)
Picture1.PSet (i, j), vbBlue
Next i
'画y=100sinx的图像
End Sub