Preorder Windows 7 and save 50% or more
 
Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Bookstore...
 
Feedback
Tip Jar
 
XML RSS Feed
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
 
TitleDraw a non-triangular Sierpinski gasket (fractal)
DescriptionThis example shows how to draw a non-triangular Sierpinski gasket (fractal) in Visual Basic 6.
KeywordsSierpinski gasket, fractal, random
CategoriesGraphics
 
When the user left-clicks, the program saves the point to use as a corner. When the user right-clicks, the program calls subroutine PlotPoints to draw the fractal.

PlotPoints picks a random corner to use as a starting point. It the repeatedly picks a new random corner and moves halfway from the current point to this corner, and plots the resulting point. When this process repeats enough times, the program draws a Sierpinski gasket.

If you cover and expose the form, the program draws the same Sierpinski gasket.

 
Private m_Running As Boolean
Private m_NumPoints As Integer
Private m_CornerX() As Single
Private m_CornerY() As Single

Private Const RADIUS As Single = 60

Private Sub Form_Load()
    Randomize
End Sub

Private Sub Form_MouseDown(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    If m_Running Then
        ' Stop running.
        m_Running = False
        m_NumPoints = 0
    Else
        ' Left or right button?
        If Button = vbLeftButton Then
            ' If this is the first point,
            ' erase the previous picture.
            If m_NumPoints = 0 Then Me.Cls

            ' Save the point.
            m_NumPoints = m_NumPoints + 1
            ReDim Preserve m_CornerX(0 To m_NumPoints - 1)
            ReDim Preserve m_CornerY(0 To m_NumPoints - 1)
            m_CornerX(m_NumPoints - 1) = X
            m_CornerY(m_NumPoints - 1) = Y
            Me.Circle (X, Y), RADIUS
        Else
            ' See if we have enough points.
            If m_NumPoints < 2 Then
                ' We need more points.
                MsgBox "You must left-click at least two " & _
                    "points before right-clicking.", _
                    vbExclamation, "Need More Points"
            Else
                ' Start.
                m_Running = True
                PlotPoints
            End If
        End If
    End If
End Sub

Private Sub Form_Resize()
Dim wid As Single

    wid = Me.ScaleWidth - 2 * Label1.Left
    If wid < 120 Then wid = 120
    Label1.Width = wid
End Sub

Private Sub PlotPoints()
Dim i As Integer
Dim last_x As Single
Dim last_y As Single

    ' Pick a starting point.
    i = Int(Rnd * m_NumPoints)
    last_x = m_CornerX(i)
    last_y = m_CornerY(i)

    ' Plot points until the user clicks again.
    Do While m_Running
        ' Pick the next corner.
        i = Int(Rnd * m_NumPoints)

        ' Move halfway from the current point
        ' to the new corner.
        last_x = (last_x + m_CornerX(i)) / 2
        last_y = (last_y + m_CornerY(i)) / 2
        Me.PSet (last_x, last_y)

        ' Check for events.
        DoEvents
    Loop
End Sub

Private Sub Form_Unload(Cancel As Integer)
    End
End Sub
 
Experiment with different corner points. Try a triangle, rectangle (you may need to use code to position the points exactly), and pentagon. Try a rectangle with three extra points at the same spot in the middle.

For more information on drawing fractals and other graphics, see my book Visual Basic Graphics Programming.

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