// implementation of Lorenz's attractor
// more: https://en.wikipedia.org/wiki/Lorenz_attractor
// x axis: x position
// y axis: z position
// starting positions
let x = 0.01;
let y = 0;
let z = 0;
// dynamical variables for lorenz attractors
let a = 10;
let b = 28;
let c = 8.0 / 3.0;
// store previous positions so to draw fluid lines
let px = 0;
let py = 0;
let pz = 0;
function setup() {
createCanvas(400, 400);
background(255);
}
function draw() {
translate(200, 100);
let dt = 0.01;
let dx = a * (y - x) * dt;
let dy = (x * (b - z) - y) * dt;
let dz = (x * y - c * z) * dt;
x = x + dx;
y = y + dy;
z = z + dz;
scale(5);
strokeCap(SQUARE);
stroke(z * 2, 40);
if (frameCount > 0) {
line(x, z, px, pz);
}
px = x;
py = y;
pz = z;
}
// implementation of Lorenz Attractor
// more: https://en.wikipedia.org/wiki/Lorenz_attractor
// each plot corresponds to the x, y, and z positions
// starting positions
let x = 0.01;
let y = 0;
let z = 0;
// dynamical variables for lorenz attractors
let a = 10;
let b = 28;
let c = 8.0 / 3.0;
let points = []; // store points
function setup() {
createCanvas(400, 250);
}
function draw() {
background(255);
// iterate Lorenz equation
let dt = 0.01;
let dx = a * (y - x) * dt;
let dy = (x * (b - z) - y) * dt;
let dz = (x * y - c * z) * dt;
x = x + dx;
y = y + dy;
z = z + dz;
// add new point to array of points, forget oldest one to scroll
points.push(createVector(x, y, z));
if (points.length > width) {
points.shift(); // remove oldest value
}
// move down, draw x value
translate(0, 50);
for (let i = 1; i < points.length; i++) {
line(i - 1, points[i - 1].x, i, points[i].x);
}
// move down, draw y value
translate(0, 50);
for (let i = 1; i < points.length; i++) {
line(i - 1, points[i - 1].y, i, points[i].y);
}
// move down, draw z value
translate(0, 50);
for (let i = 1; i < points.length; i++) {
line(i - 1, points[i - 1].z, i, points[i].z);
}
}
// implementation of period doubling bifurcation.
// X axis designates increasing rate of growth (r)
// Y axis designates population (x)
// more: https://en.wikipedia.org/wiki/Period-doubling_bifurcation
let x = 0.5; // current population, between 0 and 1
let r = 2; // growth rate, between 0 and 4 usually
function setup() {
createCanvas(400, 400);
background(255);
stroke(0, 50);
}
function draw() {
// run simulation as long as r is less than 4
if (r < 4.0) {
x = 0.5; // reset pop
// Iterate to let the system settle (transient behavior)
for (let i = 0; i < 100; i++) {
x = r * x * (1 - x);
}
// Plot the final 100 points to show the stable orbit
for (let i = 0; i < 100; i++) {
x = r * x * (1 - x);
let px = map(r, 2, 4, 0, width);
let py = map(x, 0, 1, height, 0); // display population with lower values at the bottom of canvas
point(px, py);
}
r += 0.002; // add to r
} else {
noLoop(); // stop sketch at r = 4;
}
}
// implementation of Sierzpinki's triangle
// more: https://en.wikipedia.org/wiki/Sierpi%C5%84ski_triangle
let a, b, c; // triangle vertices
let vertices = [];
let pos; // current position
function setup() {
createCanvas(400, 400);
a = createVector(20, 20);
b = createVector(380, 20);
c = createVector(180, 380);
vertices = [a, b, c];
let inside = false;
while (!inside) {
pos = createVector(random(width), random(height));
inside = checkInside();
}
strokeWeight(0.5);
background(255);
stroke(0);
}
function draw() {
let v = random(vertices); // get a random vertex of the three
//console.log(v);
pos = p5.Vector.lerp(pos, v, 0.5);
point(pos);
}
function checkInside() {
//calculate edge vectors
let ab = p5.Vector.sub(a, b);
let bc = p5.Vector.sub(b, c);
let ca = p5.Vector.sub(c, a);
// calculate point to vertex vectors
let ap = p5.Vector.sub(pos, a);
let bp = p5.Vector.sub(pos, b);
let cp = p5.Vector.sub(pos, c);
// calc cross products
let cp1 = p5.Vector.cross(ab, ap);
let cp2 = p5.Vector.cross(bc, bp);
let cp3 = p5.Vector.cross(ca, cp);
return (
(cp1.z <= 0 && cp2.z <= 0 && cp3.z <= 0) ||
(cp1.z >= 0 && cp2.z >= 0 && cp3.z >= 0)
);
}