Skip to content

Instantly share code, notes, and snippets.

@reuk
Created March 26, 2015 16:33
Show Gist options
  • Save reuk/edd5bcd29e3e75aa3fe4 to your computer and use it in GitHub Desktop.
Save reuk/edd5bcd29e3e75aa3fe4 to your computer and use it in GitHub Desktop.
Simple lorenz system in Processing.
class Lorenz
{
Lorenz()
{
final float SIZE = 10;
PVector p = new PVector(random(-SIZE/2, SIZE), random(-SIZE/2, SIZE), random(-SIZE/2, SIZE));
for (int i = 0; i != positions.length; ++i)
{
positions[i] = p;
}
}
void update(float a, float b, float c, float step)
{
for (int i = positions.length - 1; i != 0; --i)
{
positions[i] = positions[i - 1];
}
positions[0] = lorenzPosition(positions[1], a, b, c, step);
}
void draw()
{
beginShape();
for (int i = 0; i != positions.length; ++i)
{
final float x = 1.0 - (i / float(positions.length));
stroke(x, 0.5, 0.75, 0.2);
vertex(positions[i]);
}
endShape();
}
private PVector lorenzVelocity(PVector p, float a, float b, float c)
{
return new PVector(a * (p.y - p.x), p.x * (b - p.z) - p.y, p.x * p.y - c * p.z);
}
private PVector lorenzPosition(PVector p, float a, float b, float c, float step)
{
PVector vel = lorenzVelocity(p, a, b, c);
vel.mult(step);
return PVector.add(p, vel);
}
PVector[] positions = new PVector[100];
}
int SIZE = 500;
void setup()
{
size(SIZE, SIZE, P3D);
colorMode(RGB, 1);
blendMode(ADD);
for (int i = 0; i != lorenz.length; ++i)
{
lorenz[i] = new Lorenz();
}
}
void vertex(PVector p) {vertex(p.x, p.y, p.z);}
Lorenz[] lorenz = new Lorenz[500];
void draw()
{
translate(width / 2, height);
rotateY(frameCount * 0.01);
rotateX(PI / 2);
scale(8);
background(0);
strokeWeight(0.2);
noFill();
for (int i = 0; i != lorenz.length; ++i)
{
lorenz[i].update(10, 28, 8.0 / 3, 0.01);
lorenz[i].draw();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment