From aea6a53cf26a5feb09f5a496b098195f60f29b81 Mon Sep 17 00:00:00 2001 From: Fr3deric Date: Sun, 3 Mar 2019 19:50:21 +0100 Subject: [PATCH] add new crown shape: ellipse --- baumgen.js | 62 ++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 55 insertions(+), 7 deletions(-) diff --git a/baumgen.js b/baumgen.js index 787d274..b81fee7 100644 --- a/baumgen.js +++ b/baumgen.js @@ -7,23 +7,29 @@ function gen_tree(pos) { baum.length = (20 + Math.random() * 20); - var shapernd = Math.random(); - if(shapernd < 0.5) { + var shapernd = Math.random() * 3; + if(shapernd < 1) { baum.crown = new KiteCrown( 70 + Math.random() * 30, 0.4 + Math.random() * 0.2, {bisect: 0.2 + Math.random() * 0.2}, - ) - } else { + ); + } else if(shapernd < 2) { baum.crown = new TriangleCrown( 70 + Math.random() * 30, 0.4 + Math.random() * 0.2, - ) + ); + } else { + baum.crown = new EllipseCrown( + 70 + Math.random() * 30, + 0.4 + Math.random() * 0.2, + {bisect: 0.1 + Math.random() * 0.4}, + ); } baum.angle = Math.PI / 2; // + Math.random() * 0.2 - 0.1; - if((baum.crown.shape == "kite") && (Math.random() < 0.5)) { + if((baum.crown.shape != "triangle") && (Math.random() < 0.5)) { var crown = baum.crown.clone(); crown.length *= 0.4; var b = { @@ -83,7 +89,7 @@ class Crown { class KiteCrown extends Crown { - constructor(length, width, {bisect = 0.3}) { + constructor(length, width, {bisect = 0.3}={}) { super(length, width, 'kite'); this.bisect = bisect; } @@ -138,6 +144,48 @@ class TriangleCrown extends Crown { } +class EllipseCrown extends Crown { + constructor(length, width, {bisect = 0.5}={}) { + super(length, width, 'ellipse'); + this.bisect = bisect; + } + + clone() { + return new EllipseCrown(this.length, this.width, {bisect: this.bisect}); + } + + draw(branch, bvec, cpos) { + this.undraw(); + var cvec = bvec.multiply(1/bvec.length * this.length); + var bswidth = this.length * this.width; + var bsvec = new Point(Math.sin(branch.angle) * bswidth / 2, + Math.cos(branch.angle) * bswidth / 2); + this._path = new Path(); + this._path.strokeColor = 'black'; + this._path.fillColor = 'white'; + this._path.strokeWidth = 2; + this._path.closed = true; + var cmid = cpos.add(cvec.multiply(this.bisect)); + this._path.add(new Segment(cpos, + bsvec.multiply(0.5), + bsvec.multiply(-0.5), + )); + this._path.add(new Segment(cmid.subtract(bsvec), + bvec.multiply(-0.5), + bvec.multiply(0.5), + )); + this._path.add(new Segment(cpos.add(cvec), + bsvec.multiply(-0.5), + bsvec.multiply(0.5), + )); + this._path.add(new Segment(cmid.add(bsvec), + bvec.multiply(0.5), + bvec.multiply(-0.5), + )); + } +} + + class Branch { constructor(data, parentBranch=null) { //this.parentBranch = parentBranch;