What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleMake a curlicue fractal
DescriptionThis example shows how to make a curlicue fractal in Visual Basic 6.
Keywordscurlicue, fractal
See Curlicue Fractal by Eric W. Weisstein from MathWorld, a Wolfram Web Resource.

The program starts by drawing a line segment in the direction of the X axis. It then iteratively calculates theta and phi using these equations:

    theta(n + 1) = (theta(n) + 2 * Pi * S) Mod (2 * Pi)
    phi(n + 1) = (theta(n) + phi(n)) Mod (2 * Pi)

for some irrational number S. At each step, the program draws a new line segment in the direction given by phi.

Subroutine DrawCurlicue draws 10,000 segments following these rules.

Private Sub DrawCurlicue()
Const PI = 3.14159265
Const MY_SCALE As Single = 4

Dim xmax As Single
Dim ymax As Single
Dim s As Single
Dim theta As Single
Dim phi As Single
Dim x As Single
Dim y As Single
Dim i As Integer

    picCanvas.ScaleMode = vbPixels
    xmax = picCanvas.ScaleWidth / MY_SCALE
    ymax = picCanvas.ScaleHeight / MY_SCALE
    picCanvas.Scale (-xmax, -ymax)-(xmax, ymax)

    s = CSng(txtS.Text)
    theta = 0
    phi = 0
    x = 0
    y = 0
    picCanvas.CurrentX = x
    picCanvas.CurrentY = y
    For i = 1 To 10000
        x = x + Cos(phi)
        y = y + Sin(phi)
        picCanvas.Line -(x, y)

        phi = SingleMod(theta + phi, 2 * PI)
        theta = SingleMod(theta + 2 * PI * s, 2 * PI)
    Next i
End Sub
Visual Basic 6's Mod function always returns an integer. This program needs a floating point result so it uses its own SingleMod function instead of Mod.
' Return value Mod base.
Private Function SingleMod(ByVal value As Single, ByVal _
    base As Single) As Single
Dim divisor As Integer

    divisor = value \ base
    SingleMod = value - divisor * base
End Function
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.