扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
用vb画出简易函数图像的操作步骤为:
创新互联建站专注于企业网络营销推广、网站重做改版、准格尔网站定制设计、自适应品牌网站建设、H5网站设计、电子商务商城网站建设、集团公司官网建设、成都外贸网站制作、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为准格尔等各大城市提供网站开发制作服务。
(1)新建一个工程,新建一个Form1,在上面添加一个picture控件和一个command控件;
(2)选中此控件,右击"复制",在窗体空白处右击"粘贴",在弹出的对话框中选择"是",创建了一个Command控件数组,复制,使窗体上总共出现7个Command控件;
(3)复制代码:
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
VB.net与VB不同。
VB.net已经有专门绘图的类。
可以定义笔刷然后用Drawing类中的方法绘制。
Private Sub DrawEllipse()
Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Red)
Dim formGraphics as System.Drawing.Graphics
formGraphics = Me.CreateGraphics()
formGraphics.DrawEllipse(myPen, New Rectangle(0,0,200,300))
myPen.Dispose()
formGraphics.Dispose()
End Sub
Private Sub DrawRectangle()
Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Red)
Dim formGraphics as System.Drawing.Graphics
formGraphics = Me.CreateGraphics()
formGraphics.DrawRectangle(myPen, New Rectangle(0,0,200,300))
myPen.Dispose()
formGraphics.Dispose()
End Sub
VB系统的坐标原点在左上角,X轴的正方向是水平向右,而Y轴的正方向是垂直向下。所以,要绘制三角函数的曲线,自己可以通过改变点坐标的方法来实现,当然,VB.NET提供了相应的方法可以来实现坐标变换,也可以通过VB.Net的Graphics类提供的平移、旋转等转换来实现。
下面是我通过自己变换实现的示例,提供参考;我的环境是VB.NET 2010
Imports System.Math
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'1,获得一个Graphics对象
Dim MyGraphics As Graphics
MyGraphics = PictureBox1.CreateGraphics
'2,定义一个Pen对象,用于绘制图形(轮廓线)
Dim MyPen As New Pen(Color.Black, 1)
'3,定义一个Brush对象,用于填充图形(如果需要填充的话)
Dim MyBrush As New SolidBrush(Color.Orange)
MyGraphics.DrawLine(MyPen, 0, 200, 700, 200)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'1,获得一个Graphics对象
Dim MyGraphics As Graphics
MyGraphics = PictureBox1.CreateGraphics
'2,定义一个Pen对象,用于绘制图形(轮廓线)
Dim MyPen As New Pen(Color.Black, 1)
'3,定义一个Brush对象,用于填充图形(如果需要填充的话)
Dim MyBrush As New SolidBrush(Color.Orange)
'声明横向和纵向比例变量
Dim Heng As Integer = 20
Dim Zong As Integer = 50
'先获得正弦值,保存到点坐标数组
Dim MyPoints(700) As Point
Dim i As Integer
For i = 0 To 700
MyPoints(i) = New Point(i * Heng, 200 + Sin(i) * Zong)
Next
'采用绘制光滑线连接点的方式绘制曲线
MyGraphics.DrawCurve(MyPen, MyPoints)
End Sub
End Class
显示的效果图:
1、利用公式求出Y值 第一列设置表头为x,下面依次填入取值, 比如 x 1 2 3 4 ...... 第二例表头设置为y,在下面表格中填入计算公式 如 =2*x+1 然后选中点右下角的十字型标记,拖下去,和前面x数量一致。 2、然后选中数据源(即选中要进行操作的数据区域,这里指XY值所在区域。) 3、执行菜单命令插入——图表——XY散点图 执行命令,打开图表向导对话框选XY散点图--选平滑线散点图--完成 最后使用图表向导在横坐标填入x值,纵坐标填入y值,生成图即可 将一次函数的Y列用公式编辑出来,分别填写出当取不同的X值时得到不的函数值,选择函数值所在列,插入——图表按提示操作。执行菜单命令 执行菜单命令
1、先定义坐标系,即XY轴。
2、根据具体需要,调用内部函数,或自定义函数,绘制函数图像。
3、代码供参考:
Me.AutoRedraw = True
Me.Width = 8000: Me.Height = 6000
Me.Scale (-10, 2)-(10, -2)
Line (0, -2)-(0, 2)
Line (-20, 0)-(20, 0)
For i! = -2 To 2 Step 0.5
Line (0, i)-(0.5, i)
CurrentX = -1.8
CurrentY = i
Print i
Next i
For j! = -8 To 10 Step 2
Line (j, 0)-(j, 0.05)
CurrentX = j - 0.5
CurrentY = 0
If Abs(j) 0 Then Print j
Next j
For k = -10 To 10 Step 0.01
PSet (k, Sin(k)), vbRed
PSet (k, Cos(k)), vbBlue
Next k
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流