functionality for adding a single pendulum

main
Benjamin Kraft 1 year ago
parent c8a389b809
commit 75fdcebf73
  1. 29
      public/data/scripts/ts/manager.ts
  2. 12
      public/data/scripts/ts/pendulum.ts

@ -7,22 +7,7 @@ class Manager {
playing = false;
static Size = 20;
constructor() {
p.colorMode(p.HSB, 100);
let count = 100;
for (let i = 0; i < count; i++){
let rad = i / count / 1e3 + p.PI * 1.05;
let hue = i / count * 100;
let color = p.color(hue, 100, 100);
this.pendula.push(
new Pendulum([1, 1, 1, 1, 1, 1, 1, 1], [50, 50, 50, 50, 50, 50, 50, 100 + i / count / 10000], color)
);
}
p.colorMode(p.RGB);
}
static Size = 200;
init(){
// @ts-ignore
@ -71,7 +56,17 @@ class Manager {
},
methods: {
add() {
if (this.multiple){
} else {
let color = p.color(this.color)
let M = this.masses.slice(0, this.segmentCount);
let L = this.lengths.slice(0, this.segmentCount);
let newPendulum = new Pendulum(M, L, color, this.startAngle);
manager.pendula.push(newPendulum);
}
p.colorMode(p.HSB, 100);
p.colorMode(p.RGB);
},
deleteAll(){
if (confirm("Delete all pendulums?")){
@ -107,7 +102,7 @@ class Manager {
update(){
if (this.playing) {
const h = this.timescale / Math.max(p.frameRate(), 1);
const h = this.timescale / Math.max(p.frameRate(), 30);
this.pendula.forEach(p => p.update(h));
}
}

@ -5,15 +5,16 @@ class Pendulum {
size: number;
constructor(readonly M: number[], readonly L: number[], readonly color: p5.Color) {
constructor(readonly M: number[], readonly L: number[], readonly color: p5.Color, startAngle: number) {
console.assert(M.length === L.length, M, L, "Masses and Lengths are not of equal length!");
this.size = M.length;
startAngle *= Math.PI / 180;
let direction = new Vector(Math.sin(startAngle), Math.cos(startAngle));
let currentPosition = new Vector(0, 0);
for (let i = 0; i < this.size; i++){
let a = Math.sqrt(L[i] * L[i] / 2);
currentPosition.addC(-L[i], 0);
currentPosition.add(Vector.Mult(direction, L[i]));
this.X.push(currentPosition.copy());
this.V.push(new Vector(0, 0));
}
@ -78,13 +79,18 @@ class Pendulum {
p.strokeWeight(1);
p.fill(255);
let scale = p.height / Manager.Size;
let p1 = new Vector(0, 0);
for (let p2 of this.X){
p2 = p2.copy();
p2.mult(scale);
p.line(p1.x, p1.y, p2.x, p2.y);
p1 = p2.copy();
}
for (let p2 of this.X){
p2 = p2.copy();
p2.mult(scale);
p.ellipse(p2.x, p2.y, 10, 10);
}

Loading…
Cancel
Save