Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleDraw the Barnsley's Fern fractal
DescriptionThis example shows how to draw the Barnsley's Fern fractal in Visual Basic 6.
KeywordsBarnsley's Fern, fractal, iterated functions, iterated function system
CategoriesGraphics
 

See Barnsley's Fern by Eric W. Weisstein from MathWorld, a Wolfram Web Resource.

The program starts from a random point. At each step, it randomly picks a function (with non-uniform probability) and applies the function to the point to find the next point. It then plots that point.

Each function has the form:

    X(n+1) = A * X(n) + B * Y(n) + C
    Y(n+1) = D * X(n) + E * Y(n) + F

The Form_Load event handler initializes the parameters for each function in the m_Func array, and the probability for each function. It also sets the color to be used when plotting a point found by each function. Using different colors is interesting and gives you some sense of what each set of functions does but the final result here is drawn in green.

 
Private m_Prob(0 To 3) As Single
Private m_Func(0 To 3, 0 To 1, 0 To 1) As Single
Private m_Plus(0 To 3, 0 To 1) As Single
Private m_Clr(0 To 3) As OLE_COLOR

Private Sub Form_Load()
    Randomize
    picCanvas.AutoRedraw = True
    picCanvas.Scale (-4, 10)-(4, 0)

    ' Initialize the fern functions.
    m_Clr(0) = vbRed
    m_Prob(0) = 0.01
    m_Func(0, 0, 0) = 0
    m_Func(0, 0, 1) = 0
    m_Func(0, 1, 0) = 0
    m_Func(0, 1, 1) = 0.16
    m_Plus(0, 0) = 0
    m_Plus(0, 1) = 0

    m_Clr(1) = vbGreen
    m_Prob(1) = 0.85
    m_Func(1, 0, 0) = 0.85
    m_Func(1, 0, 1) = 0.04
    m_Func(1, 1, 0) = -0.04
    m_Func(1, 1, 1) = 0.85
    m_Plus(1, 0) = 0
    m_Plus(1, 1) = 1.6

    m_Clr(2) = vbBlue
    m_Prob(2) = 0.08
    m_Func(2, 0, 0) = 0.2
    m_Func(2, 0, 1) = -0.26
    m_Func(2, 0, 1) = -0.23
    m_Func(2, 1, 1) = 0.22
    m_Plus(2, 0) = 0
    m_Plus(2, 1) = 1.6

    m_Clr(3) = vbWhite
    m_Prob(3) = 0.06
    m_Func(3, 0, 0) = -0.15
    m_Func(3, 0, 1) = 0.28
    m_Func(3, 1, 0) = 0.26
    m_Func(3, 1, 1) = 0.24
    m_Plus(3, 0) = 0
    m_Plus(3, 1) = 0.44

    m_Clr(0) = vbGreen
    m_Clr(1) = vbGreen
    m_Clr(2) = vbGreen
    m_Clr(3) = vbGreen
End Sub
 
When you click the Go button, the following code picks the random starting point. It then repeatedly applies the randomly selected functions.
 
Private Sub cmdGo_Click()
Dim i As Long
Dim j As Integer
Dim num As Single
Dim x As Single
Dim y As Single
Dim x1 As Single
Dim y1 As Single
Dim func_num As Integer
Dim clr As OLE_COLOR

    If cmdGo.Caption = "Go" Then
        picCanvas.Cls
        cmdGo.Caption = "Stop"
        Screen.MousePointer = vbHourglass
        DoEvents

        x = 1
        y = 1
        For i = 1 To 100000
            If i Mod 1000 = 0 Then DoEvents
            If cmdGo.Caption = "Go" Then Exit For

            num = Rnd
            For j = 0 To 3
                num = num - m_Prob(j)
                If num <= 0 Then
                    func_num = j
                    clr = m_Clr(j)
                    Exit For
                End If
            Next j

            x1 = x * m_Func(func_num, 0, 0) + y * _
                m_Func(func_num, 0, 1) + m_Plus(func_num, 0)
            y1 = x * m_Func(func_num, 1, 0) + y * _
                m_Func(func_num, 1, 1) + m_Plus(func_num, 1)
            x = x1
            y = y1
            picCanvas.PSet (x, y), clr
        Next i
        cmdGo.Caption = "Go"

        Screen.MousePointer = vbDefault
    Else
        cmdGo.Caption = "Go"
    End If
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated