Creative Programming In Processing | Set 2 (Lorenz Attractor)

The Lorenz attractor system is most commonly expressed as 3 coupled non-linear differential equations-

Sample implementation of the differential equations in java:-

int i = 0;
double x0, y0, z0, x1, y1, z1;
double h = 0.01, a = 10.0, b = 28.0, c = 8.0 / 3.0;
  
x0 = 0.1;
y0 = 0;
z0 = 0;
for (i = 0; i < N; i++) {
    x1 = x0 + h * a * (y0 - x0);
    y1 = y0 + h * (x0 * (b - z0) - y0);
    z1 = z0 + h * (x0 * y0 - c * z0);
    x0 = x1;
    y0 = y1;
    z0 = z1;
    // Printing the coordinates
    if (i > 100)
        System.out.println(i + " " + x0 + " " + y0 + " " + z0);
}

                    

We will try to implement the above logic in Processing Java visually. Since we will be plotting the points in 3d, we need use a 3d renderer. We will be using the OPENGL renderer in the following implementation but a P3D renderer can also be used. We also need to use an external Processing library called PeasyCam which helps us to create an interactive camera object for 3d environment workflow. PeasyCam can be downloaded using the Processing IDE from tools ->Add Tool -> Libraries.

We will also be using the following functions to represent the Lorenz Attractor structure-

  • beginShape() – begins recording vertices for a shape.
  • endShape() – stops recording vertices for a shape.
  • vertex() – this function is used to specify the vertex coordinates for points, lines, triangles, quads, and polygons. It is used exclusively within the beginShape() and endShape() functions.

Implementation of Lorenz Attractor in Processing java:-

/* FINAL SKETCH FOR LORENZ ATTRACTOR */
  
import peasy.*; // Importing peasy package
  
// Initialization
float x = 0.01, y = 0, z = 0;
float a = 10, b = 28, c = 8.0 / 3.0;
  
// ArrayList of PVector objects to store
// the position vectors of the points to be plotted.
ArrayList<PVector> points = new ArrayList<PVector>();
PeasyCam cam; // Declaring PeasyCam object
  
void setup()
{
    // Creating the output window
    // and setting up the OPENGL renderer
    size(800, 600, OPENGL);
  
    // Initializing the cam object
    cam = new PeasyCam(this, 500);
}
  
void draw()
{
    background(0);
  
    // Implementation of the differential equations
    float dt = 0.01;
    float dx = (a * (y - x)) * dt;
    float dy = (x * (b - z) - y) * dt;
    float dz = (x * y - c * z) * dt;
    x += dx;
    y += dy;
    z += dz;
  
    // Adding the position vectors to points ArrayList
    points.add(new PVector(x, y, z));
    translate(0, 0, -80);
    scale(5);
    stroke(255);
    noFill();
  
    // Beginning plotting of points
    beginShape();
    for (PVector v : points) {
        // Adding random color to the structure in each frame
        stroke(random(0, 255), random(0, 255), random(0, 255));
        vertex(v.x, v.y, v.z); // plotting the vertices
    }
    endShape(); // Drawing ends
}

                    

Output:-

Source Materials

  • The Coding Train Coding Challenges by Daniel Shiffman.
  • The Lorenz Attractor in 3D by Paul Bourke.