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

Title Draw strange attractors (fractals) in Visual Basic .NET This example shows how to draw strange attractors (fractals) in Visual Basic .NET. strange attractor, fractal, equation, graphics, VB.NET Graphics, Algorithms, VB.NET

If you plot a sequence of point (x1, y1), (x1, y2), ..., the points can settle into one of several patterns. For example, they can enter a repeating cycle, tend towards infinity, or look chaotic. Another option is following a strange attractor. In that case, the points clearly follow some pattern but not an obvious repeating cycle.

This example plots points given by the equations:

```    X' = A0 + A1 * x + A2 * x * x + A3 * x * y + A4 * y + A5 * y * y
Y' = A6 + A7 * x + A8 * x * x + A9 * x * y + A10 * y + A11 * y * y```

For various values of A1, A2, ... A11.

The program uses a Timer to plot points. The following code shows the Tick event handler that draws the curve. It plots 1,000 points at a time to improve performance and reduce flicker. For each iteration, the routine generates the next (X, Y) point and draws it on the Bitmap named m_Bitmap. After it has finished its 1,000 points, it draws the Bitmap onto the picCanvas control so you can see the result.

```Private X As Double
Private Y As Double

Private Sub tmrDrawPoint_Tick(ByVal sender As _
System.Object, ByVal e As
System.EventArgs) Handles tmrDrawPoint.Tick
For i As Integer = 1 To 1000
Dim new_x As Double = A(0) + A(1) * X + A(2) * X * _
X + A(3) * X * Y +
A(4) * Y + A(5) * Y * Y
Dim new_y As Double = A(6) + A(7) * X + A(8) * X * _
X + A(9) * X * Y +
A(10) * Y + A(11) * Y * Y
X = new_x
Y = new_y

Dim pix_x As Integer = (X - m_Wxmin) / m_Wwid * _
m_Wid
Dim pix_y As Integer = m_Hgt - (Y - m_Wymin) / _
m_Whgt * m_Hgt - 1
If (pix_x >= 0) AndAlso (pix_x < m_Wid) AndAlso _
(pix_y >= 0) AndAlso (pix_y < m_Hgt) _
Then
m_Bitmap.SetPixel(pix_x, pix_y, Color.Blue)
End If
Next i

' Display the result.
Dim gr As Graphics = picCanvas.CreateGraphics()
gr.DrawImage(m_Bitmap, 0, 0)
gr.Dispose()
End Sub```

See the code for further details.

For more information on this kind of strange attractor, see Strange Attractor by Eric W. Weisstein from MathWorld--A Wolfram Web Resource.