const Vector = p5.Vector; function Segment(startx, starty, len, heading){ this.start = new Vector(startx, starty); this.len = len; this.reference = 0; this.heading = heading; this.child = null; this.parent = null; }; Segment.prototype.end = function(){ angle = this.heading + this.reference; let x = this.len * cos(angle) + this.start.x; let y = this.len * sin(angle) + this.start.y; return new Vector(x,y); }; Segment.prototype.update = function(){ if(!this.parent) return; this.parent.heading = Vector.sub(this.start, this.parent.start).heading(); this.parent.start.add(Vector.sub(this.start, this.parent.end())); this.parent.update(); }; Segment.prototype.setHeading = function(angle){ this.heading = angle; this.update(); }; Segment.prototype.draw = function(){ push(); stroke(255); strokeWeight(5); line( this.start.x, this.start.y, this.end().x, this.end().y); pop(); }; Segment.prototype.expand_parent = function(stx, sty, len, heading){ let next = new Segment(stx, sty, len, heading); next.child = this; this.parent = next; return next; }; Segment.prototype.eats_parents = function(){ let start = this.start; let end = this.end(); for(let theSeg = this.parent.parent; theSeg != null; theSeg = theSeg.parent) if(doIntersect(start, end, theSeg.start, theSeg.end())) return true; return false; };