VB6之Mandelbrot集

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 = 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

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) - 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编辑  收藏  举报