在窗体上放一个timer,两个command,然后添加代码:Dim can As Boolean
Dim stopdraw As BooleanPrivate Sub draw(x As Integer, y As Integer, l As Integer, d As Integer, k As Single)
Const Max = 5 ' You can modify this figure to draw a graph that you want
Dim px(1 To Max) As Integer, py(1 To Max) As Integer, st(1 To Max) As Integer
Dim ln(1 To Max) As Single
Dim i As Integer
Dim p As Integer, c As Integer, id As Integer
If l * d * k = 0 Then Exit Sub
p = 1: c = 1
px(1) = x: py(1) = y: ln(1) = l
For i = 2 To Max
ln(i) = ln(i - 1) * k
Next i
While c
DoEvents
Do Until can ' * You can rem these ... sentences
DoEvents ' * to make it draw faster.
If stopdraw Then Exit Sub ' *
Loop ' *
can = False ' *
'------draw--------------
Select Case st(p)
Case 0
id = (1 And p) Xor d
drawline px(p), py(p), Int(ln(p)), id
st(p) = st(p) + 1
If p < Max Then
p = p + 1
px(p) = px(p - 1) - ln(p - 1) * (id Xor 1)
py(p) = py(p - 1) - ln(p - 1) * id
st(p) = 0
End If
Case 1
id = (1 And p) Xor d
st(p) = st(p) + 1
If p < Max Then
p = p + 1
px(p) = px(p - 1) + ln(p - 1) * (id Xor 1)
py(p) = py(p - 1) + ln(p - 1) * id
st(p) = 0
End If
Case 2
p = p - 1
End Select
'------------------------
If p = 0 Then c = 0
Wend
Beep
End Sub
Private Sub drawline(x As Integer, y As Integer, l As Integer, d As Integer)
If d Then 'draw vertically
Form1.Line (x, y - l)-(x, y + l)
Else
Form1.Line (x - l, y)-(x + l, y)
End If
End SubPrivate Sub Command1_Click()
Timer1.Enabled = True
Call draw(150, 90, 50, 1, 0.70717)
End SubPrivate Sub Command2_Click()
Timer1.Enabled = False
stopdraw = True
End SubPrivate Sub Form_Load()
Timer1.Enabled = False
can = False
stopdraw = False
Timer1.Interval = 10
With Command1
.Caption = "draw"
.Height = 17
.Left = 272
.Top = 168
.Width = 38
End With
With Command2
.Caption = "stop"
.Height = 17
.Width = 38
.Top = 192
.Left = 272
End With
Form1.ScaleMode = 3
End SubPrivate Sub Timer1_Timer()
can = True
End Sub'---<<code end>>--------本来我使用大数组储存“生长点”的位置和“树枝”的大小,但这样作需要太大的内存:递归几次数组长度就需要2的几次方。
后来改进成回溯的算法,空间复杂度大大降低。代码中很多部分都不重要,核心部分是Sub draw中的主while循环。常量max越大,回溯深度越大。后面注释有星号的语句如果被注释掉(或删去),程序就会将图形一次性画出(保留则分步绘制)
Dim stopdraw As BooleanPrivate Sub draw(x As Integer, y As Integer, l As Integer, d As Integer, k As Single)
Const Max = 5 ' You can modify this figure to draw a graph that you want
Dim px(1 To Max) As Integer, py(1 To Max) As Integer, st(1 To Max) As Integer
Dim ln(1 To Max) As Single
Dim i As Integer
Dim p As Integer, c As Integer, id As Integer
If l * d * k = 0 Then Exit Sub
p = 1: c = 1
px(1) = x: py(1) = y: ln(1) = l
For i = 2 To Max
ln(i) = ln(i - 1) * k
Next i
While c
DoEvents
Do Until can ' * You can rem these ... sentences
DoEvents ' * to make it draw faster.
If stopdraw Then Exit Sub ' *
Loop ' *
can = False ' *
'------draw--------------
Select Case st(p)
Case 0
id = (1 And p) Xor d
drawline px(p), py(p), Int(ln(p)), id
st(p) = st(p) + 1
If p < Max Then
p = p + 1
px(p) = px(p - 1) - ln(p - 1) * (id Xor 1)
py(p) = py(p - 1) - ln(p - 1) * id
st(p) = 0
End If
Case 1
id = (1 And p) Xor d
st(p) = st(p) + 1
If p < Max Then
p = p + 1
px(p) = px(p - 1) + ln(p - 1) * (id Xor 1)
py(p) = py(p - 1) + ln(p - 1) * id
st(p) = 0
End If
Case 2
p = p - 1
End Select
'------------------------
If p = 0 Then c = 0
Wend
Beep
End Sub
Private Sub drawline(x As Integer, y As Integer, l As Integer, d As Integer)
If d Then 'draw vertically
Form1.Line (x, y - l)-(x, y + l)
Else
Form1.Line (x - l, y)-(x + l, y)
End If
End SubPrivate Sub Command1_Click()
Timer1.Enabled = True
Call draw(150, 90, 50, 1, 0.70717)
End SubPrivate Sub Command2_Click()
Timer1.Enabled = False
stopdraw = True
End SubPrivate Sub Form_Load()
Timer1.Enabled = False
can = False
stopdraw = False
Timer1.Interval = 10
With Command1
.Caption = "draw"
.Height = 17
.Left = 272
.Top = 168
.Width = 38
End With
With Command2
.Caption = "stop"
.Height = 17
.Width = 38
.Top = 192
.Left = 272
End With
Form1.ScaleMode = 3
End SubPrivate Sub Timer1_Timer()
can = True
End Sub'---<<code end>>--------本来我使用大数组储存“生长点”的位置和“树枝”的大小,但这样作需要太大的内存:递归几次数组长度就需要2的几次方。
后来改进成回溯的算法,空间复杂度大大降低。代码中很多部分都不重要,核心部分是Sub draw中的主while循环。常量max越大,回溯深度越大。后面注释有星号的语句如果被注释掉(或删去),程序就会将图形一次性画出(保留则分步绘制)
记得这次ISEF冬令营数学那边就是做的分形探讨,推广到三维的雪花分形,瀑布寒的
http://www.aivisoft.net/Fractal.rar