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 a fractal binary tree
DescriptionThis example shows how to draw a fractal binary tree in Visual Basic 6.
Keywordsfractal, binary tree, tree
CategoriesGraphics
 
The DrawBranch subroutine draws a branch and then recursively draws two smaller branches from the end of that one.
 
' Recursively draw a binary tree branch.
Private Sub DrawBranch(ByVal depth As Integer, ByVal X As _
    Single, ByVal Y As Single, ByVal length As Single, _
    ByVal theta As Single, ByVal length_scale As Single, _
    ByVal dtheta As Single)
Dim x1 As Integer
Dim y1 As Integer

    ' See where this branch should end.
    x1 = X + length * Cos(theta)
    y1 = Y + length * Sin(theta)
    picCanvas.Line (X, Y)-(x1, y1)

    ' If depth > 1, draw the attached branches.
    If depth > 1 Then
        DrawBranch depth - 1, x1, y1, length * _
            length_scale, theta + dtheta, length_scale, _
            dtheta
        DrawBranch depth - 1, x1, y1, length * _
            length_scale, theta - dtheta, length_scale, _
            dtheta
    End If
End Sub
 
Try adding some randomness to the program. You can change the lengths of branches, their angles, and the number of branches at each point. You can also make each branch somewhat curvy instead of straight. If you tweak the parameters properly, the results can look a lot like real trees.

For more information on fractals, see my book Visual Basic Graphics Programming.

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated