[VBA函数]:原创:正弦曲线
这是我第一次在这发贴!本论坛中有一用LISP画正弦曲线的贴子,我编了两个VBA小程序,各位PP。
Sub sin1()
'由正弦曲线的起点、终点、幅度、周波数来画正弦曲线
Dim sinObj As AcadLWPolyline
Dim points() As Double
Dim a, n, k As Integer
Dim H, L As Double
Dim pa, pb As Variant
PI = 3.1415926535
pa = ThisDrawing.Utility.GetPoint(, "请输入正弦曲线起点:")
pb = ThisDrawing.Utility.GetPoint(pa, "请输入正弦曲线终点:")
H = ThisDrawing.Utility.GetDistance(pa, "请正弦曲线幅度:")
n = ThisDrawing.Utility.GetReal("请输入正弦曲线周波数:")
k = ThisDrawing.Utility.GetReal("请输入正弦曲线每周波线段数(建议不小于36):")
ReDim points(0 To 2 * k * n + 1) As Double
L = dis(pa, pb)
b = ThisDrawing.Utility.AngleFromXAxis(pa, pb)
For a = 0 To 2 * k * n Step 2
points(a) = pa(0) + L * (a / 2) / (k * n)
points(a + 1) = pa(1) + H * Sin(2 * PI * a / k / 2)
Next
Set sinObj = ThisDrawing.ModelSpace.AddLightWeightPolyline(points)
sinObj.Rotate pa, b
ZoomExtents
End Sub
Public Function dis(pa, pb As Variant) As Double
dis = ((pa(0) - pb(0)) ^ 2 + (pa(1) - pb(1)) ^ 2 + (pa(2) - pb(2)) ^ 2) ^ 0.5
End Function
Sub sin2()
'由正弦曲线的起点、幅度、周期、周波数、方向来画正弦曲线
Dim sinObj As AcadLWPolyline
Dim points() As Double
Dim a, n, k As Integer
Dim H, f As Double
Dim pa, pb As Variant
PI = 3.1415926535
pa = ThisDrawing.Utility.GetPoint(, "请输入正弦曲线起点:")
H = ThisDrawing.Utility.GetDistance(pa, "请正弦曲线幅度:")
f = ThisDrawing.Utility.GetDistance(pa, "请正弦曲线周期:")
n = ThisDrawing.Utility.GetReal("请输入正弦曲线周波数:")
k = ThisDrawing.Utility.GetReal("请输入正弦曲线每周波线段数(建议不小于36):")
pb = ThisDrawing.Utility.GetPoint(pa, "请输入点以确定正弦曲线的方向:")
ReDim points(0 To 2 * k * n + 1) As Double
b = ThisDrawing.Utility.AngleFromXAxis(pa, pb)
For a = 0 To 2 * k * n Step 2
points(a) = pa(0) + f * (a / 2) / k
points(a + 1) = pa(1) + H * Sin(2 * PI * a / k / 2)
Next
Set sinObj = ThisDrawing.ModelSpace.AddLightWeightPolyline(points)
sinObj.Rotate pa, b
ZoomExtents
End Sub
页:
[1]