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 using an incremental color scheme in VB.NET This example shows how to draw a fractal Pickover strange attractor using an incremental color scheme in VB.NET. Barnsley's Fern, fractal, iterated functions, iterated function system, VB.NET Graphics, VB.NET

This example draws a Pickover strange attractor much as the howto Draw a fractal Pickover strange attractor in VB.NET. However, that example plots each point visited by the iterated function system:

```    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))```

The example plots the X-Y, Y-Z, or X-Z coordinates of the points visited.

Instead of coloring each point visited in red, green, or blue, this example increases the point's red, green, or blue color components. This means points visited many times become brighter. It also allows colors to combine. You can plot the attractor's X-Y, Y-Z, and X-Z components on the same picture in different colors and combinations of red, green, and blue will show where the plots overlap.

```' 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

Dim dr As Integer
Dim dg As Integer
Dim db As Integer
If PointColor.R > 0 Then dr = 16
If PointColor.G > 0 Then dg = 16
If PointColor.B > 0 Then db = 16

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.
If True Then
Dim ix As Integer
Dim iy As Integer
Select Case SelectedPlane
Case PlaneTypes.plane_XY
ix = CInt(x * xscale + xoff)
iy = CInt(y * yscale + yoff)
Case PlaneTypes.plane_XZ
ix = CInt(x * xscale + xoff)
iy = CInt(z * zscale + zoff)
Case PlaneTypes.plane_YZ
ix = CInt(y * yscale + yoff)
iy = CInt(z * zscale + zoff)
End Select

Dim clr As Color = m_Bm.GetPixel(ix, iy)

Dim new_r As Integer = clr.R + dr
If new_r > 255 Then new_r = 255
Dim new_g As Integer = clr.G + dg
If new_g > 255 Then new_g = 255
Dim new_b As Integer = clr.B + db
If new_b > 255 Then new_b = 255

clr = Color.FromArgb(255, new_r, new_g, new_b)
m_Bm.SetPixel(ix, iy, clr)
Else
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
End If

' 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```

Because the colors are incremented only slowly, you need to plot a lot of points to get an interesting result. You might also try using VB .NET's image processing capabilities to perform gamma correction on the result. This would make the fainter colors more noticeable.

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