VB6之Mandelbrot集

时间:2022-05-03 12:46:55

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

围观wiki和百科(百度百科)上关于Mandelbrot的解释至今仍是不能理解,没办法我高数实在学得不好。

搜素到园友用F#写的一篇实现代码,写得相对简单易懂,最起码能看出来是怎么生成的,于是将其翻译成了VB6。

因为没接触过F#,为了翻译那篇代码还百度了半天是F#语法和关键字还有那个复数的运算,感觉像是回到了大学数学课。

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

参考链接:

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

VB6的实现代码:

Private Const maxIterations =
Private Const scalingFactor = # / #
Private colors Private Type complex
fx As Double
fy As Double
End Type Private iteration
Private current As complex
Private temp As complex Private Sub Form_Load()
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) - #
cp.fy = ((y) * scalingFactor) - #
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 = * cp1.fx * cp1.fy + cp2.fy
Mandnext = cp
End Function Private Sub Command1_Click()
For x = # To #
For y = # To #
iteration =
current = mapPlane(x, y)
temp = current
Do While (Magnitude(temp) <= # And iteration < maxIterations)
temp = Mandnext(temp, current)
iteration = iteration +
Loop If iteration = maxIterations Then
PSet ( + x, + y), vbBlack
Else
PSet ( + x, + y), colors(iteration Mod )
End If
Next
Next
End Sub

贴张图看看效果:

VB6之Mandelbrot集

真赞!只是我现在还不知道怎么把分形放大,后面再研究看看吧。