|
|
Const pi = 3.14159
Private Sub Form_Click()
ScaleTop = 50
ScaleLeft = 0
ScaleWidth = 100
ScaleHeight = -50
Call koch(5, 0, 20, 20, 80, 20, 1)
End Sub
Sub koch(n As Integer, s As Integer, ax As Single, ay As Single, bx As Single, by As Single, c As Single)
Dim cx As Single
Dim cy As Single
Dim dx As Single
Dim dy As Single
Dim ex As Single
Dim ey As Single
Dim fx As Single
Dim fy As Single
Dim l As Single
Dim alpha As Single
If n = 2 Then
Line (ax, ay)-(bx, by)
Else
cx = ax + (bx - ax) / 3
cy = ay + (by - ay) / 3
ex = bx - (bx - ax) / 3
ey = by - (by - ay) / 3
l = Sqr((ex - cx) * (ex - cx) + (ey - cy) * (ey - cy))
If s = 0 Then
alpha = pi / 2
ElseIf s = 1 Then
alpha = 2 * pi
ElseIf s = -1 Then
alpha = pi
End If
dy = cy + Sin(alpha) * l
dx = cx + Cos(alpha) * l
fx = ex + Cos(alpha) * l
fy = ey + Sin(alpha) * l
Call koch(n - 1, 0, ax, ay, cx, cy, c)
Call koch(n - 1, 1, cx, cy, dx, dy, c)
Call koch(n - 1, 0, dx, dy, fx, fy, c)
Call koch(n - 1, -1, fx, fy, ex, ey, c)
Call koch(n - 1, 0, ex, ey, bx, by, c)
Call koch(n - 1, 2, cx, cy, ex, ey, c)
End If
End Sub
迭代次数为一时正确,但是大于1就有问题了 |
|