xxxxxxxxxx
91
class Point {
constructor(x, y, r) {
this.pos = createVector(x, y);
this.ppos = createVector(x, y);
this.vel = createVector(0, 0);
this.r = r;
colorMode(HSL);
this.col = color(random(180), random(80, 100), random(60, 100));
colorMode(RGB);
this.neighbors = new Set();
this.pinned = false;
}
update() {
if (this.pinned)
return;
if (this.pos.y >= height-this.r) {
this.vel.y = 0;
} else {
this.vel.y = 1000;
}
let ox = this.pos.x;
let oy = this.pos.y;
let dx = (this.pos.x - this.ppos.x) * dt + this.vel.x * dt*dt;
let dy = (this.pos.y - this.ppos.y) * dt + this.vel.y * dt*dt;
this.pos.add(dx, dy);
this.ppos.set(ox, oy);
if (this.pos.x < this.r) {
this.pos.x = this.r;
} else if (this.pos.x >= width-this.r) {
this.pos.x = width-this.r;
}
if (this.pos.y < this.r) {
this.pos.y = this.r;
} else if (this.pos.y >= height-this.r) {
this.pos.y = height-this.r;
}
for (let i = 0; i < 1; i++) {
let dir = createVector(0, 0);
let nd = 1000;
for (let pi of this.neighbors) {
let p = points[pi];
let ps = p5.Vector.sub(p.pos, this.pos);
let d = (ps.x*ps.x + ps.y*ps.y);
let l = (this.r + p.r)*(this.r + p.r);
if (d < 0.001)
continue;
let pc = (d - l) / d;
// if (d - l < nd) {
// nd = d - l;
// }
ps.mult(pc);
dir.add(ps);
}
dir.div(this.neighbors.size);
this.pos.add(dir);
}
// dir.div(this.neighbors.size);
// dir.add(this.pos)
// this.pos = p5.Vector.lerp(this.pos, dir, 1);
// let r = 0;
// for (let pi of this.neighbors) {
// let p = points[pi];
// let d = dist(this.pos.x, this.pos.y, p.pos.x, p.pos.y);
// r += abs(d - p.r);
// }
// r /= this.neighbors.size;
// this.r = lerp(r, this.r, 0.5);
}
draw() {
fill(this.col);
circle(this.pos.x, this.pos.y, this.r*2);
}
}