     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 in VB.NET This example shows how to draw a fractal Pickover strange attractor in VB.NET. fractal, Pickover, Pickover attractor, strange attractor Graphics, VB.NET

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()
Const XMIN As Double = -2.1
Const XMAX As Double = 2.1
Const YMIN As Double = -2.1
Const YMAX As Double = 2.1
Const ZMIN As Double = -1.2
Const ZMAX As Double = 1.2

Dim xoff As Double
Dim yoff As Double
Dim zoff As Double
Dim xscale As Double
Dim yscale As Double
Dim zscale As Double
Dim x As Double
Dim y As Double
Dim z As Double
Dim x2 As Double
Dim y2 As Double
Dim i As Integer

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

Select Case SelectedPlane
Case PlaneTypes.plane_XY
xoff = m_Wid / 2
yoff = m_Hgt / 2
xscale = m_Wid / (XMAX - XMIN)
yscale = m_Hgt / (YMAX - YMIN)
Case PlaneTypes.plane_XZ
xoff = m_Wid / 2
zoff = m_Hgt / 2
xscale = m_Wid / (XMAX - XMIN)
zscale = m_Hgt / (ZMAX - ZMIN)
Case PlaneTypes.plane_YZ
yoff = m_Wid / 2
zoff = m_Hgt / 2
yscale = m_Wid / (YMAX - YMIN)
zscale = m_Hgt / (ZMAX - ZMIN)
End Select

' 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 PlaneTypes.plane_XY
m_Bm.SetPixel(CInt(x * xscale + xoff), _
CInt(y * yscale + yoff), PointColor)
Case PlaneTypes.plane_XZ
m_Bm.SetPixel(CInt(x * xscale + xoff), _
CInt(z * zscale + zoff), PointColor)
Case PlaneTypes.plane_YZ
m_Bm.SetPixel(CInt(y * yscale + yoff), _
CInt(z * zscale + zoff), PointColor)
End Select

' To make things faster, only DoEvents
' every 100 times.
i = i + 1
If i > 100 Then
i = 0
picCanvas.Refresh()
Application.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.  Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.  Updated 