zoukankan      html  css  js  c++  java
  • 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

    贴张图看看效果:

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

  • 相关阅读:
    zabbix server 给agent 添加 CPU 监听笔记
    解决windows文件名过长无法删除的问题
    一个大的OpenAPI Specification yaml 分割成小的yaml文件
    win10 移除2345输入法
    树莓派登录ssh 很慢和开机启动tightvncserver
    docker gitlab-ce 容器root密码重置小记
    Arduino Nokia 5110 LCD屏幕使用小记
    Docker Compose文件下载慢的处理笔记
    Ubuntu 18.04 安装rtorrent笔记
    vagrant Which interface should the network bridge to?
  • 原文地址:https://www.cnblogs.com/lichmama/p/4171917.html
Copyright © 2011-2022 走看看