如何用VB画出二次函数图像和波长图像

来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/22 07:44:13
如何用VB画出二次函数图像和波长图像如何用VB画出二次函数图像和波长图像如何用VB画出二次函数图像和波长图像在窗体上放置一个名为PicDraw的图片框,两个按钮,分别命名为CmdDrawQuad与Cm

如何用VB画出二次函数图像和波长图像
如何用VB画出二次函数图像和波长图像

如何用VB画出二次函数图像和波长图像
在窗体上放置一个名为 PicDraw 的图片框,两个按钮,分别命名为 CmdDrawQuad 与 CmdDrawWave,全部代码如下[Visual Basic 6.0]:
Option Explicit
'声明平面直角坐标系的左上角、右下角的坐标
Const sngCrdL_X As Single = -50
Const sngCrdL_Y As Single = 100
Const sngCrdR_X As Single = 100
Const sngCrdR_Y As Single = -50
'声明二次函数的解析式(Y = AX^2 + BX + C)
Const sngA As Single = 1 / 5
Const sngB As Single = 0
Const sngC As Single = 10
'
Sub Prepare()
Dim X As Long,Y As Long
PicDraw.Cls '清空图片框
PicDraw.Scale (sngCrdL_X,sngCrdL_Y)-(sngCrdR_X,sngCrdR_Y) '设置坐标
PicDraw.Line (sngCrdL_X,0)-(sngCrdR_X,0),vbBlue '绘制 X 轴
PicDraw.Line (0,sngCrdL_Y)-(0,sngCrdR_Y),vbBlue '绘制 Y 轴
For X = sngCrdL_X To sngCrdR_X
For Y = sngCrdR_Y To sngCrdL_Y
PicDraw.PSet (X,Y),vbCyan '绘制网格
Next Y
Next X
End Sub
Private Sub CmdDrawQuad_Click()
Prepare
Dim sngX As Single,sngY As Single
Dim LastX As Single,LastY As Single
Dim bDrawing As Boolean
For sngX = sngCrdL_X To sngCrdR_X Step 0.5
LastY = sngY
sngY = CalculateFunction(sngX,sngA,sngB,sngC) '计算与X坐标所对应的Y坐标
If bDrawing Then
PicDraw.Line (LastX,LastY)-(sngX,sngY) '绘制线段
End If
LastX = sngX
bDrawing = True
Next sngX
End Sub
Private Sub CmdDrawWave_Click()
Prepare
Dim sngX As Single,sngY As Single
Dim LastX As Single,LastY As Single
Dim bDrawing As Boolean
For sngX = sngCrdL_X To sngCrdR_X Step 0.5
LastY = sngY
sngY = CalculateWave(sngX,50,50) '计算与X坐标所对应的Y坐标
If bDrawing Then
PicDraw.Line (LastX,LastY)-(sngX,sngY) '绘制线段
End If
LastX = sngX
bDrawing = True
Next sngX
End Sub
声明一点,由于知识水平原因,CalculateWave函数是我自己摸索出来的,如果有问题,请告知,