Autocad VBA画函数曲线

程序代码
Dim pi As Double
pi = 4 * Atn(1)
Dim p(0 To 81) As Double
Dim myl As Object
j = 0
For i = -1 To 1 Step 0.05
p(j) = i * pi
p(j + 1) = Sin(p(j))
j = j + 2
Next i
Set myl = ThisDrawing.ModelSpace.AddLightWeightPolyline(p)
ZoomExtents
函数图的最后一个点跑到原点(0,0)位置了?
而把循环改成For i = 1 To 3 Step 0.05,图形却是正常的。
并且如果我在for循环之前,加上dim i as integer这一句,老是出现实时错误9,下标越界,这是为什么啊?
我又把for循环改成这样
For i = -pi To pi Step pi / 20
p(j) = i
p(j + 1) = sin(i)
j = j + 2
Next i
这样运行程序结果就对了。这个算法跟上面的不是一样的吗?

第1个回答  推荐于2016-01-26
Sub kkk()
Dim i As Double
Dim pi As Double
pi = 4 * Atn(1)
Dim p(0 To 81) As Double
Dim myl As Object
Dim j As Double
Dim inp(2) As Double
Dim n As Integer"定义一个n,查看循环次数
n = 0
For i = -1 To 1 Step 0.05
n = n + 1
p(j) = i * pi
p(j + 1) = Sin(p(j))
inp(1) = i * 20
ThisDrawing.ModelSpace.AddText n & Space(4) & j, inp, 1
j = j + 2
Next i
Set myl = ThisDrawing.ModelSpace.AddLightWeightPolyline(p)
ZoomExtents

End Sub本回答被提问者和网友采纳
第2个回答  2021-01-12
你算错顶点数,只要把这里的81改为79
Dim p(0 To 81) As Double
相似回答