Draw a strange attractor fractal in C#

If you plot a sequence of points (x0, y0), (x1, y1), ..., the points can settle into one of several patterns. For example, they can enter a repeating cycle, tend towards infinity, or look chaotic.

The points can also follow a strange attractor. In that case, the points clearly follow some sort of pattern but not an obvious repeating cycle. If you plot the points over time, a strange, ghostly image emerges.

This example draws a particular strange attractor described by Eric W. Weisstein at Wolfram MathWorld. 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 the coefficients A1, A2, ... A11.

This 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 to 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 refreshes the PictureBox that displays the Bitmap so you can see the result.

// Plot 1,000 points.
private void tmrDrawPoint_Tick(object sender, EventArgs e)
{
for (int i = 1; i <= 1000; i++)
{
double new_x = A[0] + A[1] * X + A[2] * X * X + A[3] * X * Y + A[4] * Y + A[5] * Y * Y;
double new_y = 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;

int pix_x = (int)Math.Round((X - m_Wxmin) / m_Wwid * m_Wid);
int pix_y = (int)Math.Round(m_Hgt - (Y - m_Wymin) / m_Whgt * m_Hgt - 1);
if ((pix_x >= 0) && (pix_x < m_Wid) &&
(pix_y >= 0) && (pix_y < m_Hgt))
{
m_Bitmap.SetPixel(pix_x, pix_y, Color.Blue);
}
}

// Display the result.
picCanvas.Refresh();
}

See the code for further details.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.