Home Search   What's New Index Books Links Q & A Newsletter Banners   Feedback Tip Jar MSDN Visual Basic Community

Title Draw a fractal Pickover strange attractor This example shows how to draw a fractal Pickover strange attractor in Visual Basic. fractal, Pickover, Pickover attractor, strange attractor Graphics

Suppose you perform a series of iterations of equations to generate points. Sometimes the points converge to one or more points. For example, the equations X(n) = X(n - 1) / 2, Y(n) = Y(n - 1) / 3 approach the point (0, 0) as n grows large.

The points to which the equations converge is called an attractor.

Some equations are drawn towards a collection of points that is not easily defined but that somehow has a coherent shape. These points are called a strange attractor.

Clifford Pickover discovered that the following equations geneate points that are drawn to a strange attractor.

```    X(n) = Sin(A * Y(n - 1)) - Z(n - 1) * Cos(B * X(n - 1))
Y(n) = Z(n) * Sin(C * X(n - 1)) - Cos(D * Y(n - 1))
Z(n) = Sin(X(n - 1))```

Here A, B, C, and D are constants.

The following code plots these points.

```' Draw the curve.
Private Sub DrawCurve()
...

' Get the drawing parameters.
GetParameters
picCanvas.ForeColor = PointColor

' Compute the values.
x = X0
y = Y0
z = Z0
i = 0
Do While Running
' Move to the next point.
x2 = Sin(A * y) - z * Cos(B * x)
y2 = z * Sin(C * x) - Cos(D * y)
z = Sin(x)
x = x2
y = y2

' Plot the point.
Select Case SelectedPlane
Case plane_XY
picCanvas.PSet (x * xscale + xoff, y * _
yscale + yoff)
Case plane_XZ
picCanvas.PSet (x * xscale + xoff, z * _
zscale + zoff)
Case plane_YZ
picCanvas.PSet (y * yscale + yoff, z * _
zscale + zoff)
End Select

' To make things faster, only DoEvents
' every 100 times.
i = i + 1
If i > 100 Then
i = 0
DoEvents
End If
Loop
End Sub```

Use the program's option buttons to plot X-Y, X-Z, or Y-Z projectsion of the points in different colors. You can also change the equations' constants and starting value to see what happens.

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