The Lorenz attractor is a fractal based on the Lorenz oscillator, which is a 3-dimensional chaotic system.
For a full explanation on the Lorentz attractor, you can read the wikipedia article.
The central equations needed for the Lorenz oscillator are:
dx/dt = σ(y - x)
dy/dt = x(ρ - z) - y
dz/dt = xy - βz
σ is the Prandtl number, and is usually set to 10.
ρ is the Rayleigh number and can be varied.
β is set to 8/3.
Now, drawing the Lorenz attractor in C#, we are going to iterate a fixed number of times through these equations. In the code below, ρ is set to 28, and the number of iterations to 8000.
The code is able to draw the attractor in two dimensions or three dimensions, and the first part of the code calculates the transformations we will use later to transform the coordinates of attractor (which are in three dimensions) to screen coordinates.
We then iterate through each iteration, taking the x, y and z from the previous iteration, and running them through the Lorens oscillator equations to get our first set of derivatives.
We then add these to the previous x,y and z values, and then run them through the equations again, getting our second set of derivatives, and then add these to the x,y and z values as before. We repeat this until we get four sets of derivatives.
Once we have that we then add them together multiply by a scaling factor, and and them to x, y and z respectively.
Now that we have the new coordinates, we can plot a point at that position, applying our transformations to convert the three dimensional coordinates to screen coordinates.
Read More »