1实验步骤
新建标准EXE工程,将默认窗体Form1的Caption属性改为“绘制三角函数图形”,在窗体上添加3个命令按钮Command1、Command2和Command3,将其Caption属性分别改为“建立坐标系”“y=Sin(x)”和“y=Cos(x)”,并按图7.18调整好位置。
2编写代码
双击窗体上的“建立坐标系”按钮,进入代码编辑器窗口,在代码编辑区的Command1_Click()中添加代码:
'绘制坐标轴和坐标刻度
Private Sub Command1_Click()
Cls
Form1.Scale (-8, 2)-(8, -2) ??? ?'用Scale方法定义坐标系
Line (-7.5, 0)-(7.5, 0) ???? '用Line方法画X轴
Line (0, 1.7)-(0, -1.7) ??? ?'画Line方法画Y轴
CurrentX = 7.6: CurrentY = 0.1: Print "X" ?? ??'标识X轴
CurrentY = 0.5: CurrentY = 1.8: Print "Y" ??? ?'标识X轴
For i = -7 To 7
Line (i, 0)-(i, 0.1) ??? ?'在X轴上标记坐标刻度
CurrentX = i - 0.2: CurrentY = -0.1: Print I???? '在X轴上输出数字标识
Next i
For i = -1 To 1
If i <> 0 Then
CurrentX = -0.7: CurrentY = i + 0.1: Print I???? '在Y轴上输出数字标识
Line (0.5, i)-(0, i) ???? '在Y轴上标记坐标刻度
End If
Next i
End Sub
' 绘制正弦曲线
Private Sub Command2_Click()
CurrentX = -6.283: CurrentY = 0 ???? '曲线的起点坐标
For i = -6.283 To 6.283 Step 0.01
x = i: y = Sin(i)
Line -(x, y) ???? '用Line方法绘制正弦曲线
Next i
CurrentX = 2.5: CurrentY = 1: Print "y=Sin(x)"??? ?'输出y=Sin(x)
End Sub
Private Sub Command3_Click() ??? ?'绘制余弦曲线
DrawWidth = 2 ???? '设置线宽
CurrentX = -6.283: CurrentY = 1
For i = -6.283 To 6.283 Step 0.01
x = i: y = Cos(i)
Line -(x, y) ??? ?'绘制余弦曲线
Next i
CurrentX = -7: CurrentY = 1.2: Print "y=Cos(x)"??? ?'输出y=Cos(x)
End Sub
运行程序,并单击“建立坐标系”按钮,即绘制出坐标系,然后单击“y=Sin(x)”“y=Cos(x)”按钮,分别绘制出正弦、余弦曲线。