Skip to content

Instantly share code, notes, and snippets.

@andrusenn
Created November 22, 2022 21:05
Show Gist options
  • Save andrusenn/8ab8500e660692468fd3fca232fb095c to your computer and use it in GitHub Desktop.
Save andrusenn/8ab8500e660692468fd3fca232fb095c to your computer and use it in GitHub Desktop.
Armonografo y pendulos
/**
Maneja los péndulos
*/
class Harmonograph {
constructor(p) {
this.pendulums = p;
}
update(fn, laps = 10) {
for (let j = 0; j < laps; j++) {
let px = 0;
let py = 0;
this.pendulums.forEach((p, i) => {
if (this.pendulums.length == 1) {
if (typeof fn === "function") {
fn(p.x, p.y, i);
}
} else if (i == 0) {
// Primero
p.update();
px = p.x;
py = p.y;
} else {
// Los siguientes toman como referencia el anterior
p.update(px, py);
if (typeof fn === "function") {
fn(p.x, p.y);
}
px = p.x;
py = p.y;
}
});
}
}
addPendulum(p) {
this.pendulums.push(p);
}
addData(d) {
this.data = {
...d,
};
}
}
/**
Péndulo
*/
class Pendulum {
constructor(r1, r2, av) {
this.cx = 0;
this.cy = 0;
this.av = av;
this.r1 = r1;
this.r2 = r2;
this.x = 0;
this.x = 0;
this.ang = 0;
this.data = {};
}
update(cx = null, cy = null) {
if (typeof cx !== "null" && typeof cy !== "null") {
this.cx = cx;
this.cy = cy;
}
this.x = this.cx + Math.cos(this.ang) * this.r1;
this.y = this.cy + Math.sin(this.ang) * this.r2;
this.ang += this.av;
}
addData(d) {
this.data = {
...d,
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment