xxxxxxxxxx
76
let rhiz = [];
let restLength = 75;
let pullstepdiv = 10; //how much to divide the steps taken each frame
class Node {
constructor(x,y,id,links){
this.pos = createVector(x,y)
this.links = links;
}
drawLinks(){
for(let i=0;i<this.links.length;i++){
line(this.pos.x,this.pos.y,rhiz[this.links[i]].pos.x,rhiz[this.links[i]].pos.y);
}
}
pushPull(){
let move = createVector(0,0)
for(let i=0;i<this.links.length;i++){
let p = createVector( rhiz[this.links[i]].pos.x,rhiz[this.links[i]].pos.y );
move.add( p5.Vector.mult(
p5.Vector.sub(this.pos, p), // the vector towards the linked node
(restLength / p5.Vector.dist(this.pos, p)) - 1.0 ) // how close the current lengh is to the rest length
);
}
move.div(this.links.length*pullstepdiv)
this.pos.add(move);
}
}
class Leaf extends Node {
constructor(x,y,id,links){
super(x,y,id,links);
}
}
class Stem extends Node {
constructor(x,y,id,links){
super(brand);
}
grow(){
if(random()>0.9)
}
}
function setup() {
createCanvas(400, 400);
// random test network w 3 links each
for(let i=0; i<10; i++){
rhiz.push(new Node(
random()*width,
random()*height,
i,
[floor(random()*10), floor(random()*10), floor(random()*10)]));
}
}
function draw() {
background(255);
rhiz[0].pos = createVector(mouseX,mouseY)
for(let i=1;i<rhiz.length;i++){
rhiz[i].pushPull();
}
for(let i=0;i<rhiz.length;i++){
rhiz[i].drawLinks();
}
}