# VB6之Mandelbrot集

Mandelbrot真是上帝之作，数学之美最直观的表现。

VB6中没有复数类型只能用Type自定义个complex类型，一些方法（Magnitude）也只能用VB6重新实现。

http://www.cnblogs.com/anderslly/archive/2008/10/10/mandelbrot-set-by-fsharp.html

VB6的实现代码：

Private Const maxIterations = 100
Private Const scalingFactor = 1# / 200#
Private colors

Private Type complex
fx As Double
fy As Double
End Type

Private iteration
Private current As complex
Private temp As complex

colors = Array(vbRed, vbGreen, vbBlue, vbYellow, vbMagenta, vbCyan, vbWhite)
End Sub

Private Function mapPlane(ByVal x As Double, ByVal y As Double) As complex
Dim cp As complex
cp.fx = ((x) * scalingFactor) - 2#
cp.fy = ((y) * scalingFactor) - 1#
mapPlane = cp
End Function

Private Function Magnitude(cp As complex) As Double
Magnitude = Sqr(cp.fx * cp.fx + cp.fy * cp.fy)
End Function

Private Function Mandnext(cp1 As complex, cp2 As complex) As complex
Dim cp As complex
cp.fx = cp1.fx * cp1.fx - cp1.fy * cp1.fy + cp2.fx
cp.fy = 2 * cp1.fx * cp1.fy + cp2.fy
Mandnext = cp
End Function

Private Sub Command1_Click()
For x = 0# To 600#
For y = 0# To 400#
iteration = 0
current = mapPlane(x, y)
temp = current
Do While (Magnitude(temp) <= 2# And iteration < maxIterations)
temp = Mandnext(temp, current)
iteration = iteration + 1
Loop

If iteration = maxIterations Then
PSet (10 + x, 10 + y), vbBlack
Else
PSet (10 + x, 10 + y), colors(iteration Mod 7)
End If
Next
Next
End Sub

posted @ 2014-12-18 15:45  lichmama  阅读(640)  评论(0编辑  收藏  举报