let r1, r2, m1, m2, a1, a2;
let a1_v = 0;
let a2_v = 0;
let g;
let px2 = -1,
py2 = -1;
let cx, cy;
function setup() {
cnv = createCanvas(windowWidth * 0.9, windowHeight * 0.9);
rectMode(CENTER);
strokeWeight(0.1);
wc = min(width, height);
g = random(0.1, 1);
a1 = random(-PI / 2, PI / 2);
a2 = random(-PI / 2, PI / 2);
r1 = random(wc / 5, wc / 3);
r2 = random(wc / 5, wc / 3);
nRot = int(random(2, 20));
if (nRot === 4 || nRot === 8 || nRot === 16) {
nRot = 2;
}
m1 = random(2, 8);
m2 = random(2, 8);
cx = width / 2;
cy = wc / 4 + random(-wc / 10, wc / 10);
s = random(0.1, 0.4);
background(10, 30, 40);
noStroke();
}
function draw() {
translate(width / 2, height / 2);
t = frameCount;
a1_a =
(-g * (2 * m1 + m2) * sin(a1) +
-m2 * g * sin(a1 - 2 * a2) +
-2 *
sin(a1 - a2) *
m2 *
(a2_v * a2_v * r2 + a1_v * a1_v * r1 * cos(a1 - a2))) /
(r1 * (2 * m1 + m2 - m2 * cos(2 * a1 - 2 * a2)));
a2_a =
(2 *
sin(a1 - a2) *
(a1_v * a1_v * r1 * (m1 + m2) +
g * (m1 + m2) * cos(a1) +
a2_v * a2_v * r2 * m2 * cos(a1 - a2))) /
(r2 * (2 * m1 + m2 - m2 * cos(2 * a1 - 2 * a2)));
a1_v += a1_a * s;
a2_v += a2_a * s;
a1 += a1_v * s;
a2 += a2_v * s;
x1 = r1 * sin(a1) + cx;
y1 = r1 * cos(a1) + cy;
x2 = x1 + r2 * sin(a2);
y2 = y1 + r2 * cos(a2);
fill(abs(a2) * 120, abs(a2 + a1) * 205, abs(a2 + a1) * 255, 50);
stroke(abs(a1) * 50, 20 + abs(a2) * 205, 20 + abs(a2) * 255, 50);
for (let i = 0; i < nRot; i++) {
push();
rotate((TAU / nRot) * i);
line(-width / 2 + cx, -height / 2 + cy, -width / 2 + x1, -height / 2 + y1);
ellipse(-width / 2 + x1, -height / 2 + y1, m1 / 3, m1 / 3);
line(-width / 2 + x1, -height / 2 + y1, -width / 2 + x2, -height / 2 + y2);
ellipse(-width / 2 + x2, -height / 2 + y2, m2 / 3, m2 / 3);
pop();
}
border();
}
function border() {
push();
noFill();
stroke(20, 50, 60);
strokeWeight(4);
rect(0, 0, width - 20, height - 20);
pop();
}
function mousePressed() {
frameCount = 0;
setup();
draw();
}
function keyPressed() {
save(cnv, "chaotic", "jpg");
}
function windowResized() {
resizeCanvas(windowWidth * 0.9, windowHeight * 0.9);
}