怎么用VB画函数图像?

怎么用VB画函数图像?,第1张

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

一般的vb画函数图像,用的是隐友pset()函数

如你要乱猜画y=3*x

(x从1到3)

可以用

for

x=1

to

3

step=0.01

'步伐越小画得越精细

y=3*x

form1.pset(x,y)

next

x

就可以画出该图像来了,其他的灶陪槐函数的画法也一样的

一个VB绘制直角坐标系函数的程序 理论上说这个函数可以绘制所有的图像,不明白的百度HI我我给你根据你的函数公式改造一下

Option Explicit

Private Const X_Axe = 0.5 'X轴原点位于窗口的水平位置携羡山比例

Private Const Y_Axe = 0.6 '派仿Y轴原点位于窗口的垂直位置比例

Private Const ScaleRate = 30'多少像素代表一个单位长度

Private Const DrawColor = vbRed '曲线颜色

Private ErrExp As Boolean '当表达式发生错误时,会置True(比如函数在此点无有效值)

Private Sub Form_Load()

Me.Show

Me.Cls

Call DrawAxe

Call DrawCoordinate

End Sub

Private Function Expression(ByVal X As Double) As Double'公式函数可以是任意内容

ErrExp = False '进入时,必须重置此标志为False

If X >0 Then '这里代码任意,也可以是Expression = 2 * X ^ 2 + 2 * X + 1之类的

Expression = Log(X)

Else

Expression = 0

ErrExp = True

End If

End Function

Private Sub DrawCoordinate()'绘制曲线过程

Dim i As Long

Dim Last(1 To 2) As Long, This(1 To 2) As Long

Dim X_offset As Long

Dim Y_offset As Long

Me.ScaleMode = vbPixels

X_offset = Me.ScaleWidth * X_Axe

Y_offset = Me.ScaleHeight * Y_Axe

'设置一个初始值

Last(1) = 0

Last(2) = -Expression((0 - X_offset) / ScaleRate) * ScaleRate

i = 0

While i <= Me.ScaleWidth

'取下一点的值

This(1) = i

This(2) = -Expression((i - X_offset) / ScaleRate) * ScaleRate

'判断表达辩中式是否出错

If ErrExp = True Then

'出错的情况下,循环直到没有错误或者超出范围为止

While ErrExp = True And i <= Me.ScaleWidth

i = i + 1

This(1) = i

This(2) = -Expression((i - X_offset) / ScaleRate) * ScaleRate

Wend

'重置起点

Last(1) = This(1)

Last(2) = This(2)

End If

'画线

Me.Line (Last(1), Last(2) + Y_offset)-(This(1), This(2) + Y_offset), DrawColor

Last(1) = This(1)

Last(2) = This(2)

i = i + 1

Wend

End Sub

Private Sub DrawAxe() '绘制坐标的过程

Dim X_offset As Long

Dim Y_offset As Long

Dim i As Long

Me.ScaleMode = vbPixels '取单位长度为像素

X_offset = Me.ScaleWidth * X_Axe'计算坐标轴轴位置

Y_offset = Me.ScaleHeight * Y_Axe

'绘制坐标轴

Me.Line (X_offset, 0)-(X_offset, Me.ScaleHeight)

Me.Line (0, Y_offset)-(Me.ScaleWidth, Y_offset)

'绘制坐标线

For i = X_offset + ScaleRate To Me.ScaleWidth Step ScaleRate

Me.Line (i, 0)-(i, Me.ScaleHeight), vbWhite

Next i

For i = X_offset - ScaleRate To 0 Step -ScaleRate

Me.Line (i, 0)-(i, Me.ScaleHeight), vbWhite

Next i

For i = Y_offset + ScaleRate To Me.ScaleHeight Step ScaleRate

Me.Line (0, i)-(Me.ScaleWidth, i), vbWhite

Next i

For i = Y_offset - ScaleRate To 0 Step -ScaleRate

Me.Line (0, i)-(Me.ScaleWidth, i), vbWhite

Next i

End Sub

Private Sub Form_Resize()

Me.Cls

Call DrawAxe

Call DrawCoordinate

End Sub


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12449397.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存