正弦函数的画法

来源:互联网 发布:网络产品代理 编辑:程序博客网 时间:2024/03/29 17:13

Option Explicit
Dim intrate%, intdeg%, m!, n!
'intrate表示速度 ,intdeg表示sin(x)中x的角度 ,n表示幅值放大的倍数,m表示sin(x)的函数周期倍数
Private Sub Command1_Click()
Call drawaxis(Picture1)
Timer1.Enabled = True
Command2.Caption = "停止"
Command1.Enabled = False
End Sub

Private Sub Command2_Click()
If Command2.Caption = "停止" Then
   Command2.Caption = "继续"
   Timer1.Enabled = False    ' 关闭时钟控件
ElseIf Command2.Caption = "继续" Then
   Command2.Caption = "停止"
   Timer1.Enabled = True
End If
End Sub

Private Sub Command3_Click()
Unload Me
End Sub

Private Sub Form_Load()
 Timer1.Interval = 50
 Timer1.Enabled = False
 Option1(1).Value = True
 Option2(1).Value = True
 Option3(1).Value = True
 m = 1
 n = 1
End Sub

Private Sub Form_Unload(Cancel As Integer)
Timer1.Enabled = False
End Sub

Private Sub drawaxis(picx As PictureBox)         '自定义绘制坐标轴过程
 Dim x1%, y1%, x2%, y2%, Y%
 picx.BackColor = vbWhite        '置图片框背景色
 picx.Cls                         ' 清屏
 picx.DrawStyle = 0              '置图片框为画实线
 x1 = 120                        ' 第一点的横坐标
 y2 = 120                        '第二点的纵坐标
 y1 = picx.ScaleHeight - 120     '第一点的纵坐标
 x2 = picx.ScaleWidth - 120      ' 第二点的横坐标
 Y = picx.ScaleHeight / 2
 picx.Line (x1, y1)-(x1, y2), QBColor(0)   ' 画y轴,自下而上
 picx.Line (x1, Y)-(x2, Y), QBColor(0)     ' 画x轴,自左向右
 picx.Line (x1 - 50, y2 + 50)-(x1, y2), QBColor(0) '画y轴箭头
 picx.Line (x1 + 50, y2 + 50)-(x1, y2), QBColor(0)
 picx.CurrentX = 300
 picx.CurrentY = 200
 picx.Print "Y轴"
 picx.Line (x2 - 50, Y + 50)-(x2, Y), QBColor(0) '画x轴箭头
 picx.Line (x2 - 50, Y - 50)-(x2, Y), QBColor(0)
 picx.CurrentX = x2 - 200
 picx.CurrentY = Y + 200
 picx.Print "x轴"
End Sub

Private Sub Option1_Click(Index As Integer)
Select Case Index
  Case 0
    m = 0.5    ' 放大一倍
  Case 1
    m = 1      ' 幅值不变
  Case 2
    m = 2      ' 缩小一倍
 End Select
End Sub

Private Sub Option2_Click(Index As Integer)
 Select Case Index
  Case 0
    n = 2    ' 幅值放大一倍
  Case 1
    n = 1      ' 幅值不变
  Case 2
    n = 0.5   ' 幅值缩小一倍
 End Select
End Sub

Private Sub Option3_Click(Index As Integer)
 '改变循环语句的循环次数以调整绘制速度
 Select Case Index
  Case 0            ' 快速
    intrate = 20
  Case 1
    intrate = 8    '  中速
  Case 2
    intrate = 2    '慢速
 End Select
End Sub

Private Sub Timer1_Timer()
 Dim X%, Y%, i%, scaly%
 scaly = (Picture1.ScaleHeight - 120) / 4          ' 信号最大值
 For i = 1 To intrate
  Picture1.CurrentX = 120                          ' 获取原点的坐标
  Picture1.CurrentY = (Picture1.ScaleHeight) / 2
  X = intdeg / 180 * scaly                          ' 取画点的x坐标
  Y = Sin(m * intdeg * 3.141593 / 180) * n * scaly  ' 取画点的y坐标
  Picture1.PSet Step(X, -Y), vbBlue
  intdeg = intdeg + 1
  If Picture1.CurrentX > Picture1.ScaleWidth - 120 Then
    intdeg = 0
    Command2.Value = True             ' 相当于按了 command2按钮
    Command1.Enabled = True          ' 相当于按了 command1按钮
  End If
 Next
End Sub

原创粉丝点击